database

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: Apache-2.0 Imports: 25 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InMemorySqliteConfig = DatabaseConfig{
	DatabaseName: "sqlite",
	SQLite: &SQLiteConfig{
		Path: ":memory:",
	},
}

Functions

func GetDriver

func GetDriver(db *sql.DB, config DatabaseConfig) (database.Driver, error)

func MySQLDriver

func MySQLDriver(db *sql.DB) (database.Driver, error)

func MySQLUniqueViolation

func MySQLUniqueViolation(err error) bool

MySQLUniqueViolation returns true when the provided error matches the MySQL code for duplicate entries (violating a unique table constraint).

func New

func New(ctx context.Context, logger log.Logger, config DatabaseConfig) (*sql.DB, error)

New establishes a database connection according to the type and environmental variables for that specific database.

func NewAndMigrate

func NewAndMigrate(ctx context.Context, logger log.Logger, config DatabaseConfig) (*sql.DB, error)

func RunMigrations

func RunMigrations(logger log.Logger, config DatabaseConfig) error

func SQLite3Driver

func SQLite3Driver(db *sql.DB) (database.Driver, error)

func SqliteUniqueViolation

func SqliteUniqueViolation(err error) bool

SqliteUniqueViolation returns true when the provided error matches the SQLite error for duplicate entries (violating a unique table constraint).

func UniqueViolation

func UniqueViolation(err error) bool

UniqueViolation returns true when the provided error matches a database error for duplicate entries (violating a unique table constraint).

Types

type DatabaseConfig

type DatabaseConfig struct {
	MySQL        *MySQLConfig
	SQLite       *SQLiteConfig
	DatabaseName string
}

func RunMySQLDockerInstance

func RunMySQLDockerInstance(config *DatabaseConfig) (*DatabaseConfig, *dockertest.Resource, error)

type MySQLConfig

type MySQLConfig struct {
	Address  string
	User     string
	Password string
}

type SQLiteConfig

type SQLiteConfig struct {
	Path string
}

type TestMySQLDB

type TestMySQLDB struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

TestMySQLDB is a wrapper around sql.DB for MySQL connections designed for tests to provide a clean database for each testcase. Callers should cleanup with Close() when finished.

func CreateTestMySQLDB

func CreateTestMySQLDB(t *testing.T) *TestMySQLDB

CreateTestMySQLDB returns a TestMySQLDB which can be used in tests as a clean mysql database. All migrations are ran on the db before.

Callers should call close on the returned *TestMySQLDB.

func (*TestMySQLDB) Close

func (r *TestMySQLDB) Close() error

type TestSQLiteDB

type TestSQLiteDB struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

TestSQLiteDB is a wrapper around sql.DB for SQLite connections designed for tests to provide a clean database for each testcase. Callers should cleanup with Close() when finished.

func CreateTestSqliteDB

func CreateTestSqliteDB(t *testing.T) *TestSQLiteDB

CreateTestSqliteDB returns a TestSQLiteDB which can be used in tests as a clean sqlite database. All migrations are ran on the db before.

Callers should call close on the returned *TestSQLiteDB.

func (*TestSQLiteDB) Close

func (r *TestSQLiteDB) Close() error

Jump to

Keyboard shortcuts

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