Documentation
¶
Overview ¶
Package dbmigrat allows for organizing database migrations across multiple locations (eg. across multiple repositories in monorepo project)
Example ¶
package main import ( "embed" "fmt" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "log" ) //go:embed fixture var exampleFixture embed.FS func main() { // resetDB only for testing purposes - you may ignore it err := th.resetDB() if err != nil { log.Fatalln(err) } db, err := sqlx.Open("postgres", "postgres://dbmigrat:dbmigrat@localhost:5432/dbmigrat?sslmode=disable") if err != nil { log.Fatalln(err) } pgStore := &PostgresStore{DB: db} err = pgStore.CreateLogTable() if err != nil { log.Fatalln(err) } authMigrations, err := ReadDir(exampleFixture, "fixture/auth") if err != nil { log.Fatalln(err) } billingMigrations, err := ReadDir(exampleFixture, "fixture/billing") if err != nil { log.Fatalln(err) } migrations := Migrations{ "auth": authMigrations, "billing": billingMigrations, } checkRes, err := CheckLogTableIntegrity(pgStore, migrations) if err != nil { log.Fatalln(err) } if checkRes.IsCorrupted { log.Fatalln(fmt.Sprintf("Db migrations are corrupted: %+v", checkRes)) } logsCount, err := Migrate(pgStore, migrations, RepoOrder{"auth", "billing"}) if err != nil { log.Fatalln(err) } fmt.Printf("[dbmigrat] applied %d migrations\n", logsCount) // Rollback migrations logsCount, err = Rollback(pgStore, migrations, RepoOrder{"billing", "auth"}, -1) if err != nil { log.Fatalln(err) } fmt.Printf("[dbmigrat] rolled back %d migrations\n", logsCount) }
Output: [dbmigrat] applied 3 migrations [dbmigrat] rolled back 3 migrations
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Migrate ¶
func Migrate(s store, migrations Migrations, repoOrder RepoOrder) (int, error)
Migrate applies migrations to the store in given repoOrder.
store is implemented by PostgresStore
migrations parameter is a map where keys are repositories names (string), values are arrays of properly ordered Migration.
repoOrder parameter is an array of repositories names (string). It determines order in which values from migrations map will be applied. eg. if migrations in repo "A" have foreign keys to repo "B" - then repoOrder should be {"B", "A"}
func Rollback ¶
func Rollback(s store, migrations Migrations, repoOrder RepoOrder, toMigrationSerial int) (int, error)
Rollback rolls back migrations applied by Migrate func
repoOrder should be reversed one passed to Migrate func
migration serial represents applied migrations (from different repos) in single run of Migrate func. When toMigrationSerial == -1, then all applied migrations will be rolled back.
Types ¶
type IntegrityCheckResult ¶
type IntegrityCheckResult struct { IsCorrupted bool RedundantRepos map[Repo]bool RedundantMigrations map[Repo][]migrationLog InvalidChecksums map[Repo][]migrationLog }
IntegrityCheckResult contains information about objects which exist in DB log but not in passed migrations to the CheckLogTableIntegrity func.
func CheckLogTableIntegrity ¶
func CheckLogTableIntegrity(s store, migrations Migrations) (*IntegrityCheckResult, error)
CheckLogTableIntegrity compares provided migrations with saved ones in migration log. It returns error when log contains migrations not present in migrations passed as argument to this func.
type Migration ¶
func ReadDir ¶
ReadDir is helper func which allows for reading migrations from directory. Directory under provided path must contain files only. Files names must follow convention: a.b.c where a is incrementing int (0,1,2,3,..), b is description, c is direction - "up" or "down". Every migration must have corresponding up and down file. Up and down file for same migration must have same description.
Examples of valid files names:
0.create_users_table.up 0.create_users_table.down.sql 1.add_username_column.up 1.add_username_column.down.sql
type Migrations ¶
type PostgresStore ¶
func (PostgresStore) CreateLogTable ¶
func (s PostgresStore) CreateLogTable() error
CreateLogTable creates table in db where applied migrations will be saved. This should be called before use of other functions from dbmigrat lib.