cdbmutil

package
v0.0.0-...-4bc21e1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CDBM_UTIL_CONFIG is default enviroment variable used to point to config file for cdmbutil
	CDBM_UTIL_CONFIG = "CDBM_UTIL_CONFIG"
)

Variables

View Source
var (
	// DefaultProtocolMap is global database protocol map used to determine
	// different settings for migrate command based on what database user is using
	DefaultProtocolMap = map[DBProtocol]DBProtocolConfig{
		PostgresProtocol: {
			DBProtocol:           PostgresProtocol,
			DatabaseType:         webutil.Postgres,
			SQLBindVar:           sqlx.DOLLAR,
			DriverConfig:         &postgres.Config{},
			MigrationTableSearch: postgresMigrationTableSearch,
		},
		CockroachdbProtocol: {
			DBProtocol:           CockroachdbProtocol,
			DatabaseType:         webutil.Postgres,
			SQLBindVar:           sqlx.DOLLAR,
			DriverConfig:         &cockroachdb.Config{},
			MigrationTableSearch: postgresMigrationTableSearch,
		},
	}
)
View Source
var (
	// ErrInvalidFileName is error to indicate that the sql files in given directory do not
	// have the correct naming convention
	ErrInvalidFileName = fmt.Errorf("cdbmutil: invalid sql file name - proper naming:<version>_<description>.<'up'|'down'>.sql")
)

Functions

func DefaultExecCmd

func DefaultExecCmd(c *exec.Cmd) error

DefaultExecCmd is default function for executing a command line tool

func DefaultFileMigrationFunc

func DefaultFileMigrationFunc(mig *migrate.Migrate, version int, mt MigrationsType) error

DefaultFileMigrationFunc is the default function to use with migrate library to determine whether to migrate database up, down or force

func DefaultGetDB

func DefaultGetDB(dbSettings BaseDatabaseSettings) (*sqlx.DB, error)

DefaultGetDB is default function for retrieving an instance of sqlx.DB based on settings passed

func DefaultGetMigrationFunc

func DefaultGetMigrationFunc(migDir string, db *sql.DB, protocolCfg DBProtocolConfig) (*migrate.Migrate, error)

DefaultGetMigrationFunc is the default function to retrieve migration configuration to use against database

func GetDatabaseDriver

func GetDatabaseDriver(db *sql.DB, protcol DBProtocol, cfg interface{}) (database.Driver, error)

GetDatabaseDriver retrieves database driver for migrate based on parameters passed

func GetNewDatabase

func GetNewDatabase(
	testSettings CDBMUtilSettings,
	cmdFunc func(*exec.Cmd) error,
	getDBFunc func(BaseDatabaseSettings) (*sqlx.DB, error),
) (*sqlx.DB, string, error)

GetNewDatabase will retrieve instance of sqlx.DB along with database name based on settings passed

func GetRandomString

func GetRandomString(length int) string

GetRandomString generates random string based on length passed

func MigrationInsertAndUpdateTable

func MigrationInsertAndUpdateTable(
	db webutil.QuerierExec,
	sqlBindVar int,
	insertQuery string,
	updateQueries []string,
) ([]interface{}, error)

MigrationInsertAndUpdateTable is util function that should take in a bulk insert query that returns the ids of all the inserts and will also execute multiple update queries based on the returned ids if passed

updateQueries parameter can be nil

Types

type BaseDatabaseSettings

type BaseDatabaseSettings struct {
	Settings         webutil.DatabaseSetting `yaml:"settings" mapstructure:"settings"`
	DatabaseType     string                  `yaml:"database_type" mapstructure:"database_type"`
	DatabaseProtocol string                  `yaml:"database_protocol" mapstructure:"database_protocol"`
}

type CDBMUtilSettings

type CDBMUtilSettings struct {
	InsertEndQuery       string               `yaml:"insert_end_query" mapstructure:"insert_end_query"`
	DBAction             DBAction             `yaml:"db_action" mapstructure:"db_action"`
	DBSetup              DBSetup              `yaml:"db_setup" mapstructure:"db_setup"`
	BaseDatabaseSettings BaseDatabaseSettings `yaml:"base_database_settings" mapstructure:"base_database_settings"`
}

func GetCDBMUtilSettings

func GetCDBMUtilSettings(envVar string) (CDBMUtilSettings, error)

GetCDBMUtilSettings retrieves CDBMUtilSettings based on envVar parameter passed

If envVar is empty string, then CDBM_UTIL_CONFIG is used as default

type ConnectionStringConfig

type ConnectionStringConfig struct {
	DBSettings webutil.DatabaseSetting
}

ConnectionStringConfig is config struct used to construct a connection string for the GetMigrationConnStr function

type CustomMigration

type CustomMigration struct {
	// Up should migrate database to next state
	Up CustomMigrationFunc

	// Down should migrate database to previous state
	Down CustomMigrationFunc
}

CustomMigration is config struct used to migrate database with custom go code

type CustomMigrationFunc

type CustomMigrationFunc func(db webutil.DBInterface) error

