Documentation ¶
Index ¶
- Variables
- func EnsureMigrationTableExistsCh(db *sql.DB) chan bool
- func GetHelpString() string
- func GetLiveMigrationInfoCh(db *sql.DB, migrationFs fs.FS, migrationDir string) chan MigrationState
- func HandleMigratorCommand(db *sql.DB, migrationFS fs.FS, migrationDir string, args ...string) bool
- func ListAvailableMigrationsCh(migrationFs fs.FS, path string) chan []migrationFileInfo
- func MigrateDownCh(db *sql.DB, migrationFs fs.FS, migrationDir string) chan bool
- func MigrateUpCh(db *sql.DB, migrationFs fs.FS, migrationDir string) chan bool
- func SetDatabaseType(querySet *MigrationQueryDefinition)
- type MigrationQueryDefinition
- type MigrationState
- type MigrationsTable
Constants ¶
This section is empty.
Variables ¶
var MySQL = &MigrationQueryDefinition{
CheckTableExists: "SELECT EXISTS (SELECT * FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'migrations')",
CreateMigrationsTable: "CREATE TABLE migrations (version INT NOT NULL, installed_at TIMESTAMP NOT NULL)",
InsertMigration: "INSERT INTO migrations (version, installed_at) VALUES (?, ?)",
DeleteMigration: "DELETE FROM migrations WHERE version = ?",
SelectInstalledVersion: "SELECT version FROM migrations ORDER BY version DESC LIMIT 1",
}
var PostgreSQL = &MigrationQueryDefinition{
CheckTableExists: "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'migrations')",
CreateMigrationsTable: "CREATE TABLE migrations (version INT NOT NULL, installed_at TIMESTAMP NOT NULL)",
InsertMigration: "INSERT INTO migrations (version, installed_at) VALUES ($1, $2)",
DeleteMigration: "DELETE FROM migrations WHERE version = $1",
SelectInstalledVersion: "SELECT version FROM migrations ORDER BY version DESC LIMIT 1",
}
var SQLServer = &MigrationQueryDefinition{
CheckTableExists: "SELECT CASE WHEN EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'migrations') THEN 1 ELSE 0 END",
CreateMigrationsTable: "CREATE TABLE migrations (version INT NOT NULL, installed_at DATETIME NOT NULL)",
InsertMigration: "INSERT INTO migrations (version, installed_at) VALUES (@p1, @p2)",
DeleteMigration: "DELETE FROM migrations WHERE version = @p1",
SelectInstalledVersion: "SELECT TOP 1 version FROM migrations ORDER BY version DESC",
}
var SQLite = &MigrationQueryDefinition{
CheckTableExists: "SELECT EXISTS (SELECT name FROM sqlite_master WHERE type='table' AND name='migrations')",
CreateMigrationsTable: "CREATE TABLE migrations (version INT NOT NULL, installed_at TIMESTAMP NOT NULL)",
InsertMigration: "INSERT INTO migrations (version, installed_at) VALUES (?, ?)",
DeleteMigration: "DELETE FROM migrations WHERE version = ?",
SelectInstalledVersion: "SELECT version FROM migrations ORDER BY version DESC LIMIT 1",
}
Functions ¶
func EnsureMigrationTableExistsCh ¶ added in v0.2.0
func GetHelpString ¶ added in v0.1.1
func GetHelpString() string
func GetLiveMigrationInfoCh ¶
GetLiveMigrationInfoCh returns the latest migration version and the installed migration version
func HandleMigratorCommand ¶ added in v0.1.1
func HandleMigratorCommand( db *sql.DB, migrationFS fs.FS, migrationDir string, args ...string) bool
HandleMigratorCommand is intended to be hooked into main.go to display help and migrate based on args for manual migrattions. This function is optional but can be used to as part of a CLI interface.
Param: db - database connection using sqlx
Param: migrationFS - ideally embed.FS based on a `migrations` with migration files structured as described in the documentation.
Param: migrationDir - directory to use for migrations inside the FS. 'migrations' is recommended.
Param: args - os.Args[1:] from main.go
Returns: boolean indicating if a command was actionable Not indicative of success or failure.
func ListAvailableMigrationsCh ¶
ListAvailableMigrationsCh returns a slice of all migration files in the migrations directory
func MigrateDownCh ¶
MigrateDownCh migrates the database down to the previous version
func MigrateUpCh ¶
MigrateUpCh migrates the database up to the latest version Returns: channel that will be closed when the migration is complete
func SetDatabaseType ¶ added in v0.2.0
func SetDatabaseType(querySet *MigrationQueryDefinition)
SetDatabaseType sets the active query set to use for migrations. This should be called before other dbmigrator functions when used, but is optional and defaults to MySQL.
MigrationQueries describes the queries used by the migrator. You can set up your own or use one of the defaults. Usage: dbmigrator.SetDatabaseType(dbmigrator.Postgres)
Types ¶
type MigrationQueryDefinition ¶ added in v0.2.0
type MigrationQueryDefinition struct { CheckTableExists string // Expect booly result CreateMigrationsTable string InsertMigration string DeleteMigration string SelectInstalledVersion string }
MigrationQueries describes the queries used by the migrator. These can be overridden if you want to use a different DB or table name.