modulepackage
0.2.2
Repository: https://github.com/notcoffee418/dbmigrator.git
Documentation: pkg.go.dev
# README
Go Database Migrator
Simple database migration system for Go.
Usage
Expected migration file structure
- Must contain a
-- +up
comment to indicate the SQL below should run when applying a migration. - May contain a
-- +down
comment to indicate the SQL below should run when reverting a migration. - Comments behind
-- +up
and-- +down
are allowed.
-- +up <- SQL below runs when applying a migration
CREATE TABLE demo_guestbook (
id serial PRIMARY KEY,
name varchar(255) NOT NULL,
message text NOT NULL,
created_at timestamp NOT NULL DEFAULT NOW()
);
-- +down <- SQL below runs when reverting a migration
DROP TABLE demo_guestbook;
Expected project structure
Your migration files must be named in the format 0001_initial_migration.sql
where 0001
is the migration number and initial_migration
is the name of the migration.
|-- main.go
|-- migrations
| |-- 0001_initial_migration.sql
| |-- 0002_second_migration.sql
Apply and Revert Migrations
import (
"github.com/NotCoffee418/dbmigrator"
)
//go:embed all:migrations
var migrationFS embed.FS // `embed.FS` recommended but any `fs.FS` will work
func main() {
// Define the query set for your database. Defaults to MySQL.
// Must be called before any other dbmigrator functions.
dbmigrator.SetDatabaseType(dbmigrator.PostgreSQL)
// Or
//dbmigrator.SetDatabaseType(dbmigrator.MySQL)
//dbmigrator.SetDatabaseType(dbmigrator.SQLite)
//dbmigrator.SetDatabaseType(dbmigrator.SQLServer)
// Or define your own based on dbmigrator.MigrationQueryDefinition
// Directory to migrations inside the FS
migrationsDir := "migrations"
// Migrations CLI (optional)
if len(os.Args > 1) {
// help - Display this help message.
// migrate up - Apply all new database migrations.
// migrate down - Rollback a single database migration.`
dbmigrator.HandleMigratorCommand(
db *sql.DB,
migrationFS embed.FS,
migrationsDir string, // Path to migrations dir in your fs
os.Args[1:] ...string)
// Manage migrations programatically
} else {
// Apply all new migrations
doneUp := <-dbmigrator.MigrateUpCh(
db *sql.DB,
migrations embed.FS,
migrationsDir string)
// Revert Single Migration
doneDown := <-dbmigrator.MigrateDownCh(
db *sql.DB,
migrations embed.FS,
migrationsDir string)
}
}
# Functions
No description provided by the author
No description provided by the author
GetLiveMigrationInfoCh returns the latest migration version and the installed migration version.
HandleMigratorCommand is intended to be hooked into main.go to display help and migrate based on args for manual migrattions.
ListAvailableMigrationsCh returns a slice of all migration files in the migrations directory.
MigrateDownCh migrates the database down to the previous version.
MigrateUpCh migrates the database up to the latest version Returns: channel that will be closed when the migration is complete.
SetDatabaseType sets the active query set to use for migrations.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
MigrationQueries describes the queries used by the migrator.
No description provided by the author
No description provided by the author