app

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: 18 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// CDBM_CONFIG is the default enviroment variable that should point to config file
	CDBM_CONFIG = "CDBM_CONFIG"
)

Variables

View Source
var DefaultRootNameCfg = RootNameConfig{
	DBProtocol: FlagName{
		LongHand:  "db-protocol",
		ShortHand: "b",
	},
	Env: FlagName{
		LongHand:  "env",
		ShortHand: "e",
	},
	Database: FlagName{
		LongHand:  "database",
		ShortHand: "d",
	},
	Host: FlagName{
		LongHand:  "host",
		ShortHand: "h",
	},
	User: FlagName{
		LongHand:  "user",
		ShortHand: "u",
	},
	Password: FlagName{
		LongHand:  "password",
		ShortHand: "w",
	},
	Port: FlagName{
		LongHand:  "port",
		ShortHand: "p",
	},
	SSLMode: FlagName{
		LongHand:  "ssl-mode",
		ShortHand: "m",
	},
	SSLRootCert: FlagName{
		LongHand:  "ssl-root-cert",
		ShortHand: "r",
	},
	SSLKey: FlagName{
		LongHand:  "ssl-key",
		ShortHand: "k",
	},
	SSLCert: FlagName{
		LongHand:  "ssl-cert",
		ShortHand: "c",
	},
	SSL: FlagName{
		LongHand:  "ssl",
		ShortHand: "s",
	},
	UseFileOnFail: FlagName{
		LongHand:  "use-file-on-fail",
		ShortHand: "f",
	},
}

Functions

func AppendValuesQuery

func AppendValuesQuery(
	insertQuery,
	insertEndQuery,
	updateQuery string,
	sqlBindVar int,
	bulkInsert uint,
	insertValuesQuery *string,
	counter *uint,
	isFinalExec bool,
	db webutil.QuerierExec,
) error

Types

type CDBM

type CDBM struct {
	// DB is database connection used
	//
	// This will be set in NewCDBM
	DB *sqlx.DB

	// DBProtocolCfg is config used for different database settings based
	// on database being used
	//
	// This will be set in NewCDBM
	DBProtocolCfg cdbmutil.DBProtocolConfig `yaml:"-" mapstructure:"-"`

	// MigrateFlags represents the flags for migrate command
	MigrateFlags MigrateFlagsConfig `yaml:"migrate_flags" mapstructure:"migrate_flags"`

	// RootFlags represents the flags for root command
	RootFlags RootFlagsConfig `yaml:"root_flags" mapstructure:"root_flags"`

	// DropFlags represents the flags for drop command
	DropFlags DropFlagsConfig `yaml:"drop_flags" mapstructure:"drop_flags"`

	// LogFlags represents the flags for log command
	LogFlags LogFlagsConfig `yaml:"log_flags" mapstructure:"log_flags"`

	// DatabaseConfig is map with different db connections to database to be used
	// if one or more fail
	DatabaseConfig map[string][]webutil.DatabaseSetting `yaml:"database_config" mapstructure:"database_config"`
	// contains filtered or unexported fields
}

CDBM is main struct for app and is used for all commands

func GetCDBMConfig

func GetCDBMConfig(env string) (*CDBM, error)

func NewCDBM

func NewCDBM(cfg RootFlagsConfig, driverCfg interface{}) (*CDBM, error)

NewCDBM initiates a new *CDBM instance

RootFlagsConfig#DBProtocol is a required if not read from file as it intiates what type of database we are working with

func NewTestCDBM

func NewTestCDBM(db *sqlx.DB, dbProtocol cdbmutil.DBProtocol, migCfg MigrateFlagsConfig) (*CDBM, error)

NewTestCDBM will initiate a new *CDBM instance with the bare minimum fields required to run CDBM#Migrate

This function should only be used for tests

func (*CDBM) Drop

func (cdbm *CDBM) Drop() error

Drop will drop all tables to current database

func (*CDBM) GetCurrentDBSettings

func (cdbm *CDBM) GetCurrentDBSettings() webutil.DatabaseSetting

func (*CDBM) Logs

func (cdbm *CDBM) Logs() error

Logs function will simply display log information written to log log file

func (*CDBM) Migrate

func (cdbm *CDBM) Migrate(
	getMigFunc cdbmutil.GetMigrationFunc,
	fMigFunc cdbmutil.FileMigrationFunc,
	cMigrations map[int]cdbmutil.CustomMigration,
) error

Migrate migrates database based on given settings

func (*CDBM) Status

func (cdbm *CDBM) Status() error

Status will print out to stdout the current database migration table status

Example (A)
utilSettings, err := cdbmutil.GetCDBMUtilSettings("")

if err != nil {
	fmt.Printf("%+v", err)
	return
}

settings, err := GetCDBMConfig("")

if err != nil {
	fmt.Printf("%+v", err)
	return
}

utilSettings.DBSetup.FileServerSetup = nil
utilSettings.DBSetup.BaseSchemaFile = ""

db, dbName, err := cdbmutil.GetNewDatabase(
	utilSettings,
	cdbmutil.DefaultExecCmd,
	cdbmutil.DefaultGetDB,
)

if err != nil {
	fmt.Printf("%+v", err)
	return
}

defer exec.Command("/bin/sh", "-c", fmt.Sprintf(utilSettings.DBAction.DropDB, dbName)).Start()

