sql

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 22 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ConnMaxLifetime indicates the maximum amount of time a connection may be reused.
	ConnMaxLifetime time.Duration

	// ConnMaxIdleTime indicates the maximum amount of time a connection may be idle.
	ConnMaxIdleTime time.Duration

	// DriverName indicates the SQL driver to use, currently only supports:
	// 	- mysql
	// 	- postgres
	DriverName DriverName

	// MaxIdleConns indicates the maximum number of connections in the idle connection pool.
	MaxIdleConns int

	// MaxOpenConns indicates the maximum number of open connections to the database.
	MaxOpenConns int

	// SchemaSearchPath indicates the schema search path which is only used with "postgres".
	SchemaSearchPath string

	// SchemaMigrationsTable indicates the table name for storing the schema migration versions.
	SchemaMigrationsTable string

	// URI indicates the database connection string to connect.
	//
	// URI connection string documentation:
	//   - mysql: https://dev.mysql.com/doc/refman/8.0/en/connecting-using-uri-or-key-value-pairs.html#connecting-using-uri
	//   - postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
	URI string
}

Config indicates database connection configuration.

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a wrapper around sqlx.DB

func NewDB

func NewDB(c *Config, logger *logger.Logger) *DB

NewDB initialises the DB handle that is used to connect to a database.

func (*DB) Close

func (db *DB) Close() error

Close returns the connection to the connection pool.

func (*DB) Config

func (db *DB) Config() *Config

Config returns the db configurations.

func (*DB) Conn

func (db *DB) Conn(ctx context.Context) (*sql.Conn, error)

Conn return the database connection

func (*DB) DB

func (db *DB) DB() *sqlx.DB

DB returns the database.

func (*DB) Open

func (db *DB) Open() error

Open opens a database connection and verify with a ping.

The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.

func (*DB) Ping

func (db *DB) Ping() error

Ping verifies the connections to the master/replica databases are still alive, establishing a connection if necessary.

Ping uses context.Background internally; to specify the context, use PingContext.

type DriverName

type DriverName string

DriverName indicates the name of the driver

const (
	// MYSQL refers to the mysql driver
	MYSQL DriverName = "mysql"
	// POSTGRES refers to the postgres driver
	POSTGRES DriverName = "postgres"
)

type Migrate

type Migrate struct {
	*migrate.Migrate
	// contains filtered or unexported fields
}

Migrate is the DB migrator.

func NewMigrate

func NewMigrate(config *Config, embedFS embed.FS) (*Migrate, error)

NewMigrate initialises the DB migrator with open connection.

func (*Migrate) Open

func (d *Migrate) Open(rawURL string) (source.Driver, error)

Open retrieves the FS handle based on the DB driver.

func (*Migrate) Run

func (m *Migrate) Run() ([][]string, error)

Run returns the migration status.

type Seed

type Seed struct {
	// contains filtered or unexported fields
}

Seed is the DB seeder.

func NewSeed

func NewSeed(config *Config, embedFS embed.FS, logger *logger.Logger) (*Seed, error)

NewSeed initialises the DB seeder with open connection.

func (*Seed) Close

func (s *Seed) Close() error

Close closes the database and prevents new queries from starting.

func (*Seed) Run

func (s *Seed) Run() error

Run executes the seeding.

Jump to

Keyboard shortcuts

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