From eb36c901a2c98b9c28f294c3e41c3621f65f9a6c Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Thu, 4 May 2023 21:46:18 +0200 Subject: [PATCH 01/20] Add --strict-order flag --- README.md | 1 + main.go | 6 ++++++ pkg/dbmate/db.go | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/README.md b/README.md index c86a48ae..544b9d77 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ The following options are available with all commands. You must use command line - `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from. - `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `DBMATE_MIGRATIONS_DIR`)_ - `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_ +- `--strict-order` - ignore out of order pending migrations. _(env: `DBMATE_STRICT_ORDER`)_ - `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_ - `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `DBMATE_NO_DUMP_SCHEMA`)_ - `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_ diff --git a/main.go b/main.go index 83b85bf8..8409896f 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,11 @@ func NewApp() *cli.App { Value: defaultDB.MigrationsTableName, Usage: "specify the database table to record migrations in", }, + &cli.BoolFlag{ + Name: "strict-order", + EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Usage: "ignore out of order pending migrations", + }, &cli.StringFlag{ Name: "schema-file", Aliases: []string{"s"}, @@ -233,6 +238,7 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { db.AutoDumpSchema = !c.Bool("no-dump-schema") db.MigrationsDir = c.StringSlice("migrations-dir") db.MigrationsTableName = c.String("migrations-table") + db.StrictOrder = c.Bool("strict-order") db.SchemaFile = c.String("schema-file") db.WaitBefore = c.Bool("wait") waitTimeout := c.Duration("wait-timeout") diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index ae598ffd..bbed8570 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -49,6 +49,8 @@ type DB struct { MigrationsTableName string // SchemaFile specifies the location for schema.sql file SchemaFile string + // Ignore out of order pending migrations + StrictOrder bool // Verbose prints the result of each statement execution Verbose bool // WaitBefore will wait for database to become available before running any actions @@ -75,6 +77,7 @@ func New(databaseURL *url.URL) *DB { MigrationsDir: []string{"./db/migrations"}, MigrationsTableName: "schema_migrations", SchemaFile: "./db/schema.sql", + StrictOrder: false, Verbose: false, WaitBefore: false, WaitInterval: time.Second, @@ -412,6 +415,13 @@ func (db *DB) FindMigrations() ([]Migration, error) { } } + var latestAppliedMigration string + for migrationVersion := range appliedMigrations { + if migrationVersion > latestAppliedMigration { + latestAppliedMigration = migrationVersion + } + } + migrations := []Migration{} for _, dir := range db.MigrationsDir { // find filesystem migrations @@ -441,6 +451,10 @@ func (db *DB) FindMigrations() ([]Migration, error) { migration.Applied = true } + if db.StrictOrder && !migration.Applied && migration.Version < latestAppliedMigration { + continue + } + migrations = append(migrations, migration) } } From 68da2883464efcc42c746f044f175a9203d32a0a Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 12 May 2023 13:07:30 +0200 Subject: [PATCH 02/20] Limit --strict-order flag to appropriate verbs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limits the ´--strict-order´ flag to the up, migrate and status verb. --- main.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 8409896f..8fd5c573 100644 --- a/main.go +++ b/main.go @@ -62,11 +62,6 @@ func NewApp() *cli.App { Value: defaultDB.MigrationsTableName, Usage: "specify the database table to record migrations in", }, - &cli.BoolFlag{ - Name: "strict-order", - EnvVars: []string{"DBMATE_STRICT_ORDER"}, - Usage: "ignore out of order pending migrations", - }, &cli.StringFlag{ Name: "schema-file", Aliases: []string{"s"}, @@ -106,6 +101,11 @@ func NewApp() *cli.App { Name: "up", Usage: "Create database (if necessary) and migrate to the latest version", Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "strict-order", + EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Usage: "ignore out of order pending migrations", + }, &cli.BoolFlag{ Name: "verbose", Aliases: []string{"v"}, @@ -114,6 +114,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { + db.StrictOrder = c.Bool("strict-order") db.Verbose = c.Bool("verbose") return db.CreateAndMigrate() }), @@ -136,6 +137,11 @@ func NewApp() *cli.App { Name: "migrate", Usage: "Migrate to the latest version", Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "strict-order", + EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Usage: "ignore out of order pending migrations", + }, &cli.BoolFlag{ Name: "verbose", Aliases: []string{"v"}, @@ -144,6 +150,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { + db.StrictOrder = c.Bool("strict-order") db.Verbose = c.Bool("verbose") return db.Migrate() }), @@ -169,6 +176,11 @@ func NewApp() *cli.App { Name: "status", Usage: "List applied and pending migrations", Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "strict-order", + EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Usage: "ignore out of order pending migrations", + }, &cli.BoolFlag{ Name: "exit-code", Usage: "return 1 if there are pending migrations", @@ -179,6 +191,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { + db.StrictOrder = c.Bool("strict-order") setExitCode := c.Bool("exit-code") quiet := c.Bool("quiet") if quiet { @@ -238,7 +251,6 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { db.AutoDumpSchema = !c.Bool("no-dump-schema") db.MigrationsDir = c.StringSlice("migrations-dir") db.MigrationsTableName = c.String("migrations-table") - db.StrictOrder = c.Bool("strict-order") db.SchemaFile = c.String("schema-file") db.WaitBefore = c.Bool("wait") waitTimeout := c.Duration("wait-timeout") From 9822a96ed140b23f5a0e78147de0f5c8cf68c3f8 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 12 May 2023 13:16:57 +0200 Subject: [PATCH 03/20] Rename --strict-flag to --strict --- README.md | 2 +- main.go | 18 +++++++++--------- pkg/dbmate/db.go | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 544b9d77..4261e241 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ The following options are available with all commands. You must use command line - `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from. - `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `DBMATE_MIGRATIONS_DIR`)_ - `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_ -- `--strict-order` - ignore out of order pending migrations. _(env: `DBMATE_STRICT_ORDER`)_ +- `--strict` - ignore out of order pending migrations. _(env: `DBMATE_STRICT`)_ - `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_ - `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `DBMATE_NO_DUMP_SCHEMA`)_ - `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_ diff --git a/main.go b/main.go index 8fd5c573..74e1031c 100644 --- a/main.go +++ b/main.go @@ -102,8 +102,8 @@ func NewApp() *cli.App { Usage: "Create database (if necessary) and migrate to the latest version", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "strict-order", - EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Name: "strict", + EnvVars: []string{"DBMATE_STRICT"}, Usage: "ignore out of order pending migrations", }, &cli.BoolFlag{ @@ -114,7 +114,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { - db.StrictOrder = c.Bool("strict-order") + db.Strict = c.Bool("strict") db.Verbose = c.Bool("verbose") return db.CreateAndMigrate() }), @@ -138,8 +138,8 @@ func NewApp() *cli.App { Usage: "Migrate to the latest version", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "strict-order", - EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Name: "strict", + EnvVars: []string{"DBMATE_STRICT"}, Usage: "ignore out of order pending migrations", }, &cli.BoolFlag{ @@ -150,7 +150,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { - db.StrictOrder = c.Bool("strict-order") + db.Strict = c.Bool("strict") db.Verbose = c.Bool("verbose") return db.Migrate() }), @@ -177,8 +177,8 @@ func NewApp() *cli.App { Usage: "List applied and pending migrations", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "strict-order", - EnvVars: []string{"DBMATE_STRICT_ORDER"}, + Name: "strict", + EnvVars: []string{"DBMATE_STRICT"}, Usage: "ignore out of order pending migrations", }, &cli.BoolFlag{ @@ -191,7 +191,7 @@ func NewApp() *cli.App { }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { - db.StrictOrder = c.Bool("strict-order") + db.Strict = c.Bool("strict") setExitCode := c.Bool("exit-code") quiet := c.Bool("quiet") if quiet { diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index bbed8570..fe560540 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -50,7 +50,7 @@ type DB struct { // SchemaFile specifies the location for schema.sql file SchemaFile string // Ignore out of order pending migrations - StrictOrder bool + Strict bool // Verbose prints the result of each statement execution Verbose bool // WaitBefore will wait for database to become available before running any actions @@ -77,7 +77,7 @@ func New(databaseURL *url.URL) *DB { MigrationsDir: []string{"./db/migrations"}, MigrationsTableName: "schema_migrations", SchemaFile: "./db/schema.sql", - StrictOrder: false, + Strict: false, Verbose: false, WaitBefore: false, WaitInterval: time.Second, @@ -451,7 +451,7 @@ func (db *DB) FindMigrations() ([]Migration, error) { migration.Applied = true } - if db.StrictOrder && !migration.Applied && migration.Version < latestAppliedMigration { + if db.Strict && !migration.Applied && migration.Version < latestAppliedMigration { continue } From d278d6a2f98b7739132ed1c06aa7b507f663c783 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Sat, 29 Jul 2023 16:58:15 +0200 Subject: [PATCH 04/20] Throw error on strict numerical order conflict --- README.md | 2 +- main.go | 9 ++------- pkg/dbmate/db.go | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4261e241..5a69c7c6 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,9 @@ The following options are available with all commands. You must use command line - `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from. - `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `DBMATE_MIGRATIONS_DIR`)_ - `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_ -- `--strict` - ignore out of order pending migrations. _(env: `DBMATE_STRICT`)_ - `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_ - `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `DBMATE_NO_DUMP_SCHEMA`)_ +- `--strict` - apply migrations strictly in numerical order. _(env: `DBMATE_STRICT`)_ - `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_ - `--wait-timeout 60s` - timeout for --wait flag _(env: `DBMATE_WAIT_TIMEOUT`)_ diff --git a/main.go b/main.go index 74e1031c..61b4dfc2 100644 --- a/main.go +++ b/main.go @@ -104,7 +104,7 @@ func NewApp() *cli.App { &cli.BoolFlag{ Name: "strict", EnvVars: []string{"DBMATE_STRICT"}, - Usage: "ignore out of order pending migrations", + Usage: "apply migrations strictly in numerical order", }, &cli.BoolFlag{ Name: "verbose", @@ -140,7 +140,7 @@ func NewApp() *cli.App { &cli.BoolFlag{ Name: "strict", EnvVars: []string{"DBMATE_STRICT"}, - Usage: "ignore out of order pending migrations", + Usage: "apply migrations strictly in numerical order", }, &cli.BoolFlag{ Name: "verbose", @@ -176,11 +176,6 @@ func NewApp() *cli.App { Name: "status", Usage: "List applied and pending migrations", Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "strict", - EnvVars: []string{"DBMATE_STRICT"}, - Usage: "ignore out of order pending migrations", - }, &cli.BoolFlag{ Name: "exit-code", Usage: "return 1 if there are pending migrations", diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index fe560540..e385b387 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -452,7 +452,7 @@ func (db *DB) FindMigrations() ([]Migration, error) { } if db.Strict && !migration.Applied && migration.Version < latestAppliedMigration { - continue + return nil, fmt.Errorf("Cannot apply migration `%s` after `%s` in --strict mode. Migrations have to be in numerical order.", migration.Version, latestAppliedMigration) } migrations = append(migrations, migration) From dc0e0a18c74c8402ce23374881b0d3f89a192ac2 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Sat, 29 Jul 2023 16:59:21 +0200 Subject: [PATCH 05/20] Identify latest applied migration only in strict mode --- pkg/dbmate/db.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index e385b387..40ff2997 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -416,9 +416,11 @@ func (db *DB) FindMigrations() ([]Migration, error) { } var latestAppliedMigration string - for migrationVersion := range appliedMigrations { - if migrationVersion > latestAppliedMigration { - latestAppliedMigration = migrationVersion + if db.Strict { + for migrationVersion := range appliedMigrations { + if migrationVersion > latestAppliedMigration { + latestAppliedMigration = migrationVersion + } } } From f912ddfb74c8154922351fa57ad35ced4f789742 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Sat, 29 Jul 2023 17:17:28 +0200 Subject: [PATCH 06/20] Change wording of strict mode error message --- pkg/dbmate/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 40ff2997..610fb8a4 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -454,7 +454,7 @@ func (db *DB) FindMigrations() ([]Migration, error) { } if db.Strict && !migration.Applied && migration.Version < latestAppliedMigration { - return nil, fmt.Errorf("Cannot apply migration `%s` after `%s` in --strict mode. Migrations have to be in numerical order.", migration.Version, latestAppliedMigration) + return nil, fmt.Errorf("cannot apply migration `%s` after `%s` in --strict mode, migrations have to be in numerical order", migration.Version, latestAppliedMigration) } migrations = append(migrations, migration) From 5b890ffad741b0831dacc7e9d7767e74f1f1236e Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Thu, 3 Aug 2023 15:58:29 -0700 Subject: [PATCH 07/20] Update usage text --- README.md | 2 +- main.go | 4 ++-- pkg/dbmate/db.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5a69c7c6..162d2b46 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ The following options are available with all commands. You must use command line - `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_ - `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_ - `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `DBMATE_NO_DUMP_SCHEMA`)_ -- `--strict` - apply migrations strictly in numerical order. _(env: `DBMATE_STRICT`)_ +- `--strict` - fail if migrations would be applied out of order _(env: `DBMATE_STRICT`)_ - `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_ - `--wait-timeout 60s` - timeout for --wait flag _(env: `DBMATE_WAIT_TIMEOUT`)_ diff --git a/main.go b/main.go index 61b4dfc2..c32d1891 100644 --- a/main.go +++ b/main.go @@ -104,7 +104,7 @@ func NewApp() *cli.App { &cli.BoolFlag{ Name: "strict", EnvVars: []string{"DBMATE_STRICT"}, - Usage: "apply migrations strictly in numerical order", + Usage: "fail if migrations would be applied out of order", }, &cli.BoolFlag{ Name: "verbose", @@ -140,7 +140,7 @@ func NewApp() *cli.App { &cli.BoolFlag{ Name: "strict", EnvVars: []string{"DBMATE_STRICT"}, - Usage: "apply migrations strictly in numerical order", + Usage: "fail if migrations would be applied out of order", }, &cli.BoolFlag{ Name: "verbose", diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 610fb8a4..9f3bf6e4 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -49,7 +49,7 @@ type DB struct { MigrationsTableName string // SchemaFile specifies the location for schema.sql file SchemaFile string - // Ignore out of order pending migrations + // Fail if migrations would be applied out of order Strict bool // Verbose prints the result of each statement execution Verbose bool From d9d7ce2403255f8d2d3b63802215ebeda4fa57e2 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 10:12:01 +0200 Subject: [PATCH 08/20] Check pending migrations version in `Migrate()` instead of `FindMigrations()` --- pkg/dbmate/db.go | 82 ++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 9f3bf6e4..3af5bc10 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -308,6 +308,24 @@ func (db *DB) Migrate() error { return err } + if db.Strict { + appliedMigrations, err := db.FindAppliedMigrations(1) + if err != nil { + return err + } + + var latestAppliedMigration string + for migrationVersion := range appliedMigrations { + latestAppliedMigration = migrationVersion + } + + for _, migration := range migrations { + if db.Strict && !migration.Applied && migration.Version <= latestAppliedMigration { + return fmt.Errorf("cannot apply migration `%s` after `%s` in --strict mode, migrations have to be in numerical order", migration.Version, latestAppliedMigration) + } + } + } + if len(migrations) == 0 { return ErrNoMigrationFiles } @@ -390,40 +408,11 @@ func (db *DB) readMigrationsDir(dir string) ([]fs.DirEntry, error) { // FindMigrations lists all available migrations func (db *DB) FindMigrations() ([]Migration, error) { - drv, err := db.Driver() - if err != nil { - return nil, err - } - - sqlDB, err := drv.Open() - if err != nil { - return nil, err - } - defer dbutil.MustClose(sqlDB) - - // find applied migrations - appliedMigrations := map[string]bool{} - migrationsTableExists, err := drv.MigrationsTableExists(sqlDB) + appliedMigrations, err := db.FindAppliedMigrations(-1) if err != nil { return nil, err } - if migrationsTableExists { - appliedMigrations, err = drv.SelectMigrations(sqlDB, -1) - if err != nil { - return nil, err - } - } - - var latestAppliedMigration string - if db.Strict { - for migrationVersion := range appliedMigrations { - if migrationVersion > latestAppliedMigration { - latestAppliedMigration = migrationVersion - } - } - } - migrations := []Migration{} for _, dir := range db.MigrationsDir { // find filesystem migrations @@ -453,10 +442,6 @@ func (db *DB) FindMigrations() ([]Migration, error) { migration.Applied = true } - if db.Strict && !migration.Applied && migration.Version < latestAppliedMigration { - return nil, fmt.Errorf("cannot apply migration `%s` after `%s` in --strict mode, migrations have to be in numerical order", migration.Version, latestAppliedMigration) - } - migrations = append(migrations, migration) } } @@ -468,6 +453,35 @@ func (db *DB) FindMigrations() ([]Migration, error) { return migrations, nil } +// FindAppliedMigrations lists applied migrations +func (db *DB) FindAppliedMigrations(limit int) (map[string]bool, error) { + drv, err := db.Driver() + if err != nil { + return nil, err + } + + sqlDB, err := drv.Open() + if err != nil { + return nil, err + } + defer dbutil.MustClose(sqlDB) + + appliedMigrations := map[string]bool{} + migrationsTableExists, err := drv.MigrationsTableExists(sqlDB) + if err != nil { + return nil, err + } + + if migrationsTableExists { + appliedMigrations, err = drv.SelectMigrations(sqlDB, limit) + if err != nil { + return nil, err + } + } + + return appliedMigrations, nil +} + // Rollback rolls back the most recent migration func (db *DB) Rollback() error { drv, err := db.Driver() From b9e0bdfcbedbbda8a9ad878ba9ae61ebe9761e6f Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 10:28:49 +0200 Subject: [PATCH 09/20] Change wording of strict mode error message --- pkg/dbmate/db.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 3af5bc10..775d9208 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -314,14 +314,14 @@ func (db *DB) Migrate() error { return err } - var latestAppliedMigration string + var highestAppliedMigrationVersion string for migrationVersion := range appliedMigrations { - latestAppliedMigration = migrationVersion + highestAppliedMigrationVersion = migrationVersion } for _, migration := range migrations { - if db.Strict && !migration.Applied && migration.Version <= latestAppliedMigration { - return fmt.Errorf("cannot apply migration `%s` after `%s` in --strict mode, migrations have to be in numerical order", migration.Version, latestAppliedMigration) + if db.Strict && !migration.Applied && migration.Version <= highestAppliedMigrationVersion { + return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", migration.Version, highestAppliedMigrationVersion) } } } From d0ded5d0099eb988c7c552aeb0dfe0c75c65a613 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 10:57:16 +0200 Subject: [PATCH 10/20] Check only pending migrations in `Migrate()` --- pkg/dbmate/db.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 775d9208..68dc252c 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -308,6 +308,15 @@ func (db *DB) Migrate() error { return err } + pendingMigrations := []Migration{} + for _, migration := range migrations { + if migration.Applied { + continue + } + + pendingMigrations = append(pendingMigrations, migration) + } + if db.Strict { appliedMigrations, err := db.FindAppliedMigrations(1) if err != nil { @@ -319,8 +328,8 @@ func (db *DB) Migrate() error { highestAppliedMigrationVersion = migrationVersion } - for _, migration := range migrations { - if db.Strict && !migration.Applied && migration.Version <= highestAppliedMigrationVersion { + for _, migration := range pendingMigrations { + if db.Strict && migration.Version <= highestAppliedMigrationVersion { return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", migration.Version, highestAppliedMigrationVersion) } } @@ -336,11 +345,7 @@ func (db *DB) Migrate() error { } defer dbutil.MustClose(sqlDB) - for _, migration := range migrations { - if migration.Applied { - continue - } - + for _, migration := range pendingMigrations { fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName) parsed, err := migration.Parse() From 311dc19627f10f5cf744f54763127dce4e645b96 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 11:01:40 +0200 Subject: [PATCH 11/20] Check only first pending migration FindMigrations returns a sorted list so we only need to check the first pending migration. --- pkg/dbmate/db.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 68dc252c..54cf5bc5 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -328,10 +328,8 @@ func (db *DB) Migrate() error { highestAppliedMigrationVersion = migrationVersion } - for _, migration := range pendingMigrations { - if db.Strict && migration.Version <= highestAppliedMigrationVersion { - return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", migration.Version, highestAppliedMigrationVersion) - } + if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { + return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) } } From d20cf09fc8a809752572f3c6558ccb418ffcffdd Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 11:08:15 +0200 Subject: [PATCH 12/20] Abort migration if no pending migrations files exist --- pkg/dbmate/db.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 54cf5bc5..a0be5f38 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -18,16 +18,17 @@ import ( // Error codes var ( - ErrNoMigrationFiles = errors.New("no migration files found") - ErrInvalidURL = errors.New("invalid url, have you set your --url flag or DATABASE_URL environment variable?") - ErrNoRollback = errors.New("can't rollback: no migrations have been applied") - ErrCantConnect = errors.New("unable to connect to database") - ErrUnsupportedDriver = errors.New("unsupported driver") - ErrNoMigrationName = errors.New("please specify a name for the new migration") - ErrMigrationAlreadyExist = errors.New("file already exists") - ErrMigrationDirNotFound = errors.New("could not find migrations directory") - ErrMigrationNotFound = errors.New("can't find migration file") - ErrCreateDirectory = errors.New("unable to create directory") + ErrNoMigrationFiles = errors.New("no migration files found") + ErrNoPendingMigrationFiles = errors.New("no pending migration files found") + ErrInvalidURL = errors.New("invalid url, have you set your --url flag or DATABASE_URL environment variable?") + ErrNoRollback = errors.New("can't rollback: no migrations have been applied") + ErrCantConnect = errors.New("unable to connect to database") + ErrUnsupportedDriver = errors.New("unsupported driver") + ErrNoMigrationName = errors.New("please specify a name for the new migration") + ErrMigrationAlreadyExist = errors.New("file already exists") + ErrMigrationDirNotFound = errors.New("could not find migrations directory") + ErrMigrationNotFound = errors.New("can't find migration file") + ErrCreateDirectory = errors.New("unable to create directory") ) // migrationFileRegexp pattern for valid migration files @@ -308,6 +309,10 @@ func (db *DB) Migrate() error { return err } + if len(migrations) == 0 { + return ErrNoMigrationFiles + } + pendingMigrations := []Migration{} for _, migration := range migrations { if migration.Applied { @@ -317,6 +322,10 @@ func (db *DB) Migrate() error { pendingMigrations = append(pendingMigrations, migration) } + if len(pendingMigrations) == 0 { + return ErrNoPendingMigrationFiles + } + if db.Strict { appliedMigrations, err := db.FindAppliedMigrations(1) if err != nil { @@ -333,10 +342,6 @@ func (db *DB) Migrate() error { } } - if len(migrations) == 0 { - return ErrNoMigrationFiles - } - sqlDB, err := db.openDatabaseForMigration(drv) if err != nil { return err From cd889060c3554c05e070e1187bdb15b96c8f8e52 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 11:47:50 +0200 Subject: [PATCH 13/20] Abort migration with no error if no pending migrations files exist A return error is not in line with tests. --- pkg/dbmate/db.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index a0be5f38..dc7017f6 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -18,17 +18,16 @@ import ( // Error codes var ( - ErrNoMigrationFiles = errors.New("no migration files found") - ErrNoPendingMigrationFiles = errors.New("no pending migration files found") - ErrInvalidURL = errors.New("invalid url, have you set your --url flag or DATABASE_URL environment variable?") - ErrNoRollback = errors.New("can't rollback: no migrations have been applied") - ErrCantConnect = errors.New("unable to connect to database") - ErrUnsupportedDriver = errors.New("unsupported driver") - ErrNoMigrationName = errors.New("please specify a name for the new migration") - ErrMigrationAlreadyExist = errors.New("file already exists") - ErrMigrationDirNotFound = errors.New("could not find migrations directory") - ErrMigrationNotFound = errors.New("can't find migration file") - ErrCreateDirectory = errors.New("unable to create directory") + ErrNoMigrationFiles = errors.New("no migration files found") + ErrInvalidURL = errors.New("invalid url, have you set your --url flag or DATABASE_URL environment variable?") + ErrNoRollback = errors.New("can't rollback: no migrations have been applied") + ErrCantConnect = errors.New("unable to connect to database") + ErrUnsupportedDriver = errors.New("unsupported driver") + ErrNoMigrationName = errors.New("please specify a name for the new migration") + ErrMigrationAlreadyExist = errors.New("file already exists") + ErrMigrationDirNotFound = errors.New("could not find migrations directory") + ErrMigrationNotFound = errors.New("can't find migration file") + ErrCreateDirectory = errors.New("unable to create directory") ) // migrationFileRegexp pattern for valid migration files @@ -323,7 +322,7 @@ func (db *DB) Migrate() error { } if len(pendingMigrations) == 0 { - return ErrNoPendingMigrationFiles + return nil } if db.Strict { From 3323fa530d399abb870514a83167108cea5e2f02 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 12:13:07 +0200 Subject: [PATCH 14/20] Derive highest applied version number from migration list --- pkg/dbmate/db.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index dc7017f6..583134d2 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -312,33 +312,24 @@ func (db *DB) Migrate() error { return ErrNoMigrationFiles } + highestAppliedMigrationVersion := "" pendingMigrations := []Migration{} for _, migration := range migrations { if migration.Applied { - continue + if db.Strict && highestAppliedMigrationVersion <= migration.Version { + highestAppliedMigrationVersion = migration.Version + } + } else { + pendingMigrations = append(pendingMigrations, migration) } - - pendingMigrations = append(pendingMigrations, migration) } if len(pendingMigrations) == 0 { return nil } - if db.Strict { - appliedMigrations, err := db.FindAppliedMigrations(1) - if err != nil { - return err - } - - var highestAppliedMigrationVersion string - for migrationVersion := range appliedMigrations { - highestAppliedMigrationVersion = migrationVersion - } - - if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { - return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) - } + if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { + return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) } sqlDB, err := db.openDatabaseForMigration(drv) From d896ce2413bfbb0ab3970dd6a735f4e040b3e5a5 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 11 Aug 2023 12:17:59 +0200 Subject: [PATCH 15/20] Undo refactor to find applied migrations --- pkg/dbmate/db.go | 51 ++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 583134d2..732fcfd4 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -406,11 +406,31 @@ func (db *DB) readMigrationsDir(dir string) ([]fs.DirEntry, error) { // FindMigrations lists all available migrations func (db *DB) FindMigrations() ([]Migration, error) { - appliedMigrations, err := db.FindAppliedMigrations(-1) + drv, err := db.Driver() + if err != nil { + return nil, err + } + + sqlDB, err := drv.Open() + if err != nil { + return nil, err + } + defer dbutil.MustClose(sqlDB) + + // find applied migrations + appliedMigrations := map[string]bool{} + migrationsTableExists, err := drv.MigrationsTableExists(sqlDB) if err != nil { return nil, err } + if migrationsTableExists { + appliedMigrations, err = drv.SelectMigrations(sqlDB, -1) + if err != nil { + return nil, err + } + } + migrations := []Migration{} for _, dir := range db.MigrationsDir { // find filesystem migrations @@ -451,35 +471,6 @@ func (db *DB) FindMigrations() ([]Migration, error) { return migrations, nil } -// FindAppliedMigrations lists applied migrations -func (db *DB) FindAppliedMigrations(limit int) (map[string]bool, error) { - drv, err := db.Driver() - if err != nil { - return nil, err - } - - sqlDB, err := drv.Open() - if err != nil { - return nil, err - } - defer dbutil.MustClose(sqlDB) - - appliedMigrations := map[string]bool{} - migrationsTableExists, err := drv.MigrationsTableExists(sqlDB) - if err != nil { - return nil, err - } - - if migrationsTableExists { - appliedMigrations, err = drv.SelectMigrations(sqlDB, limit) - if err != nil { - return nil, err - } - } - - return appliedMigrations, nil -} - // Rollback rolls back the most recent migration func (db *DB) Rollback() error { drv, err := db.Driver() From 4b85b8927be873c48a8a5b26e8905279e3f42a68 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Tue, 7 Nov 2023 07:18:23 +0100 Subject: [PATCH 16/20] Dump schema even if no migrations are applied --- pkg/dbmate/db.go | 66 +++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index 732fcfd4..a4bcad4a 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -324,51 +324,49 @@ func (db *DB) Migrate() error { } } - if len(pendingMigrations) == 0 { - return nil - } - - if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { - return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) - } - - sqlDB, err := db.openDatabaseForMigration(drv) - if err != nil { - return err - } - defer dbutil.MustClose(sqlDB) - - for _, migration := range pendingMigrations { - fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName) + if len(pendingMigrations) > 0 { + if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { + return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) + } - parsed, err := migration.Parse() + sqlDB, err := db.openDatabaseForMigration(drv) if err != nil { return err } + defer dbutil.MustClose(sqlDB) - execMigration := func(tx dbutil.Transaction) error { - // run actual migration - result, err := tx.Exec(parsed.Up) + for _, migration := range pendingMigrations { + fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName) + + parsed, err := migration.Parse() if err != nil { return err - } else if db.Verbose { - db.printVerbose(result) } - // record migration - return drv.InsertMigration(tx, migration.Version) - } + execMigration := func(tx dbutil.Transaction) error { + // run actual migration + result, err := tx.Exec(parsed.Up) + if err != nil { + return err + } else if db.Verbose { + db.printVerbose(result) + } + + // record migration + return drv.InsertMigration(tx, migration.Version) + } - if parsed.UpOptions.Transaction() { - // begin transaction - err = doTransaction(sqlDB, execMigration) - } else { - // run outside of transaction - err = execMigration(sqlDB) - } + if parsed.UpOptions.Transaction() { + // begin transaction + err = doTransaction(sqlDB, execMigration) + } else { + // run outside of transaction + err = execMigration(sqlDB) + } - if err != nil { - return err + if err != nil { + return err + } } } From 5d951433d82ce367937c9d45e1140fe377af04c7 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Wed, 8 Nov 2023 10:38:39 +0100 Subject: [PATCH 17/20] Fix backwards-incompatible breaking condition --- pkg/dbmate/db.go | 62 +++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/pkg/dbmate/db.go b/pkg/dbmate/db.go index a4bcad4a..9ebdf324 100644 --- a/pkg/dbmate/db.go +++ b/pkg/dbmate/db.go @@ -324,49 +324,47 @@ func (db *DB) Migrate() error { } } - if len(pendingMigrations) > 0 { - if db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { - return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) - } + if len(pendingMigrations) > 0 && db.Strict && pendingMigrations[0].Version <= highestAppliedMigrationVersion { + return fmt.Errorf("migration `%s` is out of order with already applied migrations, the version number has to be higher than the applied migration `%s` in --strict mode", pendingMigrations[0].Version, highestAppliedMigrationVersion) + } + + sqlDB, err := db.openDatabaseForMigration(drv) + if err != nil { + return err + } + defer dbutil.MustClose(sqlDB) - sqlDB, err := db.openDatabaseForMigration(drv) + for _, migration := range pendingMigrations { + fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName) + + parsed, err := migration.Parse() if err != nil { return err } - defer dbutil.MustClose(sqlDB) - - for _, migration := range pendingMigrations { - fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName) - parsed, err := migration.Parse() + execMigration := func(tx dbutil.Transaction) error { + // run actual migration + result, err := tx.Exec(parsed.Up) if err != nil { return err + } else if db.Verbose { + db.printVerbose(result) } - execMigration := func(tx dbutil.Transaction) error { - // run actual migration - result, err := tx.Exec(parsed.Up) - if err != nil { - return err - } else if db.Verbose { - db.printVerbose(result) - } - - // record migration - return drv.InsertMigration(tx, migration.Version) - } + // record migration + return drv.InsertMigration(tx, migration.Version) + } - if parsed.UpOptions.Transaction() { - // begin transaction - err = doTransaction(sqlDB, execMigration) - } else { - // run outside of transaction - err = execMigration(sqlDB) - } + if parsed.UpOptions.Transaction() { + // begin transaction + err = doTransaction(sqlDB, execMigration) + } else { + // run outside of transaction + err = execMigration(sqlDB) + } - if err != nil { - return err - } + if err != nil { + return err } } From c216c93bb788b4561f5cd6cf275bd8de8f9bfb10 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 10 Nov 2023 13:39:05 +0100 Subject: [PATCH 18/20] Add tests for strict mode --- pkg/dbmate/db_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/pkg/dbmate/db_test.go b/pkg/dbmate/db_test.go index 6ba8d5bb..8b89e2bb 100644 --- a/pkg/dbmate/db_test.go +++ b/pkg/dbmate/db_test.go @@ -583,3 +583,75 @@ func TestFindMigrationsFSMultipleDirs(t *testing.T) { require.Equal(t, "db/migrations_a/005_test_migration_a.sql", actual[4].FilePath) require.Equal(t, "db/migrations_c/006_test_migration_c.sql", actual[5].FilePath) } + +func TestMigrateUnrestrictedOrder(t *testing.T) { + emptyMigration := []byte("-- migrate:up\n-- migrate:down") + + // initialize database + u := dbutil.MustParseURL(os.Getenv("POSTGRES_TEST_URL")) + db := newTestDB(t, u) + + db.Drop() + db.Create() + + // test to apply new migrations on empty database + db.FS = fstest.MapFS{ + "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, + "db/migrations/100_test_migration_b.sql": { Data: emptyMigration }, + } + + err := db.Migrate() + require.NoError(t, err) + + // test to apply an out of order migration + db.FS = fstest.MapFS{ + "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, + "db/migrations/100_test_migration_b.sql": { Data: emptyMigration }, + "db/migrations/010_test_migration_c.sql": { Data: emptyMigration }, + } + + err = db.Migrate() + require.NoError(t, err) +} + +func TestMigrateStrictOrder(t *testing.T) { + emptyMigration := []byte("-- migrate:up\n-- migrate:down") + + // initialize database + u := dbutil.MustParseURL(os.Getenv("POSTGRES_TEST_URL")) + db := newTestDB(t, u) + db.Strict = true + + db.Drop() + db.Create() + + // test to apply new migrations on empty database + db.FS = fstest.MapFS{ + "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, + "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, + } + + err := db.Migrate() + require.NoError(t, err) + + // test to apply an in order migration + db.FS = fstest.MapFS{ + "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, + "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, + "db/migrations/100_test_migration_c.sql": { Data: emptyMigration }, + } + + err = db.Migrate() + require.NoError(t, err) + + // test to apply an out of order migration + db.FS = fstest.MapFS{ + "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, + "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, + "db/migrations/100_test_migration_c.sql": { Data: emptyMigration }, + "db/migrations/050_test_migration_d.sql": { Data: emptyMigration }, + } + + err = db.Migrate() + require.Error(t, err) +} From 9949e88e50bd055f97eb0aed439fb0a6150e1de1 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 10 Nov 2023 13:49:23 +0100 Subject: [PATCH 19/20] Fix tests --- pkg/dbmate/db_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/dbmate/db_test.go b/pkg/dbmate/db_test.go index 8b89e2bb..4f8ebfd2 100644 --- a/pkg/dbmate/db_test.go +++ b/pkg/dbmate/db_test.go @@ -591,8 +591,10 @@ func TestMigrateUnrestrictedOrder(t *testing.T) { u := dbutil.MustParseURL(os.Getenv("POSTGRES_TEST_URL")) db := newTestDB(t, u) - db.Drop() - db.Create() + err := db.Drop() + require.NoError(t, err) + err = db.Create() + require.NoError(t, err) // test to apply new migrations on empty database db.FS = fstest.MapFS{ @@ -600,7 +602,7 @@ func TestMigrateUnrestrictedOrder(t *testing.T) { "db/migrations/100_test_migration_b.sql": { Data: emptyMigration }, } - err := db.Migrate() + err = db.Migrate() require.NoError(t, err) // test to apply an out of order migration @@ -622,8 +624,10 @@ func TestMigrateStrictOrder(t *testing.T) { db := newTestDB(t, u) db.Strict = true - db.Drop() - db.Create() + err := db.Drop() + require.NoError(t, err) + err = db.Create() + require.NoError(t, err) // test to apply new migrations on empty database db.FS = fstest.MapFS{ @@ -631,7 +635,7 @@ func TestMigrateStrictOrder(t *testing.T) { "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, } - err := db.Migrate() + err = db.Migrate() require.NoError(t, err) // test to apply an in order migration From 02d2d7be4ab4ee6e3579bf22a24dedb2dfd36e11 Mon Sep 17 00:00:00 2001 From: Philip Hartmann Date: Fri, 10 Nov 2023 14:16:13 +0100 Subject: [PATCH 20/20] Remove whitespaces --- pkg/dbmate/db_test.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/dbmate/db_test.go b/pkg/dbmate/db_test.go index 4f8ebfd2..9205a795 100644 --- a/pkg/dbmate/db_test.go +++ b/pkg/dbmate/db_test.go @@ -598,8 +598,8 @@ func TestMigrateUnrestrictedOrder(t *testing.T) { // test to apply new migrations on empty database db.FS = fstest.MapFS{ - "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, - "db/migrations/100_test_migration_b.sql": { Data: emptyMigration }, + "db/migrations/001_test_migration_a.sql": {Data: emptyMigration}, + "db/migrations/100_test_migration_b.sql": {Data: emptyMigration}, } err = db.Migrate() @@ -607,9 +607,9 @@ func TestMigrateUnrestrictedOrder(t *testing.T) { // test to apply an out of order migration db.FS = fstest.MapFS{ - "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, - "db/migrations/100_test_migration_b.sql": { Data: emptyMigration }, - "db/migrations/010_test_migration_c.sql": { Data: emptyMigration }, + "db/migrations/001_test_migration_a.sql": {Data: emptyMigration}, + "db/migrations/100_test_migration_b.sql": {Data: emptyMigration}, + "db/migrations/010_test_migration_c.sql": {Data: emptyMigration}, } err = db.Migrate() @@ -631,8 +631,8 @@ func TestMigrateStrictOrder(t *testing.T) { // test to apply new migrations on empty database db.FS = fstest.MapFS{ - "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, - "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, + "db/migrations/001_test_migration_a.sql": {Data: emptyMigration}, + "db/migrations/010_test_migration_b.sql": {Data: emptyMigration}, } err = db.Migrate() @@ -640,9 +640,9 @@ func TestMigrateStrictOrder(t *testing.T) { // test to apply an in order migration db.FS = fstest.MapFS{ - "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, - "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, - "db/migrations/100_test_migration_c.sql": { Data: emptyMigration }, + "db/migrations/001_test_migration_a.sql": {Data: emptyMigration}, + "db/migrations/010_test_migration_b.sql": {Data: emptyMigration}, + "db/migrations/100_test_migration_c.sql": {Data: emptyMigration}, } err = db.Migrate() @@ -650,10 +650,10 @@ func TestMigrateStrictOrder(t *testing.T) { // test to apply an out of order migration db.FS = fstest.MapFS{ - "db/migrations/001_test_migration_a.sql": { Data: emptyMigration }, - "db/migrations/010_test_migration_b.sql": { Data: emptyMigration }, - "db/migrations/100_test_migration_c.sql": { Data: emptyMigration }, - "db/migrations/050_test_migration_d.sql": { Data: emptyMigration }, + "db/migrations/001_test_migration_a.sql": {Data: emptyMigration}, + "db/migrations/010_test_migration_b.sql": {Data: emptyMigration}, + "db/migrations/100_test_migration_c.sql": {Data: emptyMigration}, + "db/migrations/050_test_migration_d.sql": {Data: emptyMigration}, } err = db.Migrate()