cdbm := &CDBM{
	DB:            db,
	DBProtocolCfg: cdbmutil.DefaultProtocolMap[cdbmutil.DBProtocol(settings.RootFlags.DBProtocol)],
}

if err = cdbm.Status(); err != nil {
	fmt.Printf("%+v", err)
	return
}
Output:

No migration entry
Example (B)
utilSettings, err := cdbmutil.GetCDBMUtilSettings("")

if err != nil {
	fmt.Printf("%+v", err)
	return
}

settings, err := GetCDBMConfig("")

if err != nil {
	fmt.Printf("%+v", err)
	return
}

utilSettings.DBSetup.FileServerSetup = nil
utilSettings.DBSetup.BaseSchemaFile = ""

db, dbName, err := cdbmutil.GetNewDatabase(
	utilSettings,
	cdbmutil.DefaultExecCmd,
	cdbmutil.DefaultGetDB,
)

if err != nil {
	fmt.Printf("%+v", err)
	return
}

defer exec.Command("/bin/sh", "-c", fmt.Sprintf(utilSettings.DBAction.DropDB, dbName)).Start()

cdbm := &CDBM{
	DB:            db,
	DBProtocolCfg: cdbmutil.DefaultProtocolMap[cdbmutil.DBProtocol(settings.RootFlags.DBProtocol)],
}

if _, err = db.Exec(
	`
		CREATE TABLE public.schema_migrations (
			version INT8 NOT NULL primary key,
			dirty boolean not null,
			dirty_state text
		);
		`,
); err != nil {
	fmt.Printf("%+v", err)
	return
}

if _, err = db.Exec(
	`
		insert into schema_migrations(version, dirty, dirty_state)
		values(2, true, 'Up');
		`,
); err != nil {
	fmt.Printf("%+v", err)
	return
}

if err = cdbm.Status(); err != nil {
	fmt.Printf("%+v", err)
	return
}
Output:

migration state - version:2 / dirty:true / dirty state:Up

type DropFlagsConfig

type DropFlagsConfig struct {
	Confirm bool `yaml:"confirm" mapstructure:"confirm"`
}

DropFlagsConfig is config settings used for CDBM#Drop function

type FlagName

type FlagName struct {
	// LongHand is long hand form of flag ie. --name
	LongHand string

	// Shoartnad is short hand form of flag ie. -n
	ShortHand string
}

FlagName is config struct to determine the long and short hand flag names

type LogFlagsConfig

type LogFlagsConfig struct {
	// LogFile should the the directory
	LogFile string `yaml:"log_file" mapstructure:"log_file"`
}

LogFlagsConfig is config struct used for logging settings for CDBM#Logs function

type MigrateFlagsConfig

type MigrateFlagsConfig struct {
	// TargetVersion should be the version you wish to set the database at
	// If set below 0 than the latest version is used by default
	TargetVersion int `yaml:"target_version" mapstructure:"target_version"`

	// RollbackOnFailure gives user ability to rollback a migration to starting state
	// if it fails on migration when set
	//
	// This only works when migrating up
	RollbackOnFailure bool `yaml:"rollback_on_failure" mapstructure:"rollback_on_failure"`

	// ResetDirtyFlag must be set if migration is in a dirty state to reset dirty state
	// or error will be returned without running anything
	//
	// This is used as a "saftey switch" to remind user that database is in dirty state
	ResetDirtyFlag bool `yaml:"reset_dirty_flag" mapstructure:"reset_dirty_flag"`

	// MigrationsProtocol is protocol used to retrieve migration files
	MigrationsProtocol cdbmutil.MigrationsProtocol `yaml:"migrations_protocol" mapstructure:"migrations_protocol"`

	// MigrationsDir is directory where migration files are stored whether locally or remotely
	MigrationsDir string `yaml:"migrations_dir" mapstructure:"migrations_dir"`
}

MigrateFlagsConfig is flag config struct for migrate command set by command line or set in code if used a library

type RootFlagsConfig

type RootFlagsConfig struct {
	DBProtocol    string `yaml:"db_protocol" mapstructure:"db_protocol"`
	EnvVar        string `yaml:"env_var" mapstructure:"env_var"`
	Database      string `yaml:"database" mapstructure:"database"`
	Host          string `yaml:"host" mapstructure:"host"`
	User          string `yaml:"user" mapstructure:"user"`
	Password      string `yaml:"password" mapstructure:"password"`
	SSLMode       string `yaml:"ssl_mode" mapstructure:"ssl_mode"`
	SSLRootCert   string `yaml:"ssl_root_cert" mapstructure:"ssl_root_cert"`
	SSLKey        string `yaml:"ssl_key" mapstructure:"ssl_key"`
	SSLCert       string `yaml:"ssl_cert" mapstructure:"ssl_cert"`
	Port          int    `yaml:"port" mapstructure:"port"`
	SSL           bool   `yaml:"ssl" mapstructure:"ssl"`
	UseFileOnFail bool   `yaml:"use_file_on_fail" mapstructure:"use_file_on_fail"`
}

type RootNameConfig

type RootNameConfig struct {
	DBProtocol    FlagName
	Env           FlagName
	Database      FlagName
	Host          FlagName
	User          FlagName
	Password      FlagName
	Port          FlagName
	SSLMode       FlagName
	SSLRootCert   FlagName
	SSLKey        FlagName
	SSLCert       FlagName
	SSL           FlagName
	UseFileOnFail FlagName
}

Jump to

Keyboard shortcuts

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