helper

package
v0.10.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(db *gorm.DB, sql string, values any) error

Exec executes raw sql without return rows

func InitDatabase

func InitDatabase(db *gorm.DB, dbmap map[string]*gorm.DB) (err error)

InitDatabase initializes database tables and records with asynchronous processing support. It creates tables and inserts records that are registered via Register() or RegisterTo() functions. The function supports concurrent model registration at any stage - before, during, or after InitDatabase execution.

Key features:

  • Asynchronous table creation and record insertion using goroutines and channels
  • Thread-safe concurrent model registration support
  • Automatic handling of both default database and custom database instances
  • Real-time processing of models registered during initialization

NOTE: The version of gorm.io/driver/postgres lower than v1.5.4 have some issues. More details see: https://github.com/go-gorm/gorm/issues/6886

func Transaction

func Transaction(db *gorm.DB, fn func(tx *gorm.DB) error) error

Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

func Wait added in v0.10.0

func Wait()

Wait blocks until all pending database initialization operations are completed. It monitors three channels used by the InitDatabase function's background goroutine:

  • model.TableChan: Contains models waiting for table creation in the default database
  • model.TableDBChan: Contains models waiting for table creation in custom databases
  • model.RecordChan: Contains records waiting for insertion after table creation

This function is useful in scenarios where you need to ensure that all database tables and initial records are fully created before proceeding with application logic. Common use cases include:

  • Testing environments where you need to wait for complete database setup
  • Application startup sequences that depend on specific tables being available
  • Migration scripts that require all tables to be created before data operations

The function polls the channels every 100 milliseconds and prints progress information showing the number of pending operations in each channel. It returns only when all channels are empty, indicating that the InitDatabase background processing is complete.

NOTE: This function should be called after InitDatabase() has been invoked, as it relies on the background goroutine started by InitDatabase to process the channels. Calling Wait() before InitDatabase() will return immediately with a warning.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL