Documentation
¶
Overview ¶
Package sql provides function for managing connection to various database. You can open a database connection by calling Open function.
connection := "postgresql://user:password@host/db[?options]" db, _ := sql.Open(connection)
Index ¶
- func Migrate(db *sqlx.DB, service string, fs fs.FS) error
- func MigrateToVersion(db *sqlx.DB, service string, fs fs.FS, version uint) error
- func MigrateWithPath(db *sqlx.DB, fs fs.FS, service string, path string) error
- func Open(connection string, ops ...Option) (*sqlx.DB, error)
- func StatusCheck(ctx context.Context, db *sqlx.DB) error
- type Option
- type Queryable
- type TestingDB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Migrate ¶
Migrate looks at the currently active migration version of the service and will migrate all the way up (applying all up migrations). Migrate will look at the folder `db` by default (generally assets/db).
func MigrateToVersion ¶
MigrateToVersion should be use to apply down or up script to a given version
func MigrateWithPath ¶
MigrateWithPath looks at the currently active migration version of the service and will migrate all the way up (applying all up migrations) from the given fs path.
Types ¶
type Option ¶
type Option func(*options)
Option defines a SQL option.
func WithMaxConnIdleTime ¶
WithMaxConnIdleTime defines the maximum amount of time a connection may be idle.
func WithMaxConnLifeTime ¶
WithMaxConnLifeTime defines the maximum amount of time a connection may be reused.
func WithMaxIdleConns ¶
WithMaxIdleConns defines the maximum number of connections in the idle connection pool.
func WithMaxOpenConns ¶
WithMaxOpenConns defines the maximum number of open connections to the database.
func WithStatementCacheMode ¶
WithStatementCacheMode defines the maximum amount of time a connection may be idle.
type Queryable ¶
type Queryable interface { sqlx.Ext sqlx.ExecerContext sqlx.PreparerContext sqlx.QueryerContext sqlx.Preparer GetContext(context.Context, interface{}, string, ...interface{}) error SelectContext(context.Context, interface{}, string, ...interface{}) error Get(interface{}, string, ...interface{}) error MustExecContext(context.Context, string, ...interface{}) sql.Result PreparexContext(context.Context, string) (*sqlx.Stmt, error) QueryRowContext(context.Context, string, ...interface{}) *sql.Row Select(interface{}, string, ...interface{}) error QueryRow(string, ...interface{}) *sql.Row PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error) PrepareNamed(string) (*sqlx.NamedStmt, error) Preparex(string) (*sqlx.Stmt, error) NamedExec(string, interface{}) (sql.Result, error) NamedExecContext(context.Context, string, interface{}) (sql.Result, error) MustExec(string, ...interface{}) sql.Result NamedQuery(string, interface{}) (*sqlx.Rows, error) }
Queryable includes all methods shared by sqlx.DB and sqlx.Tx, allowing either type to be used interchangeably.
type TestingDB ¶
TestingDB abstracts a database that can be used in tests. defaults to connecting to localhost:5432 with username "postgres" and password "postgres".
The environment variable TESTINGDB_URL can be used to control the connection settings.
Example usage:
func TestMe(t *testing.T) { var db sql.TestingDB err := db.Open() if !assert.NoError(t, err) { return } defer db.Close() // connecting to the test database via generated dsn for this test purpose. database.Open(db.DSN) }