CustomMigrationFunc should implement migrating database up or down through custom code

type DBAction

type DBAction struct {
	CreateDB string       `yaml:"create_db" mapstructure:"create_db"`
	DropDB   string       `yaml:"drop_db" mapstructure:"drop_db"`
	Import   ImportConfig `yaml:"import" mapstructure:"import"`
}

type DBProtocol

type DBProtocol string

DBProtocol represents different database protocols

const (
	// PostgresProtocol is postgres protocol string when making connection to database
	PostgresProtocol DBProtocol = "postgres"

	// CockroachdbProtocol is cockroach protocol string when making connection to database
	CockroachdbProtocol DBProtocol = "cockroachdb"
)

type DBProtocolConfig

type DBProtocolConfig struct {
	// SQLBindVar determines what bind var to use for database
	SQLBindVar int

	// DatabaseType is what database is currently being used
	DatabaseType string

	// DBProtocol is what protocol to use when connecting to database
	DBProtocol DBProtocol

	// MigrationTableSearch determines if schema_migrations table exists
	// in database or not
	//
	// Should return nil if schema_migrations is found
	MigrationTableSearch func(db webutil.DBInterface) error

	// DriverConfig is config struct used for migrate library
	// for different settings based on database
	DriverConfig interface{}
}

DBProtocolConfig is config struct used to set up different settings for migrate command based on database being used

type DBSetup

type DBSetup struct {
	BaseSchemaFile  string           `yaml:"base_schema_file" mapstructure:"base_schema_file"`
	FileServerSetup *FileServerSetup `yaml:"file_server_setup" mapstructure:"file_server_setup"`
}

type FileMigrationFunc

type FileMigrationFunc func(mig *migrate.Migrate, version int, mt MigrationsType) error

FileMigrationFunc should implement migrating database up or down

type FileServerSetup

type FileServerSetup struct {
	BaseSchemaDir string `yaml:"base_schema_dir" mapstructure:"base_schema_dir"`
	FileServerURL string `yaml:"file_server_url" mapstructure:"file_server_url"`
}

type GetMigrationFunc

type GetMigrationFunc func(migDir string, db *sql.DB, protocolCfg DBProtocolConfig) (*migrate.Migrate, error)

GetMigrationFunc should implement getting migrate.Migrate based on migrations directory and database instance

type ImportConfig

type ImportConfig struct {
	ImportKeys []string          `yaml:"import_keys" mapstructure:"import_keys"`
	ImportMap  map[string]string `yaml:"import_map" mapstructure:"import_map"`
}

type MigrationSetupTeardownReturn

type MigrationSetupTeardownReturn struct {
	DB       *sqlx.DB
	Settings CDBMUtilSettings
	TearDown func()
}

func GetMigrationSetupTeardown

func GetMigrationSetupTeardown(
	cdbmUtilEnvVar string,
	execCmd func(*exec.Cmd) error,
	getDB func(BaseDatabaseSettings) (*sqlx.DB, error),
	importKeys []string,
) (MigrationSetupTeardownReturn, error)

GetMigrationSetupTeardown will retrieve instance of sqlx.DB along with function, that when called, will delete the currently created database

type MigrationsProtocol

type MigrationsProtocol string

MigrationsProtocol is enum for different database protocols

const (
	// FileProtocol is protocol used to find migration files on host machine
	FileProtocol MigrationsProtocol = "file://"
	GoBindData   MigrationsProtocol = "go-bindata"

	// GithubProtocol is protocol used to find migration files on github servers
	GithubProtocol MigrationsProtocol = "github://"

	// GitHubEnterpriseProtocol is protocol used to find migration files on github enterprise servers
	GitHubEnterpriseProtocol MigrationsProtocol = "github-ee://"

	// BitbucketProtocol is protocol used to find migration files on bitbucket servers
	BitbucketProtocol MigrationsProtocol = "bitbucket://"

	// GitlabProtcol is protocol used to find migration files on gitlab servers
	GitlabProtcol MigrationsProtocol = "gitlab://"

	// S3Protocol is protocol used to find migration files on s3 compatible servers
	S3Protocol MigrationsProtocol = "s3://"

	// GoogleCloudStorageProtocol is protocol used to find migration files on google cloud servers
	GoogleCloudStorageProtocol MigrationsProtocol = "gcs://"
)

Below are protocol strings used to connect to a variety of source urls for sql migrations files

type MigrationsType

type MigrationsType string

MigrationsType is enum for different migration type ie. "Up", "Down", "Force"

const (
	// MigrateTypeUp is MigrationsType's type for migrating a database forward
	MigrateTypeUp MigrationsType = "Up"

	// MigrateTypeDown is MigrationsType's type for reversing a database backward
	MigrateTypeDown MigrationsType = "Down"

	// MigrateTypeForce is MigrationsType's type for forcing a database migration up or down
	MigrateTypeForce MigrationsType = "Force"
)

Below are migration types that determine which direction to migrate a database

Jump to

Keyboard shortcuts

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