From db7de4b31b1836abed822077840665639f0d66da Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 19 Jan 2024 23:27:27 -0500 Subject: Add ability to use sequential or timestamp-based versions --- internal/commands/new.go | 5 +-- internal/configloader/configloader.go | 47 +++++++++++++++++++++---- internal/models/migration.go | 38 ++++++++------------ internal/models/migration_test.go | 25 ++++++------- internal/repositories/migration_db_repo_test.go | 10 +++--- internal/repositories/migration_fs_repo.go | 46 +++++++++++++++++------- internal/repositories/migration_fs_repo_test.go | 8 ++--- internal/services/migration.go | 24 ++++++++++++- 8 files changed, 135 insertions(+), 68 deletions(-) (limited to 'internal') diff --git a/internal/commands/new.go b/internal/commands/new.go index a4eeff4..0c44248 100644 --- a/internal/commands/new.go +++ b/internal/commands/new.go @@ -3,10 +3,8 @@ package commands import ( "context" "flag" - "time" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/output" "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" @@ -70,8 +68,7 @@ func (cmd *NewCmd) Execute( return subcommands.ExitFailure } - migration := models.NewMigration(time.Now(), cmd.message) - err = migrationService.CreateMigration(migration) + err = migrationService.CreateMigration(cfg.Migrations.VersionStyle, cmd.message) if err != nil { consoleOutputter.Error(err.Error()) return subcommands.ExitFailure diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index c50ec24..c5aba2d 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -10,6 +10,7 @@ package configloader import ( "errors" + "fmt" "os" "path/filepath" "runtime" @@ -18,8 +19,21 @@ import ( "github.com/kelseyhightower/envconfig" ) -var errConfigFileNotFound = errors.New( - "bolt configuration file not found in current directory or any parent directories", +type VersionStyle string + +const ( + VersionStyleSequential VersionStyle = "sequential" + VersionStyleTimestamp VersionStyle = "timestamp" +) + +var ( + ErrConfigFileNotFound = errors.New( + "bolt configuration file not found in current directory or any parent directories", + ) + ErrInvalidVersionStyle = fmt.Errorf( + "invalid version style for bolt migrations. supported styles: %v", + []VersionStyle{VersionStyleSequential, VersionStyleTimestamp}, + ) ) // Config represents the application configuration settings. @@ -35,7 +49,16 @@ type Config struct { } type MigrationsConfig struct { - DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` + DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` + VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` +} + +func (mc *MigrationsConfig) Validate() error { + if mc.VersionStyle != VersionStyleSequential && mc.VersionStyle != VersionStyleTimestamp { + return ErrInvalidVersionStyle + } + + return nil } type ConnectionConfig struct { @@ -49,12 +72,17 @@ type ConnectionConfig struct { func NewConfig() (*Config, error) { filePath, err := findConfigFilePath() - if err != nil && !errors.Is(err, errConfigFileNotFound) { + if err != nil && !errors.Is(err, ErrConfigFileNotFound) { return nil, err } - cfg := Config{Migrations: MigrationsConfig{DirectoryPath: "migrations"}} - if !errors.Is(err, errConfigFileNotFound) { + cfg := Config{ + Migrations: MigrationsConfig{ + DirectoryPath: "migrations", + VersionStyle: VersionStyleSequential, + }, + } + if !errors.Is(err, ErrConfigFileNotFound) { _, err = toml.DecodeFile(filePath, &cfg) if err != nil { return nil, err @@ -66,6 +94,11 @@ func NewConfig() (*Config, error) { return nil, err } + err = cfg.Migrations.Validate() + if err != nil { + return nil, err + } + return &cfg, nil } @@ -91,7 +124,7 @@ func findConfigFilePath() (filePath string, err error) { } if dir == rootDir { - return "", errConfigFileNotFound + return "", ErrConfigFileNotFound } dir = filepath.Dir(dir) diff --git a/internal/models/migration.go b/internal/models/migration.go index f647a1b..4007f3c 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -11,30 +11,22 @@ type Migration struct { Applied bool } -func NewMigration(timestamp time.Time, message string) *Migration { - version := fmt.Sprintf( - "%d%02d%02d%02d%02d%02d", - timestamp.Year(), timestamp.Month(), timestamp.Day(), - timestamp.Hour(), timestamp.Minute(), timestamp.Second(), - ) - return &Migration{Version: version, Message: message, Applied: false} -} - -func (m *Migration) String() string { - checkmark := " " - if m.Applied { - checkmark = "x" +func NewTimestampMigration(version time.Time, message string) *Migration { + return &Migration{ + Version: fmt.Sprintf( + "%d%02d%02d%02d%02d%02d", + version.Year(), version.Month(), version.Day(), + version.Hour(), version.Minute(), version.Second(), + ), + Message: message, + Applied: false, } +} - message := m.Message - if len(message) > 0 { - message = fmt.Sprintf("- %s ", message) +func NewSequentialMigration(version uint64, message string) *Migration { + return &Migration{ + Version: fmt.Sprintf("%03d", version), + Message: message, + Applied: false, } - - return fmt.Sprintf( - "%s %s- [%s]", - m.Version, - message, - checkmark, - ) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 8d1fb06..81068f3 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -9,25 +9,26 @@ import ( "github.com/eugenetriguba/bolt/internal/models" ) -var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) +func TestNewTimestampMigration(t *testing.T) { + var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) -func TestNewMigrationInit(t *testing.T) { - m := models.NewMigration(timestamp, "test message") + m := models.NewTimestampMigration(timestamp, "test message") assert.Equal(t, m.Version, "20200101000000") assert.Equal(t, m.Message, "test message") assert.Equal(t, m.Applied, false) } -func TestAppliedNewMigration(t *testing.T) { - m := models.NewMigration(timestamp, "test_message") +func TestNewSequentialMigration(t *testing.T) { + m := models.NewSequentialMigration(1, "test message") - assert.Equal(t, m.String(), "20200101000000 - test_message - [ ]") - m.Applied = true - assert.Equal(t, m.String(), "20200101000000 - test_message - [x]") -} + assert.Equal(t, m.Version, "001") + assert.Equal(t, m.Message, "test message") + assert.Equal(t, m.Applied, false) -func TestEmptyMessageMigration(t *testing.T) { - m := models.NewMigration(timestamp, "") - assert.Equal(t, "20200101000000 - [ ]", m.String()) + m = models.NewSequentialMigration(100, "test message") + + assert.Equal(t, m.Version, "100") + assert.Equal(t, m.Message, "test message") + assert.Equal(t, m.Applied, false) } diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 55b8eb2..dc635b0 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -131,7 +131,7 @@ func TestMigrationDBRepo_Apply(t *testing.T) { repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) assert.NilError(t, err) assert.Equal(t, migration.Applied, true) @@ -158,7 +158,7 @@ func TestMigrationDBRepo_Revert(t *testing.T) { _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, @@ -194,7 +194,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. err = repo.Apply( `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, @@ -215,7 +215,7 @@ func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply("this is not SQL", migration) assert.ErrorContains(t, err, "syntax error") @@ -230,7 +230,7 @@ func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") migration.Applied = true err = repo.Revert("this is not SQL", migration) diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 5478b5c..65afe5f 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -3,6 +3,7 @@ package repositories import ( "errors" "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -87,6 +88,19 @@ func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) { return migration, nil } +func (mr *MigrationFsRepo) Latest() (*models.Migration, error) { + entries, err := os.ReadDir(mr.migrationsDirPath) + if err != nil { + return nil, err + } + + if len(entries) > 0 { + return dirEntryToMigration(entries[len(entries)-1]) + } + + return nil, nil +} + func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { @@ -95,24 +109,32 @@ func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) { var migrations = make(map[string]*models.Migration) for _, entry := range entries { - parts := strings.SplitN(entry.Name(), "_", 2) - if len(parts) != 2 { - return nil, fmt.Errorf( - "%s is an invalid migration name: expected a "+ - "migration directory of the format _", - entry.Name(), - ) - } - migrations[parts[0]] = &models.Migration{ - Version: parts[0], - Message: parts[1], - Applied: false, + migration, err := dirEntryToMigration(entry) + if err != nil { + return nil, err } + migrations[migration.Version] = migration } return migrations, nil } +func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) { + parts := strings.SplitN(entry.Name(), "_", 2) + if len(parts) != 2 { + return nil, fmt.Errorf( + "%s is an invalid migration name: expected a "+ + "migration directory of the format _", + entry.Name(), + ) + } + return &models.Migration{ + Version: parts[0], + Message: parts[1], + Applied: false, + }, nil +} + func (mr *MigrationFsRepo) ReadUpgradeScript( migration *models.Migration, ) (string, error) { diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index 8fdcd8f..b1659fd 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -45,7 +45,7 @@ func TestMigrationFsRepoCreate(t *testing.T) { migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "add users table") + migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) assert.NilError(t, err) @@ -62,7 +62,7 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "add users table") + migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) assert.NilError(t, err) @@ -94,11 +94,11 @@ func TestMigrationFsRepo_List(t *testing.T) { repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) assert.NilError(t, err) - migration1 := models.NewMigration( + migration1 := models.NewTimestampMigration( time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC), "migration_1", ) - migration2 := models.NewMigration( + migration2 := models.NewTimestampMigration( time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC), "migration_2", ) diff --git a/internal/services/migration.go b/internal/services/migration.go index 8b5898e..91921bc 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -3,7 +3,10 @@ package services import ( "fmt" "sort" + "strconv" + "time" + "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/output" "github.com/eugenetriguba/bolt/internal/repositories" @@ -195,7 +198,26 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration(migration *models.Migration) error { +func (ms *MigrationService) CreateMigration(versionStyle configloader.VersionStyle, message string) error { + var migration *models.Migration + + if versionStyle == configloader.VersionStyleTimestamp { + migration = models.NewTimestampMigration(time.Now(), message) + } else { + var currentVerison uint64 = 0 + latestMigration, err := ms.fsRepo.Latest() + if err != nil { + return err + } + if latestMigration != nil { + currentVerison, err = strconv.ParseUint(latestMigration.Version, 10, 64) + if err != nil { + return err + } + } + migration = models.NewSequentialMigration(currentVerison+1, message) + } + err := ms.fsRepo.Create(migration) if err != nil { return err -- cgit v1.2.3 From b06eba3df09a8fcd777b8fe350c2b184758963d1 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 16:10:14 -0500 Subject: Use checkmate for tests --- internal/bolttest/db.go | 10 +- internal/bolttest/fs.go | 12 +-- internal/configloader/configloader_test.go | 14 +-- internal/configloader/configloader_unix_test.go | 8 +- internal/configloader/configloader_windows_test.go | 10 +- internal/models/migration_test.go | 21 ++-- internal/output/console_test.go | 14 +-- internal/repositories/migration_db_repo_test.go | 118 ++++++++++----------- internal/repositories/migration_fs_repo_test.go | 40 +++---- internal/sqlparse/sqlparse_test.go | 6 +- internal/storage/db_test.go | 12 +-- 11 files changed, 132 insertions(+), 133 deletions(-) (limited to 'internal') diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index de2dcfa..74ca14e 100644 --- a/internal/bolttest/db.go +++ b/internal/bolttest/db.go @@ -8,12 +8,12 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/storage" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig { port, err := strconv.Atoi(os.Getenv("DB_PORT")) - assert.NilError(t, err) + checkmate.AssertNil(t, err) return &configloader.ConnectionConfig{ Driver: driver, @@ -29,7 +29,7 @@ func NewTestDB(t *testing.T, driver string) *sql.DB { connectionConfig := NewTestConnectionConfig(t, driver) connInfo := storage.DBConnectionString(connectionConfig) db, err := storage.DBConnect(driver, connInfo) - assert.NilError(t, err) + checkmate.AssertNil(t, err) t.Cleanup(func() { _, err = db.Exec(` DO $$ DECLARE rec RECORD; @@ -41,9 +41,9 @@ func NewTestDB(t *testing.T, driver string) *sql.DB { END LOOP; END $$; `) - assert.NilError(t, err) + checkmate.AssertNil(t, err) err = db.Close() - assert.NilError(t, err) + checkmate.AssertNil(t, err) }) return db } diff --git a/internal/bolttest/fs.go b/internal/bolttest/fs.go index 9edd894..0237733 100644 --- a/internal/bolttest/fs.go +++ b/internal/bolttest/fs.go @@ -6,7 +6,7 @@ import ( "github.com/BurntSushi/toml" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func CreateConfigFile(t *testing.T, cfg *configloader.Config, filePath string) { @@ -14,15 +14,15 @@ func CreateConfigFile(t *testing.T, cfg *configloader.Config, filePath string) { encoder := toml.NewEncoder(f) err := encoder.Encode(cfg) - assert.NilError(t, err) + checkmate.AssertNil(t, err) err = f.Close() - assert.NilError(t, err) + checkmate.AssertNil(t, err) } func CreateTempFile(t *testing.T, filePath string) *os.File { f, err := os.Create(filePath) - assert.NilError(t, err) + checkmate.AssertNil(t, err) t.Cleanup(func() { err = os.Remove(f.Name()) @@ -36,10 +36,10 @@ func CreateTempFile(t *testing.T, filePath string) *os.File { func ChangeCwd(t *testing.T, path string) { dir, err := os.Getwd() - assert.NilError(t, err) + checkmate.AssertNil(t, err) err = os.Chdir(path) - assert.NilError(t, err) + checkmate.AssertNil(t, err) t.Cleanup(func() { err = os.Chdir(dir) diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index a41c2be..3cb9d71 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -7,16 +7,16 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestNewConfigMigrationsDirPathDefault(t *testing.T) { bolttest.ChangeCwd(t, os.TempDir()) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + checkmate.AssertNil(t, err) - assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations") + checkmate.AssertEqual(t, cfg.Migrations.DirectoryPath, "migrations") } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { @@ -34,8 +34,8 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml") cfg, err := configloader.NewConfig() - assert.NilError(t, err) - assert.DeepEqual(t, *cfg, expectedCfg) + checkmate.AssertNil(t, err) + checkmate.AssertDeepEqual(t, *cfg, expectedCfg) } func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { @@ -72,6 +72,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { t.Setenv("BOLT_DB_CONN_DRIVER", envCfg.Connection.Driver) cfg, err := configloader.NewConfig() - assert.NilError(t, err) - assert.DeepEqual(t, *cfg, envCfg) + checkmate.AssertNil(t, err) + checkmate.AssertDeepEqual(t, *cfg, envCfg) } diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go index dcd8ebd..b9cef31 100644 --- a/internal/configloader/configloader_unix_test.go +++ b/internal/configloader/configloader_unix_test.go @@ -8,7 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { @@ -18,11 +18,11 @@ func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml") homedir, err := os.UserHomeDir() - assert.NilError(t, err) + checkmate.AssertNil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + checkmate.AssertNil(t, err) - assert.DeepEqual(t, *cfg, expectedCfg) + checkmate.AssertDeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go index 42f2978..a6a24e6 100644 --- a/internal/configloader/configloader_windows_test.go +++ b/internal/configloader/configloader_windows_test.go @@ -9,7 +9,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { @@ -18,14 +18,14 @@ func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { } configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml") bolttest.CreateConfigFile(t, &expectedCfg, configPath) - assert.NilError(t, err) + checkmate.AssertNil(t, err) homedir, err := os.UserHomeDir() - assert.NilError(t, err) + checkmate.AssertNil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + checkmate.AssertNil(t, err) - assert.DeepEqual(t, *cfg, expectedCfg) + checkmate.AssertDeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 81068f3..1d449c8 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -4,9 +4,8 @@ import ( "testing" "time" - "gotest.tools/v3/assert" - "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/checkmate" ) func TestNewTimestampMigration(t *testing.T) { @@ -14,21 +13,21 @@ func TestNewTimestampMigration(t *testing.T) { m := models.NewTimestampMigration(timestamp, "test message") - assert.Equal(t, m.Version, "20200101000000") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + checkmate.AssertEqual(t, m.Version, "20200101000000") + checkmate.AssertEqual(t, m.Message, "test message") + checkmate.AssertEqual(t, m.Applied, false) } func TestNewSequentialMigration(t *testing.T) { m := models.NewSequentialMigration(1, "test message") - assert.Equal(t, m.Version, "001") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + checkmate.AssertEqual(t, m.Version, "001") + checkmate.AssertEqual(t, m.Message, "test message") + checkmate.AssertEqual(t, m.Applied, false) m = models.NewSequentialMigration(100, "test message") - assert.Equal(t, m.Version, "100") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + checkmate.AssertEqual(t, m.Version, "100") + checkmate.AssertEqual(t, m.Message, "test message") + checkmate.AssertEqual(t, m.Applied, false) } diff --git a/internal/output/console_test.go b/internal/output/console_test.go index be369ab..e006cd6 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -5,7 +5,7 @@ import ( "io" "testing" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { @@ -19,8 +19,8 @@ func TestConsoleOutputter_Output(t *testing.T) { consoleOutputter.Output("test string") - assert.Equal(t, stdout.String(), "test string\n") - assert.Equal(t, stderr.String(), "") + checkmate.AssertEqual(t, stdout.String(), "test string\n") + checkmate.AssertEqual(t, stderr.String(), "") } func TestConsoleOutputter_Error(t *testing.T) { @@ -30,8 +30,8 @@ func TestConsoleOutputter_Error(t *testing.T) { consoleOutputter.Error("test string") - assert.Equal(t, stdout.String(), "") - assert.Equal(t, stderr.String(), "test string\n") + checkmate.AssertEqual(t, stdout.String(), "") + checkmate.AssertEqual(t, stderr.String(), "test string\n") } func TestConsoleOutputter_Table(t *testing.T) { @@ -70,7 +70,7 @@ func TestConsoleOutputter_Table(t *testing.T) { consoleOutputter.Table(tc.headers, tc.rows) - assert.Equal(t, stdout.String(), tc.expectedStdout) - assert.Equal(t, stderr.String(), "") + checkmate.AssertEqual(t, stdout.String(), tc.expectedStdout) + checkmate.AssertEqual(t, stderr.String(), "") } } diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index dc635b0..5d1a9f1 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -8,7 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { @@ -21,10 +21,10 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { `) var scanResult int err := row.Scan(&scanResult) - assert.ErrorIs(t, err, sql.ErrNoRows) + checkmate.AssertErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,49 +33,49 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - assert.NilError(t, err) + checkmate.AssertNil(t, err) } func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") _, err := db.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) row := db.QueryRow(`SELECT id FROM bolt_migrations`) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) - assert.Equal(t, scanResult, 1) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, scanResult, 1) } func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 0) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 0) } func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 1) - assert.DeepEqual( + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 1) + checkmate.AssertDeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -85,16 +85,16 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 1) - assert.DeepEqual( + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 1) + checkmate.AssertDeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -104,37 +104,37 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - assert.NilError(t, err) - assert.Equal(t, applied, false) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, false) } func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) applied, err := repo.IsApplied(version) - assert.NilError(t, err) - assert.Equal(t, applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, true) } func TestMigrationDBRepo_Apply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, true) row := db.QueryRow(` SELECT 1 @@ -144,31 +144,31 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + checkmate.AssertNil(t, err) applied, err := repo.IsApplied(migration.Version) - assert.NilError(t, err) - assert.Equal(t, applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, true) } func TestMigrationDBRepo_Revert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, ) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, false) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, false) row := db.QueryRow(` SELECT 1 @@ -178,21 +178,21 @@ func TestMigrationDBRepo_Revert(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.ErrorIs(t, err, sql.ErrNoRows) + checkmate.AssertErrorIs(t, err, sql.ErrNoRows) row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) var count int err = row.Scan(&count) - assert.NilError(t, err) - assert.Equal(t, count, 0) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, count, 0) } func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. @@ -200,44 +200,44 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, true) err = repo.Revert( "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", migration, ) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, false) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, false) } func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, false) err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, false) } func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") migration.Applied = true err = repo.Revert("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, true) err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, true) } diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index b1659fd..2398260 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -10,12 +10,12 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func assertFileExists(t *testing.T, path string) { _, err := os.Stat(path) - assert.NilError(t, err) + checkmate.AssertNil(t, err) } func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) { @@ -24,7 +24,7 @@ func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) { migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} _, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.NilError(t, err) + checkmate.AssertNil(t, err) assertFileExists(t, migrationsDir) } @@ -34,21 +34,21 @@ func TestNewMigrationFsRepoMigrationDirIsFile(t *testing.T) { migrationsDir := filepath.Join(tempDir, "migrations") migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} _, err := os.Create(migrationsDir) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = repositories.NewMigrationFsRepo(&migrationsConfig) - assert.ErrorContains(t, err, "is not a directory") + checkmate.AssertErrorContains(t, err, "is not a directory") } func TestMigrationFsRepoCreate(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) assertFileExists(t, filepath.Join(tempDir, migrationDirName)) @@ -60,11 +60,11 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);") @@ -81,18 +81,18 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { ) upgradeScriptContents, err := repo.ReadUpgradeScript(migration) - assert.NilError(t, err) - assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) downgradeScriptContents, err := repo.ReadDowngradeScript(migration) - assert.NilError(t, err) - assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) } func TestMigrationFsRepo_List(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration1 := models.NewTimestampMigration( time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC), @@ -103,13 +103,13 @@ func TestMigrationFsRepo_List(t *testing.T) { "migration_2", ) err = repo.Create(migration1) - assert.NilError(t, err) + checkmate.AssertNil(t, err) repo.Create(migration2) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 2) - assert.DeepEqual(t, migrations[migration1.Version], migration1) - assert.DeepEqual(t, migrations[migration2.Version], migration2) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 2) + checkmate.AssertDeepEqual(t, migrations[migration1.Version], migration1) + checkmate.AssertDeepEqual(t, migrations[migration2.Version], migration2) } diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 51b93b1..644a79f 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/eugenetriguba/bolt/internal/sqlparse" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestSqlParser_Parse(t *testing.T) { @@ -49,8 +49,8 @@ func TestSqlParser_Parse(t *testing.T) { sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) execOptions, err := sqlParser.Parse() - assert.NilError(t, err) + checkmate.AssertNil(t, err) - assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) + checkmate.AssertDeepEqual(t, execOptions, tc.expectedExecutionOptions) } } diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index d9e22d0..2f4c7db 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -7,30 +7,30 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/storage" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate" ) func TestDBConnectWithPostgres(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - assert.NilError(t, err) + checkmate.AssertNil(t, err) defer conn.Close() _, err = conn.Exec("SELECT 1;") - assert.NilError(t, err) + checkmate.AssertNil(t, err) } func TestDBConnectMalformedConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") - assert.ErrorContains(t, err, "connection refused") + checkmate.AssertErrorContains(t, err, "connection refused") } func TestDBConnectUnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - assert.ErrorContains(t, err, `unknown driver "redis"`) + checkmate.AssertErrorContains(t, err, `unknown driver "redis"`) } func TestDBConnectionString(t *testing.T) { @@ -49,5 +49,5 @@ func TestDBConnectionString(t *testing.T) { "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, ) - assert.Equal(t, cs, expectedConnectionString) + checkmate.AssertEqual(t, cs, expectedConnectionString) } -- cgit v1.2.3 From 345e28bd44ba6a38bf7ee9bfe2066f333bbbe894 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 16:29:00 -0500 Subject: Update to checkmate v0.2.0 --- internal/bolttest/db.go | 10 +- internal/bolttest/fs.go | 12 +-- internal/configloader/configloader_test.go | 14 +-- internal/configloader/configloader_unix_test.go | 8 +- internal/configloader/configloader_windows_test.go | 10 +- internal/models/migration_test.go | 20 ++-- internal/output/console_test.go | 14 +-- internal/repositories/migration_db_repo_test.go | 118 ++++++++++----------- internal/repositories/migration_fs_repo_test.go | 40 +++---- internal/sqlparse/sqlparse_test.go | 6 +- internal/storage/db_test.go | 12 +-- 11 files changed, 132 insertions(+), 132 deletions(-) (limited to 'internal') diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index 74ca14e..bde519b 100644 --- a/internal/bolttest/db.go +++ b/internal/bolttest/db.go @@ -8,12 +8,12 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/storage" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig { port, err := strconv.Atoi(os.Getenv("DB_PORT")) - checkmate.AssertNil(t, err) + assert.Nil(t, err) return &configloader.ConnectionConfig{ Driver: driver, @@ -29,7 +29,7 @@ func NewTestDB(t *testing.T, driver string) *sql.DB { connectionConfig := NewTestConnectionConfig(t, driver) connInfo := storage.DBConnectionString(connectionConfig) db, err := storage.DBConnect(driver, connInfo) - checkmate.AssertNil(t, err) + assert.Nil(t, err) t.Cleanup(func() { _, err = db.Exec(` DO $$ DECLARE rec RECORD; @@ -41,9 +41,9 @@ func NewTestDB(t *testing.T, driver string) *sql.DB { END LOOP; END $$; `) - checkmate.AssertNil(t, err) + assert.Nil(t, err) err = db.Close() - checkmate.AssertNil(t, err) + assert.Nil(t, err) }) return db } diff --git a/internal/bolttest/fs.go b/internal/bolttest/fs.go index 0237733..36078f4 100644 --- a/internal/bolttest/fs.go +++ b/internal/bolttest/fs.go @@ -6,7 +6,7 @@ import ( "github.com/BurntSushi/toml" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func CreateConfigFile(t *testing.T, cfg *configloader.Config, filePath string) { @@ -14,15 +14,15 @@ func CreateConfigFile(t *testing.T, cfg *configloader.Config, filePath string) { encoder := toml.NewEncoder(f) err := encoder.Encode(cfg) - checkmate.AssertNil(t, err) + assert.Nil(t, err) err = f.Close() - checkmate.AssertNil(t, err) + assert.Nil(t, err) } func CreateTempFile(t *testing.T, filePath string) *os.File { f, err := os.Create(filePath) - checkmate.AssertNil(t, err) + assert.Nil(t, err) t.Cleanup(func() { err = os.Remove(f.Name()) @@ -36,10 +36,10 @@ func CreateTempFile(t *testing.T, filePath string) *os.File { func ChangeCwd(t *testing.T, path string) { dir, err := os.Getwd() - checkmate.AssertNil(t, err) + assert.Nil(t, err) err = os.Chdir(path) - checkmate.AssertNil(t, err) + assert.Nil(t, err) t.Cleanup(func() { err = os.Chdir(dir) diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 3cb9d71..f08f12f 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -7,16 +7,16 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestNewConfigMigrationsDirPathDefault(t *testing.T) { bolttest.ChangeCwd(t, os.TempDir()) cfg, err := configloader.NewConfig() - checkmate.AssertNil(t, err) + assert.Nil(t, err) - checkmate.AssertEqual(t, cfg.Migrations.DirectoryPath, "migrations") + assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations") } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { @@ -34,8 +34,8 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml") cfg, err := configloader.NewConfig() - checkmate.AssertNil(t, err) - checkmate.AssertDeepEqual(t, *cfg, expectedCfg) + assert.Nil(t, err) + assert.DeepEqual(t, *cfg, expectedCfg) } func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { @@ -72,6 +72,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { t.Setenv("BOLT_DB_CONN_DRIVER", envCfg.Connection.Driver) cfg, err := configloader.NewConfig() - checkmate.AssertNil(t, err) - checkmate.AssertDeepEqual(t, *cfg, envCfg) + assert.Nil(t, err) + assert.DeepEqual(t, *cfg, envCfg) } diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go index b9cef31..0095afd 100644 --- a/internal/configloader/configloader_unix_test.go +++ b/internal/configloader/configloader_unix_test.go @@ -8,7 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { @@ -18,11 +18,11 @@ func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml") homedir, err := os.UserHomeDir() - checkmate.AssertNil(t, err) + assert.Nil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - checkmate.AssertNil(t, err) + assert.Nil(t, err) - checkmate.AssertDeepEqual(t, *cfg, expectedCfg) + assert.DeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go index a6a24e6..1b62624 100644 --- a/internal/configloader/configloader_windows_test.go +++ b/internal/configloader/configloader_windows_test.go @@ -9,7 +9,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { @@ -18,14 +18,14 @@ func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { } configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml") bolttest.CreateConfigFile(t, &expectedCfg, configPath) - checkmate.AssertNil(t, err) + assert.Nil(t, err) homedir, err := os.UserHomeDir() - checkmate.AssertNil(t, err) + assert.Nil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - checkmate.AssertNil(t, err) + assert.Nil(t, err) - checkmate.AssertDeepEqual(t, *cfg, expectedCfg) + assert.DeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 1d449c8..aa92510 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -5,7 +5,7 @@ import ( "time" "github.com/eugenetriguba/bolt/internal/models" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestNewTimestampMigration(t *testing.T) { @@ -13,21 +13,21 @@ func TestNewTimestampMigration(t *testing.T) { m := models.NewTimestampMigration(timestamp, "test message") - checkmate.AssertEqual(t, m.Version, "20200101000000") - checkmate.AssertEqual(t, m.Message, "test message") - checkmate.AssertEqual(t, m.Applied, false) + assert.Equal(t, m.Version, "20200101000000") + assert.Equal(t, m.Message, "test message") + assert.Equal(t, m.Applied, false) } func TestNewSequentialMigration(t *testing.T) { m := models.NewSequentialMigration(1, "test message") - checkmate.AssertEqual(t, m.Version, "001") - checkmate.AssertEqual(t, m.Message, "test message") - checkmate.AssertEqual(t, m.Applied, false) + assert.Equal(t, m.Version, "001") + assert.Equal(t, m.Message, "test message") + assert.Equal(t, m.Applied, false) m = models.NewSequentialMigration(100, "test message") - checkmate.AssertEqual(t, m.Version, "100") - checkmate.AssertEqual(t, m.Message, "test message") - checkmate.AssertEqual(t, m.Applied, false) + assert.Equal(t, m.Version, "100") + assert.Equal(t, m.Message, "test message") + assert.Equal(t, m.Applied, false) } diff --git a/internal/output/console_test.go b/internal/output/console_test.go index e006cd6..ff1f8b8 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -5,7 +5,7 @@ import ( "io" "testing" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { @@ -19,8 +19,8 @@ func TestConsoleOutputter_Output(t *testing.T) { consoleOutputter.Output("test string") - checkmate.AssertEqual(t, stdout.String(), "test string\n") - checkmate.AssertEqual(t, stderr.String(), "") + assert.Equal(t, stdout.String(), "test string\n") + assert.Equal(t, stderr.String(), "") } func TestConsoleOutputter_Error(t *testing.T) { @@ -30,8 +30,8 @@ func TestConsoleOutputter_Error(t *testing.T) { consoleOutputter.Error("test string") - checkmate.AssertEqual(t, stdout.String(), "") - checkmate.AssertEqual(t, stderr.String(), "test string\n") + assert.Equal(t, stdout.String(), "") + assert.Equal(t, stderr.String(), "test string\n") } func TestConsoleOutputter_Table(t *testing.T) { @@ -70,7 +70,7 @@ func TestConsoleOutputter_Table(t *testing.T) { consoleOutputter.Table(tc.headers, tc.rows) - checkmate.AssertEqual(t, stdout.String(), tc.expectedStdout) - checkmate.AssertEqual(t, stderr.String(), "") + assert.Equal(t, stdout.String(), tc.expectedStdout) + assert.Equal(t, stderr.String(), "") } } diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 5d1a9f1..4e41b36 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -8,7 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { @@ -21,10 +21,10 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { `) var scanResult int err := row.Scan(&scanResult) - checkmate.AssertErrorIs(t, err, sql.ErrNoRows) + assert.ErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,49 +33,49 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) + assert.Nil(t, err) } func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") _, err := db.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) row := db.QueryRow(`SELECT id FROM bolt_migrations`) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, scanResult, 1) + assert.Nil(t, err) + assert.Equal(t, scanResult, 1) } func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 0) + assert.Nil(t, err) + assert.Equal(t, len(migrations), 0) } func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 1) - checkmate.AssertDeepEqual( + assert.Nil(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -85,16 +85,16 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 1) - checkmate.AssertDeepEqual( + assert.Nil(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -104,37 +104,37 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, false) + assert.Nil(t, err) + assert.Equal(t, applied, false) } func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, true) + assert.Nil(t, err) + assert.Equal(t, applied, true) } func TestMigrationDBRepo_Apply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, true) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, true) row := db.QueryRow(` SELECT 1 @@ -144,31 +144,31 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(migration.Version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, true) + assert.Nil(t, err) + assert.Equal(t, applied, true) } func TestMigrationDBRepo_Revert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, ) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, false) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) row := db.QueryRow(` SELECT 1 @@ -178,21 +178,21 @@ func TestMigrationDBRepo_Revert(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertErrorIs(t, err, sql.ErrNoRows) + assert.ErrorIs(t, err, sql.ErrNoRows) row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) var count int err = row.Scan(&count) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, count, 0) + assert.Nil(t, err) + assert.Equal(t, count, 0) } func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. @@ -200,44 +200,44 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, true) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, true) err = repo.Revert( "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", migration, ) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, false) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) } func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply("this is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, false) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, false) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) } func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") migration.Applied = true err = repo.Revert("this is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, true) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, true) err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, true) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, true) } diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index 2398260..72ba792 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -10,12 +10,12 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func assertFileExists(t *testing.T, path string) { _, err := os.Stat(path) - checkmate.AssertNil(t, err) + assert.Nil(t, err) } func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) { @@ -24,7 +24,7 @@ func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) { migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} _, err := repositories.NewMigrationFsRepo(&migrationsConfig) - checkmate.AssertNil(t, err) + assert.Nil(t, err) assertFileExists(t, migrationsDir) } @@ -34,21 +34,21 @@ func TestNewMigrationFsRepoMigrationDirIsFile(t *testing.T) { migrationsDir := filepath.Join(tempDir, "migrations") migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} _, err := os.Create(migrationsDir) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = repositories.NewMigrationFsRepo(&migrationsConfig) - checkmate.AssertErrorContains(t, err, "is not a directory") + assert.ErrorContains(t, err, "is not a directory") } func TestMigrationFsRepoCreate(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) assertFileExists(t, filepath.Join(tempDir, migrationDirName)) @@ -60,11 +60,11 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);") @@ -81,18 +81,18 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { ) upgradeScriptContents, err := repo.ReadUpgradeScript(migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) + assert.Nil(t, err) + assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) downgradeScriptContents, err := repo.ReadDowngradeScript(migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) + assert.Nil(t, err) + assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) } func TestMigrationFsRepo_List(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration1 := models.NewTimestampMigration( time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC), @@ -103,13 +103,13 @@ func TestMigrationFsRepo_List(t *testing.T) { "migration_2", ) err = repo.Create(migration1) - checkmate.AssertNil(t, err) + assert.Nil(t, err) repo.Create(migration2) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 2) - checkmate.AssertDeepEqual(t, migrations[migration1.Version], migration1) - checkmate.AssertDeepEqual(t, migrations[migration2.Version], migration2) + assert.Nil(t, err) + assert.Equal(t, len(migrations), 2) + assert.DeepEqual(t, migrations[migration1.Version], migration1) + assert.DeepEqual(t, migrations[migration2.Version], migration2) } diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 644a79f..17e2f10 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/eugenetriguba/bolt/internal/sqlparse" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestSqlParser_Parse(t *testing.T) { @@ -49,8 +49,8 @@ func TestSqlParser_Parse(t *testing.T) { sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) execOptions, err := sqlParser.Parse() - checkmate.AssertNil(t, err) + assert.Nil(t, err) - checkmate.AssertDeepEqual(t, execOptions, tc.expectedExecutionOptions) + assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) } } diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index 2f4c7db..61a9bf4 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -7,30 +7,30 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/storage" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestDBConnectWithPostgres(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - checkmate.AssertNil(t, err) + assert.Nil(t, err) defer conn.Close() _, err = conn.Exec("SELECT 1;") - checkmate.AssertNil(t, err) + assert.Nil(t, err) } func TestDBConnectMalformedConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") - checkmate.AssertErrorContains(t, err, "connection refused") + assert.ErrorContains(t, err, "connection refused") } func TestDBConnectUnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - checkmate.AssertErrorContains(t, err, `unknown driver "redis"`) + assert.ErrorContains(t, err, `unknown driver "redis"`) } func TestDBConnectionString(t *testing.T) { @@ -49,5 +49,5 @@ func TestDBConnectionString(t *testing.T) { "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, ) - checkmate.AssertEqual(t, cs, expectedConnectionString) + assert.Equal(t, cs, expectedConnectionString) } -- cgit v1.2.3 From 77f54361419914991093a7d5a2b2566bcd2e208b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 16:55:23 -0500 Subject: Fix config tests --- internal/configloader/configloader_test.go | 19 +++++++++++++++---- internal/configloader/configloader_unix_test.go | 7 +++++-- internal/configloader/configloader_windows_test.go | 7 +++++-- 3 files changed, 25 insertions(+), 8 deletions(-) (limited to 'internal') diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index f08f12f..d33a19b 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -10,18 +10,22 @@ import ( "github.com/eugenetriguba/checkmate/assert" ) -func TestNewConfigMigrationsDirPathDefault(t *testing.T) { +func TestNewConfigDefaults(t *testing.T) { bolttest.ChangeCwd(t, os.TempDir()) cfg, err := configloader.NewConfig() assert.Nil(t, err) assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations") + assert.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { expectedCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{DirectoryPath: "myfancymigrations"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "myfancymigrations", + VersionStyle: configloader.VersionStyleSequential, + }, Connection: configloader.ConnectionConfig{ Host: "testhost", Port: 1234, @@ -40,7 +44,10 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { fileCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{DirectoryPath: "cfgmigrations"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "cfgmigrations", + VersionStyle: configloader.VersionStyleSequential, + }, Connection: configloader.ConnectionConfig{ Host: "testhost", Port: 1234, @@ -53,7 +60,10 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml") envCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{DirectoryPath: "envmigrations"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "envmigrations", + VersionStyle: configloader.VersionStyleTimestamp, + }, Connection: configloader.ConnectionConfig{ Host: "envtesthost", Port: 4321, @@ -63,6 +73,7 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { Driver: "postgres", }, } + t.Setenv("BOLT_MIGRATIONS_VERSION_STYLE", string(envCfg.Migrations.VersionStyle)) t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath) t.Setenv("BOLT_DB_CONN_HOST", envCfg.Connection.Host) t.Setenv("BOLT_DB_CONN_PORT", fmt.Sprintf("%d", envCfg.Connection.Port)) diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go index 0095afd..8043bdb 100644 --- a/internal/configloader/configloader_unix_test.go +++ b/internal/configloader/configloader_unix_test.go @@ -1,4 +1,4 @@ -// go:build unix +//go:build unix package configloader_test @@ -13,7 +13,10 @@ import ( func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { expectedCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{DirectoryPath: "differentmigrationsdir"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "differentmigrationsdir", + VersionStyle: configloader.VersionStyleSequential, + }, } bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml") diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go index 1b62624..1ba6c51 100644 --- a/internal/configloader/configloader_windows_test.go +++ b/internal/configloader/configloader_windows_test.go @@ -1,4 +1,4 @@ -// go:build windows +//go:build windows package configloader_test @@ -14,7 +14,10 @@ import ( func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { expectedCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{DirectoryPath: "differentmigrationsdir"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "differentmigrationsdir", + VersionStyle: configloader.VersionStyleSequential, + }, } configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml") bolttest.CreateConfigFile(t, &expectedCfg, configPath) -- cgit v1.2.3 From 8aa1cc45980e12c7fb7de25419af358a784f80c6 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 17:28:44 -0500 Subject: Add sqlparse scanner error condition test --- internal/sqlparse/sqlparse_test.go | 40 +++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 17e2f10..5fe58fa 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -1,6 +1,9 @@ package sqlparse_test import ( + "bytes" + "errors" + "io" "strings" "testing" @@ -9,12 +12,10 @@ import ( ) func TestSqlParser_Parse(t *testing.T) { - type test struct { + testCases := []struct { buffer string expectedExecutionOptions *sqlparse.ExecutionOptions - } - - tests := []test{ + }{ { buffer: "-- bolt: no-transaction", expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, @@ -45,7 +46,7 @@ func TestSqlParser_Parse(t *testing.T) { }, } - for _, tc := range tests { + for _, tc := range testCases { sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) execOptions, err := sqlParser.Parse() @@ -54,3 +55,32 @@ func TestSqlParser_Parse(t *testing.T) { assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) } } + +type ErrReader struct { + Reader io.Reader + ErrCond string +} + +func (er *ErrReader) Read(p []byte) (int, error) { + n, err := er.Reader.Read(p) + if err != nil { + return n, err + } + + if bytes.Contains(p[:n], []byte(er.ErrCond)) { + return n, errors.New("error: unwanted input encountered") + } + + return n, nil +} + +func TestSqlParserParseError(t *testing.T) { + reader := &ErrReader{ + Reader: strings.NewReader(" "), + ErrCond: " ", + } + sqlParser := sqlparse.NewSqlParser(reader) + _, err := sqlParser.Parse() + assert.NotNil(t, err) + assert.ErrorContains(t, err, "unwanted input encountered") +} -- cgit v1.2.3 From b8b23e393dcda33cc98b22f4083977b543861bf2 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 17:29:19 -0500 Subject: Add test for invalid version style in config --- internal/configloader/configloader.go | 16 ++++------------ internal/configloader/configloader_test.go | 13 +++++++++++++ internal/services/migration.go | 5 ++++- 3 files changed, 21 insertions(+), 13 deletions(-) (limited to 'internal') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index c5aba2d..6155d56 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -50,15 +50,7 @@ type Config struct { type MigrationsConfig struct { DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` - VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` -} - -func (mc *MigrationsConfig) Validate() error { - if mc.VersionStyle != VersionStyleSequential && mc.VersionStyle != VersionStyleTimestamp { - return ErrInvalidVersionStyle - } - - return nil + VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` } type ConnectionConfig struct { @@ -94,9 +86,9 @@ func NewConfig() (*Config, error) { return nil, err } - err = cfg.Migrations.Validate() - if err != nil { - return nil, err + if cfg.Migrations.VersionStyle != VersionStyleSequential && + cfg.Migrations.VersionStyle != VersionStyleTimestamp { + return nil, ErrInvalidVersionStyle } return &cfg, nil diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index d33a19b..6229cf6 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -20,6 +20,19 @@ func TestNewConfigDefaults(t *testing.T) { assert.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) } +func TestNewConfigWithInvalidVersionStyle(t *testing.T) { + fileCfg := configloader.Config{ + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "myfancymigrations", + VersionStyle: "invalid", + }, + } + bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml") + + _, err := configloader.NewConfig() + assert.ErrorIs(t, err, configloader.ErrInvalidVersionStyle) +} + func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { expectedCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ diff --git a/internal/services/migration.go b/internal/services/migration.go index 91921bc..6836980 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -198,7 +198,10 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration(versionStyle configloader.VersionStyle, message string) error { +func (ms *MigrationService) CreateMigration( + versionStyle configloader.VersionStyle, + message string, +) error { var migration *models.Migration if versionStyle == configloader.VersionStyleTimestamp { -- cgit v1.2.3 From 95eea2a07198576e3f4b0a246dd5e2b0cd4ef219 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 17:47:31 -0500 Subject: Wrap db repo and console errs to give better errors --- internal/output/console.go | 12 +++---- internal/repositories/migration_db_repo.go | 53 ++++++++++++++++++++++-------- internal/repositories/migration_fs_repo.go | 4 ++- 3 files changed, 48 insertions(+), 21 deletions(-) (limited to 'internal') diff --git a/internal/output/console.go b/internal/output/console.go index d2946ed..50534dd 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -18,12 +18,12 @@ func NewConsoleOutputter() ConsoleOutputter { func (c ConsoleOutputter) Output(message string) error { _, err := fmt.Fprintln(c.stdout, message) - return err + return fmt.Errorf("unable to print message %s: %w", message, err) } func (c ConsoleOutputter) Error(message string) error { _, err := fmt.Fprintln(c.stderr, message) - return err + return fmt.Errorf("unable to print message %s: %w", message, err) } func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { @@ -32,13 +32,13 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { for _, header := range headers { _, err := fmt.Fprintf(w, "%s\t", header) if err != nil { - return err + return fmt.Errorf("unable to print table header %s: %w", header, err) } } if len(headers) > 0 { _, err := fmt.Fprint(w, "\n") if err != nil { - return err + return fmt.Errorf("unable to print newline after table header: %w", err) } } @@ -46,12 +46,12 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { for _, cell := range row { _, err := fmt.Fprintf(w, "%s\t", cell) if err != nil { - return err + return fmt.Errorf("unable to print table cell %s: %w", cell, err) } } _, err := fmt.Fprint(w, "\n") if err != nil { - return err + return fmt.Errorf("unable to print newline for table row %s: %w", row, err) } } diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 42cf0e8..93b9c73 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -3,6 +3,7 @@ package repositories import ( "database/sql" "errors" + "fmt" "strings" "github.com/eugenetriguba/bolt/internal/models" @@ -91,19 +92,22 @@ func (mr *MigrationDBRepo) Apply( sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript)) execOptions, err := sqlParser.Parse() if err != nil { - return err + return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) } if execOptions.UseTransaction { tx, err := mr.db.Begin() if err != nil { - return err + return fmt.Errorf( + "unable to start transaction during upgrade script execution: %w", + err, + ) } defer tx.Rollback() _, err = tx.Exec(upgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute upgrade script: %w", err) } _, err = tx.Exec( @@ -111,16 +115,22 @@ func (mr *MigrationDBRepo) Apply( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to insert applied migration to bolt_migrations table: %w", + err, + ) } err = tx.Commit() if err != nil { - return err + return fmt.Errorf( + "unable to commit transaction during upgrade script execution: %w", + err, + ) } } else { _, err = mr.db.Exec(upgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute upgrade script: %w", err) } _, err = mr.db.Exec( @@ -128,7 +138,10 @@ func (mr *MigrationDBRepo) Apply( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to insert applied migration to bolt_migrations table: %w", + err, + ) } } @@ -146,19 +159,22 @@ func (mr *MigrationDBRepo) Revert( sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript)) execOptions, err := sqlParser.Parse() if err != nil { - return err + return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) } if execOptions.UseTransaction { tx, err := mr.db.Begin() if err != nil { - return err + return fmt.Errorf( + "unable to start transaction during downgrade script execution: %w", + err, + ) } defer tx.Rollback() _, err = tx.Exec(downgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute downgrade script: %w", err) } _, err = tx.Exec( @@ -166,17 +182,23 @@ func (mr *MigrationDBRepo) Revert( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) } err = tx.Commit() if err != nil { - return err + return fmt.Errorf( + "unable to commit transaction during downgrade script execution: %w", + err, + ) } } else { _, err = mr.db.Exec(downgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute downgrade script: %w", err) } _, err = mr.db.Exec( @@ -184,7 +206,10 @@ func (mr *MigrationDBRepo) Revert( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) } } diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 65afe5f..c541006 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -27,7 +27,9 @@ type MigrationFsRepo struct { migrationsDirPath string } -func NewMigrationFsRepo(migrationsConfig *configloader.MigrationsConfig) (*MigrationFsRepo, error) { +func NewMigrationFsRepo( + migrationsConfig *configloader.MigrationsConfig, +) (*MigrationFsRepo, error) { fileInfo, err := os.Stat(migrationsConfig.DirectoryPath) if errors.Is(err, os.ErrNotExist) { err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755) -- cgit v1.2.3 From fb84192eef75c909d031aa867d407f1b7797e9db Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 19:57:15 -0500 Subject: Add more wrapped errors --- internal/repositories/migration_db_repo.go | 16 +++++++++++++--- internal/sqlparse/sqlparse.go | 3 ++- internal/storage/db.go | 11 +++++------ 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'internal') diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 93b9c73..b9e2d78 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -25,7 +25,10 @@ func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { ); `) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to create bolt_migrations database table: %w", + err, + ) } return &MigrationDBRepo{db: db}, nil @@ -38,7 +41,11 @@ func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { rows, err := mr.db.Query(`SELECT version FROM bolt_migrations;`) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to execute query to select versions from "+ + "bolt_migrations database table: %w", + err, + ) } defer rows.Close() @@ -47,7 +54,10 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { var version string err := rows.Scan(&version) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to scan version row from applied migrations: %w", + err, + ) } trimmedVersion := strings.TrimSpace(version) migrations[trimmedVersion] = &models.Migration{ diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go index 9ded618..25960c5 100644 --- a/internal/sqlparse/sqlparse.go +++ b/internal/sqlparse/sqlparse.go @@ -2,6 +2,7 @@ package sqlparse import ( "bufio" + "fmt" "io" "strings" ) @@ -37,7 +38,7 @@ func (sp *SqlParser) Parse() (*ExecutionOptions, error) { } if err := scanner.Err(); err != nil { - return nil, err + return nil, fmt.Errorf("parsing sql file encountered an error: %w", err) } return options, nil diff --git a/internal/storage/db.go b/internal/storage/db.go index 9032dc3..e329e71 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -15,16 +15,15 @@ import ( func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid database connection parameters: %w", err) } - // Note: `sql.Open` only validates the connection string we provided - // it is sane. It doesn't actually open up a connection to the database. - // For that, we ping the database to ensure the connection string is fully - // valid. + // Note: `sql.Open` only validates the connection string we provided is sane. + // It doesn't actually open up a connection to the database. For that, we ping + // the database to ensure the connection string is fully valid. err = db.Ping() if err != nil { - return nil, err + return nil, fmt.Errorf("unable to ping database: %w", err) } return db, nil -- cgit v1.2.3 From 32d10125d82aa7933fffac474105bf5897f3853a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 7 Feb 2024 20:05:53 -0500 Subject: Use checkmate 0.3.0 --- internal/configloader/configloader_test.go | 5 +++-- internal/models/migration_test.go | 20 ++++++++++---------- internal/output/console_test.go | 14 +++++++------- 3 files changed, 20 insertions(+), 19 deletions(-) (limited to 'internal') diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 6229cf6..6ab21e0 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -8,6 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" ) func TestNewConfigDefaults(t *testing.T) { @@ -16,8 +17,8 @@ func TestNewConfigDefaults(t *testing.T) { cfg, err := configloader.NewConfig() assert.Nil(t, err) - assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations") - assert.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) + check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") + check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) } func TestNewConfigWithInvalidVersionStyle(t *testing.T) { diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index aa92510..a511c12 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -5,7 +5,7 @@ import ( "time" "github.com/eugenetriguba/bolt/internal/models" - "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" ) func TestNewTimestampMigration(t *testing.T) { @@ -13,21 +13,21 @@ func TestNewTimestampMigration(t *testing.T) { m := models.NewTimestampMigration(timestamp, "test message") - assert.Equal(t, m.Version, "20200101000000") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + check.Equal(t, m.Version, "20200101000000") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) } func TestNewSequentialMigration(t *testing.T) { m := models.NewSequentialMigration(1, "test message") - assert.Equal(t, m.Version, "001") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + check.Equal(t, m.Version, "001") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) m = models.NewSequentialMigration(100, "test message") - assert.Equal(t, m.Version, "100") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + check.Equal(t, m.Version, "100") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) } diff --git a/internal/output/console_test.go b/internal/output/console_test.go index ff1f8b8..78e5d38 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -5,7 +5,7 @@ import ( "io" "testing" - "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" ) func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { @@ -19,8 +19,8 @@ func TestConsoleOutputter_Output(t *testing.T) { consoleOutputter.Output("test string") - assert.Equal(t, stdout.String(), "test string\n") - assert.Equal(t, stderr.String(), "") + check.Equal(t, stdout.String(), "test string\n") + check.Equal(t, stderr.String(), "") } func TestConsoleOutputter_Error(t *testing.T) { @@ -30,8 +30,8 @@ func TestConsoleOutputter_Error(t *testing.T) { consoleOutputter.Error("test string") - assert.Equal(t, stdout.String(), "") - assert.Equal(t, stderr.String(), "test string\n") + check.Equal(t, stdout.String(), "") + check.Equal(t, stderr.String(), "test string\n") } func TestConsoleOutputter_Table(t *testing.T) { @@ -70,7 +70,7 @@ func TestConsoleOutputter_Table(t *testing.T) { consoleOutputter.Table(tc.headers, tc.rows) - assert.Equal(t, stdout.String(), tc.expectedStdout) - assert.Equal(t, stderr.String(), "") + check.Equal(t, stdout.String(), tc.expectedStdout) + check.Equal(t, stderr.String(), "") } } -- cgit v1.2.3 From b3595cd1f9793eaf8c1f80d1cfadbbebe328f0c4 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 7 Feb 2024 21:44:22 -0500 Subject: Fix return value for console outputter output and error funcs --- internal/output/console.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'internal') diff --git a/internal/output/console.go b/internal/output/console.go index 50534dd..7f04b28 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -18,12 +18,18 @@ func NewConsoleOutputter() ConsoleOutputter { func (c ConsoleOutputter) Output(message string) error { _, err := fmt.Fprintln(c.stdout, message) - return fmt.Errorf("unable to print message %s: %w", message, err) + if err != nil { + return fmt.Errorf("unable to print message %s: %w", message, err) + } + return nil } func (c ConsoleOutputter) Error(message string) error { _, err := fmt.Fprintln(c.stderr, message) - return fmt.Errorf("unable to print message %s: %w", message, err) + if err != nil { + return fmt.Errorf("unable to print message %s: %w", message, err) + } + return nil } func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { -- cgit v1.2.3 From ab25e363ed49d234f347d78b51c5dc07f6eebfa7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 7 Feb 2024 21:45:02 -0500 Subject: Move pkg/cli to internal/cli --- internal/cli/cli.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 internal/cli/cli.go (limited to 'internal') diff --git a/internal/cli/cli.go b/internal/cli/cli.go new file mode 100644 index 0000000..29a10ff --- /dev/null +++ b/internal/cli/cli.go @@ -0,0 +1,25 @@ +package cli + +import ( + "context" + "flag" + + "github.com/eugenetriguba/bolt/internal/commands" + "github.com/google/subcommands" +) + +// Run runs the Bolt CLI and returns the exit code. +func Run() int { + subcommands.Register(subcommands.HelpCommand(), "") + subcommands.Register(subcommands.FlagsCommand(), "") + subcommands.Register(subcommands.CommandsCommand(), "") + subcommands.Register(&commands.VersionCmd{}, "") + subcommands.Register(&commands.NewCmd{}, "") + subcommands.Register(&commands.UpCmd{}, "") + subcommands.Register(&commands.DownCmd{}, "") + subcommands.Register(&commands.StatusCmd{}, "") + + flag.Parse() + ctx := context.Background() + return int(subcommands.Execute(ctx)) +} -- cgit v1.2.3 From df5dce753523e0e1eca7de91d272ff16c9c0d79c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:48:14 -0500 Subject: Change Error in console outputter to accept error --- internal/commands/status.go | 10 +++++----- internal/commands/up.go | 12 ++++++------ internal/commands/version.go | 2 +- internal/output/console.go | 7 +++++++ internal/output/console_test.go | 3 ++- 5 files changed, 21 insertions(+), 13 deletions(-) (limited to 'internal') diff --git a/internal/commands/status.go b/internal/commands/status.go index a58d587..85f3824 100644 --- a/internal/commands/status.go +++ b/internal/commands/status.go @@ -39,7 +39,7 @@ func (m *StatusCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -48,20 +48,20 @@ func (m *StatusCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } defer db.Close() migrationDBRepo, err := repositories.NewMigrationDBRepo(db) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -72,7 +72,7 @@ func (m *StatusCmd) Execute( ) migrations, err := migrationService.ListMigrations(services.SortOrderAsc) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } diff --git a/internal/commands/up.go b/internal/commands/up.go index ba74299..5cbc262 100644 --- a/internal/commands/up.go +++ b/internal/commands/up.go @@ -49,7 +49,7 @@ func (cmd *UpCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -58,20 +58,20 @@ func (cmd *UpCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } defer db.Close() migrationDBRepo, err := repositories.NewMigrationDBRepo(db) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -84,13 +84,13 @@ func (cmd *UpCmd) Execute( if cmd.version == "" { err = migrationService.ApplyAllMigrations() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } } else { err = migrationService.ApplyUpToVersion(cmd.version) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } } diff --git a/internal/commands/version.go b/internal/commands/version.go index 36891a2..c9101d0 100644 --- a/internal/commands/version.go +++ b/internal/commands/version.go @@ -34,7 +34,7 @@ func (cmd *VersionCmd) Execute( consoleOutputter := output.NewConsoleOutputter() err := consoleOutputter.Output("bolt v0.2.1") if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } diff --git a/internal/output/console.go b/internal/output/console.go index 7f04b28..42b0e3f 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -24,12 +24,19 @@ func (c ConsoleOutputter) Output(message string) error { return nil } +<<<<<<< Updated upstream func (c ConsoleOutputter) Error(message string) error { _, err := fmt.Fprintln(c.stderr, message) if err != nil { return fmt.Errorf("unable to print message %s: %w", message, err) } return nil +======= +func (c ConsoleOutputter) Error(err error) error { + msg := err.Error() + _, printErr := fmt.Fprintln(c.stderr, msg) + return fmt.Errorf("unable to print message %s: %w", msg, printErr) +>>>>>>> Stashed changes } func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { diff --git a/internal/output/console_test.go b/internal/output/console_test.go index 78e5d38..492d0a2 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -2,6 +2,7 @@ package output import ( "bytes" + "errors" "io" "testing" @@ -28,7 +29,7 @@ func TestConsoleOutputter_Error(t *testing.T) { var stderr bytes.Buffer consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) - consoleOutputter.Error("test string") + consoleOutputter.Error(errors.New("test string")) check.Equal(t, stdout.String(), "") check.Equal(t, stderr.String(), "test string\n") -- cgit v1.2.3 From 29b5f58de7a5f027ea70932bf3c6d4a02de3ffdd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:49:31 -0500 Subject: fix merge conflict --- internal/output/console.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'internal') diff --git a/internal/output/console.go b/internal/output/console.go index 42b0e3f..1a4efad 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -24,19 +24,13 @@ func (c ConsoleOutputter) Output(message string) error { return nil } -<<<<<<< Updated upstream -func (c ConsoleOutputter) Error(message string) error { - _, err := fmt.Fprintln(c.stderr, message) - if err != nil { - return fmt.Errorf("unable to print message %s: %w", message, err) - } - return nil -======= func (c ConsoleOutputter) Error(err error) error { msg := err.Error() _, printErr := fmt.Fprintln(c.stderr, msg) - return fmt.Errorf("unable to print message %s: %w", msg, printErr) ->>>>>>> Stashed changes + if printErr != nil { + return fmt.Errorf("unable to print message %s: %w", msg, printErr) + } + return nil } func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { -- cgit v1.2.3 From b5f52806f29c9debb2248542d7dc6901da76fdc3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:49:49 -0500 Subject: Fix files that weren't saved and pushed up --- internal/commands/down.go | 12 ++++++------ internal/commands/new.go | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'internal') diff --git a/internal/commands/down.go b/internal/commands/down.go index 262e229..2b98011 100644 --- a/internal/commands/down.go +++ b/internal/commands/down.go @@ -49,7 +49,7 @@ func (cmd *DownCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -58,20 +58,20 @@ func (cmd *DownCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } defer db.Close() migrationDBRepo, err := repositories.NewMigrationDBRepo(db) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -84,13 +84,13 @@ func (cmd *DownCmd) Execute( if cmd.version == "" { err = migrationService.RevertAllMigrations() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } } else { err = migrationService.RevertDownToVersion(cmd.version) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } } diff --git a/internal/commands/new.go b/internal/commands/new.go index 0c44248..7298dad 100644 --- a/internal/commands/new.go +++ b/internal/commands/new.go @@ -48,13 +48,13 @@ func (cmd *NewCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } @@ -64,13 +64,13 @@ func (cmd *NewCmd) Execute( consoleOutputter, ) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } err = migrationService.CreateMigration(cfg.Migrations.VersionStyle, cmd.message) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(err) return subcommands.ExitFailure } -- cgit v1.2.3 From 38a2e5f69f46368075072be36fac7bc46b2c064d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:54:55 -0500 Subject: Fix interface --- internal/output/output.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/output/output.go b/internal/output/output.go index c51553b..9a9fb77 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -2,6 +2,6 @@ package output type Outputter interface { Output(message string) error - Error(message string) error + Error(err error) error Table(header []string, rows [][]string) error } -- cgit v1.2.3 From 8fd0e2b96a89b03dcff88a5b4bac5e614b9e4f90 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:56:19 -0500 Subject: Add Name func to Migration model --- internal/models/migration.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'internal') diff --git a/internal/models/migration.go b/internal/models/migration.go index 4007f3c..b63fec9 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -30,3 +30,7 @@ func NewSequentialMigration(version uint64, message string) *Migration { Applied: false, } } + +func (m *Migration) Name() string { + return fmt.Sprintf("%s_%s", m.Version, m.Message) +} -- cgit v1.2.3 From 052bfc3c1f25ff0f4a9506472c50857dc07a8664 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:56:37 -0500 Subject: Add error handling to unhandled errors during migration apply/revert --- internal/services/migration.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'internal') diff --git a/internal/services/migration.go b/internal/services/migration.go index 6836980..b6b00bb 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -41,7 +41,14 @@ func (ms *MigrationService) ApplyAllMigrations() error { for _, migration := range migrations { if !migration.Applied { - ms.ApplyMigration(migration) + err := ms.ApplyMigration(migration) + if err != nil { + return fmt.Errorf( + "unable to apply migration %s: %w", + migration.Name(), + err, + ) + } } } @@ -75,7 +82,11 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error { if !migration.Applied { err := ms.ApplyMigration(migration) if err != nil { - return err + return fmt.Errorf( + "unable to apply migration %s: %w", + migration.Name(), + err, + ) } } @@ -124,7 +135,14 @@ func (ms *MigrationService) RevertAllMigrations() error { for _, migration := range migrations { if migration.Applied { - ms.RevertMigration(migration) + err := ms.RevertMigration(migration) + if err != nil { + return fmt.Errorf( + "unable to revert migration %s: %w", + migration.Name(), + err, + ) + } } } @@ -158,7 +176,11 @@ func (ms *MigrationService) RevertDownToVersion(version string) error { if migration.Applied { err := ms.RevertMigration(migration) if err != nil { - return err + return fmt.Errorf( + "unable to revert migration %s: %w", + migration.Name(), + err, + ) } } -- cgit v1.2.3 From ed987d2d21e4063fcdb83e86d9f87b08f8e2165a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 16:03:14 -0500 Subject: Add better error handling for db connect --- internal/storage/db.go | 10 ++++++++-- internal/storage/db_test.go | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/storage/db.go b/internal/storage/db.go index e329e71..26d3b3b 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -2,12 +2,18 @@ package storage import ( "database/sql" + "errors" "fmt" "github.com/eugenetriguba/bolt/internal/configloader" _ "github.com/lib/pq" ) +var ( + ErrInvalidConnectionString = errors.New("invalid database connection parameters provided") + ErrUnableToConnect = errors.New("unable to open connection to database") +) + // DBConnect establishes a connection to the database using the driver // and connection information. // @@ -15,7 +21,7 @@ import ( func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, fmt.Errorf("invalid database connection parameters: %w", err) + return nil, fmt.Errorf("%w: %v", ErrInvalidConnectionString, err) } // Note: `sql.Open` only validates the connection string we provided is sane. @@ -23,7 +29,7 @@ func DBConnect(driver string, connectionParams string) (*sql.DB, error) { // the database to ensure the connection string is fully valid. err = db.Ping() if err != nil { - return nil, fmt.Errorf("unable to ping database: %w", err) + return nil, fmt.Errorf("%w: %v", ErrUnableToConnect, err) } return db, nil diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index 61a9bf4..c871c47 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -10,7 +10,7 @@ import ( "github.com/eugenetriguba/checkmate/assert" ) -func TestDBConnectWithPostgres(t *testing.T) { +func TestDBConnect_Success(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) @@ -21,15 +21,17 @@ func TestDBConnectWithPostgres(t *testing.T) { assert.Nil(t, err) } -func TestDBConnectMalformedConnectionString(t *testing.T) { +func TestDBConnect_BadConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") + assert.ErrorIs(t, err, storage.ErrUnableToConnect) assert.ErrorContains(t, err, "connection refused") } -func TestDBConnectUnsupportedDriver(t *testing.T) { +func TestDBConnect_UnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.ErrorIs(t, err, storage.ErrInvalidConnectionString) assert.ErrorContains(t, err, `unknown driver "redis"`) } -- cgit v1.2.3 From 46f7f49ebeb7f4e8fa1f395805a0b14baee5e5bc Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 16:08:04 -0500 Subject: Tweak error and doc for storage module --- internal/storage/db.go | 14 +++++++++----- internal/storage/db_test.go | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'internal') diff --git a/internal/storage/db.go b/internal/storage/db.go index 26d3b3b..02683b4 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -10,18 +10,22 @@ import ( ) var ( - ErrInvalidConnectionString = errors.New("invalid database connection parameters provided") - ErrUnableToConnect = errors.New("unable to open connection to database") + ErrMalformedConnectionString = errors.New("malformed database connection parameters provided") + ErrUnableToConnect = errors.New("unable to open connection to database") ) // DBConnect establishes a connection to the database using the driver -// and connection information. +// and connection information. Only "postgres" is supported as the driver. // -// Note that only "postgres" is supported as the driver right now. +// The following errors may be returned: +// - ErrMalformedConnectionString: The provided connection parameters are +// not in a valid format. +// - ErrUnableToConnect: Unable to make a connection to the database with +// the provided connection parameters. func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, fmt.Errorf("%w: %v", ErrInvalidConnectionString, err) + return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, err) } // Note: `sql.Open` only validates the connection string we provided is sane. diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index c871c47..e85cc57 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -31,7 +31,7 @@ func TestDBConnect_UnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - assert.ErrorIs(t, err, storage.ErrInvalidConnectionString) + assert.ErrorIs(t, err, storage.ErrMalformedConnectionString) assert.ErrorContains(t, err, `unknown driver "redis"`) } -- cgit v1.2.3 From bf1d3b3e1fc42132678b244e479839416a03a6ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 29 Feb 2024 19:53:20 -0500 Subject: Add service tests --- internal/bolttest/mock_migration_db_repo.go | 78 ++++ internal/bolttest/mock_migration_fs_repo.go | 82 ++++ internal/bolttest/null_outputer.go | 19 + internal/commands/down.go | 28 +- internal/commands/new.go | 16 +- internal/commands/status.go | 34 +- internal/commands/up.go | 28 +- internal/configloader/configloader.go | 2 +- internal/configloader/configloader_test.go | 2 +- internal/models/migration_test.go | 6 + internal/repositories/migration_db_repo.go | 235 +++++----- internal/repositories/migration_db_repo_test.go | 2 +- internal/repositories/migration_fs_repo.go | 40 +- internal/services/migration.go | 233 ++++++++-- internal/services/migration_test.go | 595 ++++++++++++++++++++++++ internal/sqlparse/sqlparse.go | 18 +- internal/sqlparse/sqlparse_test.go | 24 +- internal/storage/db.go | 6 +- 18 files changed, 1194 insertions(+), 254 deletions(-) create mode 100644 internal/bolttest/mock_migration_db_repo.go create mode 100644 internal/bolttest/mock_migration_fs_repo.go create mode 100644 internal/bolttest/null_outputer.go create mode 100644 internal/services/migration_test.go (limited to 'internal') diff --git a/internal/bolttest/mock_migration_db_repo.go b/internal/bolttest/mock_migration_db_repo.go new file mode 100644 index 0000000..bdcc928 --- /dev/null +++ b/internal/bolttest/mock_migration_db_repo.go @@ -0,0 +1,78 @@ +package bolttest + +import "github.com/eugenetriguba/bolt/internal/models" + +type MockMigrationDBRepo struct { + ListReturnValue ListReturnValue + ListCallCount int + IsAppliedReturnValue IsAppliedReturnValue + IsAppliedCallCount int + ApplyReturnValue ApplyReturnValue + ApplyCallCount int + ApplyWithTxReturnValue ApplyWithTxReturnValue + ApplyWithTxCallCount int + RevertReturnValue RevertReturnValue + RevertCallCount int + RevertWithTxReturnValue RevertWithTxReturnValue + RevertWithTxCallCount int +} + +type ListReturnValue struct { + Migrations map[string]*models.Migration + Err error +} + +type IsAppliedReturnValue struct { + IsApplied bool + Err error +} + +type ApplyReturnValue struct { + Err error +} + +type ApplyWithTxReturnValue = ApplyReturnValue +type RevertReturnValue = ApplyReturnValue +type RevertWithTxReturnValue = ApplyReturnValue + +func (repo *MockMigrationDBRepo) List() (map[string]*models.Migration, error) { + repo.ListCallCount += 1 + return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err +} + +func (repo *MockMigrationDBRepo) IsApplied(version string) (bool, error) { + repo.IsAppliedCallCount += 1 + return repo.IsAppliedReturnValue.IsApplied, repo.IsAppliedReturnValue.Err +} + +func (repo *MockMigrationDBRepo) Apply( + upgradeScript string, + migration *models.Migration, +) error { + repo.ApplyCallCount += 1 + return repo.ApplyReturnValue.Err +} + +func (repo *MockMigrationDBRepo) ApplyWithTx( + upgradeScript string, + migration *models.Migration, +) error { + repo.ApplyWithTxCallCount += 1 + return repo.ApplyWithTxReturnValue.Err +} + +func (repo *MockMigrationDBRepo) Revert( + downgradeScript string, + migration *models.Migration, +) error { + repo.RevertCallCount += 1 + return repo.RevertReturnValue.Err +} + +func (repo *MockMigrationDBRepo) RevertWithTx( + downgradeScript string, + migration *models.Migration, +) error { + repo.RevertWithTxCallCount += 1 + return repo.RevertWithTxReturnValue.Err +} diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go new file mode 100644 index 0000000..0932b37 --- /dev/null +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -0,0 +1,82 @@ +package bolttest + +import "github.com/eugenetriguba/bolt/internal/models" + +type MockMigrationFsRepo struct { + CreateReturnValue CreateReturnValue + CreateCallCount int + ExistsReturnValue ExistsReturnValue + ExistsCallCount int + GetReturnValue GetReturnValue + GetCallCount int + ListReturnValue ListReturnValue + ListCallCount int + LatestReturnValue LatestReturnValue + LatestCallCount int + ReadUpgradeScriptReturnValue ReadUpgradeScriptReturnValue + ReadUpgradeScriptCallCount int + ReadDowngradeScriptReturnValue ReadDowngradeScriptReturnValue + ReadDowngradeScriptCallCount int +} + +type CreateReturnValue struct { + Err error +} + +type ExistsReturnValue struct { + Exists bool + Err error +} + +type GetReturnValue struct { + Migration *models.Migration + Err error +} + +type LatestReturnValue = GetReturnValue + +type ReadUpgradeScriptReturnValue struct { + ScriptContents string + Err error +} + +type ReadDowngradeScriptReturnValue = ReadUpgradeScriptReturnValue + +func (repo *MockMigrationFsRepo) Create(migration *models.Migration) error { + repo.CreateCallCount += 1 + return repo.CreateReturnValue.Err +} + +func (repo *MockMigrationFsRepo) Exists(version string) (bool, error) { + repo.ExistsCallCount += 1 + return repo.ExistsReturnValue.Exists, repo.ExistsReturnValue.Err +} + +func (repo *MockMigrationFsRepo) Get(version string) (*models.Migration, error) { + repo.GetCallCount += 1 + return repo.GetReturnValue.Migration, repo.GetReturnValue.Err +} + +func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) { + repo.ListCallCount += 1 + return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err +} + +func (repo *MockMigrationFsRepo) Latest() (*models.Migration, error) { + repo.LatestCallCount += 1 + return repo.LatestReturnValue.Migration, repo.LatestReturnValue.Err +} + +func (repo *MockMigrationFsRepo) ReadUpgradeScript( + migration *models.Migration, +) (string, error) { + repo.ReadUpgradeScriptCallCount += 1 + return repo.ReadUpgradeScriptReturnValue.ScriptContents, repo.ReadUpgradeScriptReturnValue.Err +} + +func (repo *MockMigrationFsRepo) ReadDowngradeScript( + migration *models.Migration, +) (string, error) { + repo.ReadDowngradeScriptCallCount += 1 + return repo.ReadDowngradeScriptReturnValue.ScriptContents, repo.ReadDowngradeScriptReturnValue.Err +} diff --git a/internal/bolttest/null_outputer.go b/internal/bolttest/null_outputer.go new file mode 100644 index 0000000..f70b002 --- /dev/null +++ b/internal/bolttest/null_outputer.go @@ -0,0 +1,19 @@ +package bolttest + +type NullOutputter struct { + OutputLogs []string + ErrorLogs []string + TableLogs []string +} + +func (o NullOutputter) Output(message string) error { + return nil +} + +func (o NullOutputter) Error(err error) error { + return nil +} + +func (o NullOutputter) Table(header []string, rows [][]string) error { + return nil +} diff --git a/internal/commands/down.go b/internal/commands/down.go index 2b98011..8e88fc7 100644 --- a/internal/commands/down.go +++ b/internal/commands/down.go @@ -3,10 +3,10 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" - "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -49,7 +49,7 @@ func (cmd *DownCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -58,39 +58,31 @@ func (cmd *DownCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err)) return subcommands.ExitFailure } defer db.Close() - migrationDBRepo, err := repositories.NewMigrationDBRepo(db) - if err != nil { - consoleOutputter.Error(err) - return subcommands.ExitFailure - } - - migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) + migrationService, err := services.NewMigrationServiceFromConfig( + db, + consoleOutputter, + *cfg, + ) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } - migrationService := services.NewMigrationService( - migrationDBRepo, - migrationFsRepo, - consoleOutputter, - ) - if cmd.version == "" { err = migrationService.RevertAllMigrations() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to revert all migrations: %w", err)) return subcommands.ExitFailure } } else { err = migrationService.RevertDownToVersion(cmd.version) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to revert migrations down to %s: %w", cmd.version, err)) return subcommands.ExitFailure } } diff --git a/internal/commands/new.go b/internal/commands/new.go index 7298dad..96ddd98 100644 --- a/internal/commands/new.go +++ b/internal/commands/new.go @@ -3,6 +3,7 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" @@ -48,29 +49,28 @@ func (cmd *NewCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error( + fmt.Errorf("unable to setup local migrations directory: %w", err), + ) return subcommands.ExitFailure } migrationService := services.NewMigrationService( nil, migrationFsRepo, + *cfg, consoleOutputter, ) - if err != nil { - consoleOutputter.Error(err) - return subcommands.ExitFailure - } - err = migrationService.CreateMigration(cfg.Migrations.VersionStyle, cmd.message) + _, err = migrationService.CreateMigration(cmd.message) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to create new migration: %w", err)) return subcommands.ExitFailure } diff --git a/internal/commands/status.go b/internal/commands/status.go index 85f3824..eaf4332 100644 --- a/internal/commands/status.go +++ b/internal/commands/status.go @@ -3,10 +3,10 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" - "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -39,7 +39,7 @@ func (m *StatusCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -48,31 +48,24 @@ func (m *StatusCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err)) return subcommands.ExitFailure } defer db.Close() - migrationDBRepo, err := repositories.NewMigrationDBRepo(db) - if err != nil { - consoleOutputter.Error(err) - return subcommands.ExitFailure - } - - migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) + migrationService, err := services.NewMigrationServiceFromConfig( + db, + consoleOutputter, + *cfg, + ) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } - migrationService := services.NewMigrationService( - migrationDBRepo, - migrationFsRepo, - consoleOutputter, - ) migrations, err := migrationService.ListMigrations(services.SortOrderAsc) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to list migrations: %w", err)) return subcommands.ExitFailure } @@ -95,6 +88,13 @@ func (m *StatusCmd) Execute( rows[i] = []string{migration.Version, migration.Message, applied} } - consoleOutputter.Table(headers, rows) + err = consoleOutputter.Table(headers, rows) + if err != nil { + consoleOutputter.Error( + fmt.Errorf("unable to output migrations as table: %w", err), + ) + return subcommands.ExitFailure + } + return subcommands.ExitSuccess } diff --git a/internal/commands/up.go b/internal/commands/up.go index 5cbc262..e10386a 100644 --- a/internal/commands/up.go +++ b/internal/commands/up.go @@ -3,10 +3,10 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" - "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -49,7 +49,7 @@ func (cmd *UpCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -58,39 +58,31 @@ func (cmd *UpCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err)) return subcommands.ExitFailure } defer db.Close() - migrationDBRepo, err := repositories.NewMigrationDBRepo(db) - if err != nil { - consoleOutputter.Error(err) - return subcommands.ExitFailure - } - - migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) + migrationService, err := services.NewMigrationServiceFromConfig( + db, + consoleOutputter, + *cfg, + ) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } - migrationService := services.NewMigrationService( - migrationDBRepo, - migrationFsRepo, - consoleOutputter, - ) - if cmd.version == "" { err = migrationService.ApplyAllMigrations() if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to apply all migrations: %w", err)) return subcommands.ExitFailure } } else { err = migrationService.ApplyUpToVersion(cmd.version) if err != nil { - consoleOutputter.Error(err) + consoleOutputter.Error(fmt.Errorf("unable to apply migrations up to %s: %w", cmd.version, err)) return subcommands.ExitFailure } } diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index 6155d56..c2c05e7 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -71,7 +71,7 @@ func NewConfig() (*Config, error) { cfg := Config{ Migrations: MigrationsConfig{ DirectoryPath: "migrations", - VersionStyle: VersionStyleSequential, + VersionStyle: VersionStyleTimestamp, }, } if !errors.Is(err, ErrConfigFileNotFound) { diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 6ab21e0..0ad6930 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -18,7 +18,7 @@ func TestNewConfigDefaults(t *testing.T) { assert.Nil(t, err) check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") - check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) + check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp) } func TestNewConfigWithInvalidVersionStyle(t *testing.T) { diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index a511c12..b6d3205 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -1,6 +1,7 @@ package models_test import ( + "fmt" "testing" "time" @@ -31,3 +32,8 @@ func TestNewSequentialMigration(t *testing.T) { check.Equal(t, m.Message, "test message") check.Equal(t, m.Applied, false) } + +func TestName(t *testing.T) { + m := models.NewSequentialMigration(1, "test message") + check.Equal(t, m.Name(), fmt.Sprintf("%s_%s", m.Version, m.Message)) +} diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index b9e2d78..500fea4 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -7,10 +7,22 @@ import ( "strings" "github.com/eugenetriguba/bolt/internal/models" - "github.com/eugenetriguba/bolt/internal/sqlparse" ) -type MigrationDBRepo struct { +type SQLExecutor interface { + Exec(query string, args ...interface{}) (sql.Result, error) +} + +type MigrationDBRepo interface { + List() (map[string]*models.Migration, error) + IsApplied(version string) (bool, error) + Apply(upgradeScript string, migration *models.Migration) error + ApplyWithTx(upgradeScript string, migration *models.Migration) error + Revert(downgradeScript string, migration *models.Migration) error + RevertWithTx(downgradeScript string, migration *models.Migration) error +} + +type migrationDBRepo struct { db *sql.DB } @@ -18,7 +30,7 @@ type MigrationDBRepo struct { // database. Furthermore, it ensures the migration table it // operates on exists. If it is unable to create or confirm // the table exists, an error is returned. -func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { +func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS bolt_migrations( version CHARACTER(14) PRIMARY KEY NOT NULL @@ -31,14 +43,14 @@ func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { ) } - return &MigrationDBRepo{db: db}, nil + return &migrationDBRepo{db: db}, nil } // List retrieves a map of migration models that can be // looked up by their `Version`. All migrations retrieved // will be ones that have been applied, and their message // will always be an empty string. -func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { +func (mr migrationDBRepo) List() (map[string]*models.Migration, error) { rows, err := mr.db.Query(`SELECT version FROM bolt_migrations;`) if err != nil { return nil, fmt.Errorf( @@ -49,7 +61,7 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { } defer rows.Close() - var migrations = make(map[string]*models.Migration) + var migrations = make(map[string]*models.Migration, 0) for rows.Next() { var version string err := rows.Scan(&version) @@ -78,15 +90,19 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { // applied will be false when the version isn't applied and // when the version might be applied, but there was an error. // Check err first before looking at whether the version is applied. -func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) { +func (mr migrationDBRepo) IsApplied(version string) (bool, error) { row := mr.db.QueryRow(`SELECT 1 FROM bolt_migrations WHERE version = $1`, version) var scanResult int - err = row.Scan(&scanResult) + err := row.Scan(&scanResult) if errors.Is(err, sql.ErrNoRows) { return false, nil } if err != nil { - return false, err + return false, fmt.Errorf( + "unable to check whether the migration %s is applied: %w", + version, + err, + ) } return true, nil } @@ -95,134 +111,143 @@ func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) { // and adding the applied migration version into the migrations table. When // successfully applied, the `migration` model's `Applied` field will be set // to true. -func (mr *MigrationDBRepo) Apply( +func (mr migrationDBRepo) Apply( upgradeScript string, migration *models.Migration, ) error { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript)) - execOptions, err := sqlParser.Parse() + err := applyMigration(mr.db, upgradeScript, *migration) if err != nil { - return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) + return err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return fmt.Errorf( - "unable to start transaction during upgrade script execution: %w", - err, - ) - } - defer tx.Rollback() - - _, err = tx.Exec(upgradeScript) - if err != nil { - return fmt.Errorf("unable to execute upgrade script: %w", err) - } + migration.Applied = true + return nil +} - _, err = tx.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, +// ApplyWithTx applies a migration like Apply. However, it +// wraps the operation is a database transaction. +func (mr migrationDBRepo) ApplyWithTx( + upgradeScript string, + migration *models.Migration, +) error { + tx, err := mr.db.Begin() + if err != nil { + return fmt.Errorf( + "unable to start transaction during upgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to insert applied migration to bolt_migrations table: %w", - err, - ) - } - err = tx.Commit() - if err != nil { - return fmt.Errorf( - "unable to commit transaction during upgrade script execution: %w", - err, - ) - } - } else { - _, err = mr.db.Exec(upgradeScript) - if err != nil { - return fmt.Errorf("unable to execute upgrade script: %w", err) - } + } + defer tx.Rollback() - _, err = mr.db.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, + err = applyMigration(tx, upgradeScript, *migration) + if err != nil { + return err + } + + err = tx.Commit() + if err != nil { + return fmt.Errorf( + "unable to commit transaction during upgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to insert applied migration to bolt_migrations table: %w", - err, - ) - } } migration.Applied = true return nil } +func applyMigration( + executor SQLExecutor, + upgradeScript string, + migration models.Migration, +) error { + _, err := executor.Exec(upgradeScript) + if err != nil { + return fmt.Errorf("unable to execute upgrade script: %w", err) + } + + _, err = executor.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + if err != nil { + return fmt.Errorf( + "unable to insert applied migration: %w", + err, + ) + } + + return nil +} + // Revert reverts a migration by executing the corresponding downgrade script // and deleting the migration version from the migrations table. When successfully // reverted, the `migration` model's `Applied` field will be set to false. -func (mr *MigrationDBRepo) Revert( +func (mr migrationDBRepo) Revert( downgradeScript string, migration *models.Migration, ) error { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript)) - execOptions, err := sqlParser.Parse() + err := revertMigration(mr.db, downgradeScript, *migration) if err != nil { - return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) + return err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return fmt.Errorf( - "unable to start transaction during downgrade script execution: %w", - err, - ) - } - defer tx.Rollback() - - _, err = tx.Exec(downgradeScript) - if err != nil { - return fmt.Errorf("unable to execute downgrade script: %w", err) - } + migration.Applied = false + return nil +} - _, err = tx.Exec( - `DELETE FROM bolt_migrations WHERE version = $1;`, - migration.Version, +// RevertWithTx reverts a migration like Revert. However, it +// wraps the operation is a database transaction. +func (mr migrationDBRepo) RevertWithTx( + downgradeScript string, + migration *models.Migration, +) error { + tx, err := mr.db.Begin() + if err != nil { + return fmt.Errorf( + "unable to start transaction during downgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to remove reverted migration from bolt_migrations table: %w", - err, - ) - } + } + defer tx.Rollback() - err = tx.Commit() - if err != nil { - return fmt.Errorf( - "unable to commit transaction during downgrade script execution: %w", - err, - ) - } - } else { - _, err = mr.db.Exec(downgradeScript) - if err != nil { - return fmt.Errorf("unable to execute downgrade script: %w", err) - } + err = revertMigration(tx, downgradeScript, *migration) + if err != nil { + return err + } - _, err = mr.db.Exec( - `DELETE FROM bolt_migrations WHERE version = $1;`, - migration.Version, + err = tx.Commit() + if err != nil { + return fmt.Errorf( + "unable to commit transaction during downgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to remove reverted migration from bolt_migrations table: %w", - err, - ) - } } migration.Applied = false return nil } + +func revertMigration( + executor SQLExecutor, + downgradeScript string, + migration models.Migration, +) error { + _, err := executor.Exec(downgradeScript) + if err != nil { + return fmt.Errorf("unable to execute downgrade script: %w", err) + } + + _, err = executor.Exec( + `DELETE FROM bolt_migrations WHERE version = $1;`, + migration.Version, + ) + if err != nil { + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) + } + + return nil +} diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 4e41b36..9f4f9e8 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -197,7 +197,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. err = repo.Apply( - `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, + `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) assert.Nil(t, err) diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index c541006..9d21c59 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -23,13 +23,23 @@ func (e *ErrIsNotDir) Error() string { ) } -type MigrationFsRepo struct { +type MigrationFsRepo interface { + Create(migration *models.Migration) error + Exists(version string) (bool, error) + Get(version string) (*models.Migration, error) + List() (map[string]*models.Migration, error) + Latest() (*models.Migration, error) + ReadUpgradeScript(migration *models.Migration) (string, error) + ReadDowngradeScript(migration *models.Migration) (string, error) +} + +type migrationFsRepo struct { migrationsDirPath string } func NewMigrationFsRepo( migrationsConfig *configloader.MigrationsConfig, -) (*MigrationFsRepo, error) { +) (MigrationFsRepo, error) { fileInfo, err := os.Stat(migrationsConfig.DirectoryPath) if errors.Is(err, os.ErrNotExist) { err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755) @@ -42,10 +52,10 @@ func NewMigrationFsRepo( return nil, &ErrIsNotDir{path: migrationsConfig.DirectoryPath} } - return &MigrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil + return &migrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil } -func (mr *MigrationFsRepo) Create(migration *models.Migration) error { +func (mr migrationFsRepo) Create(migration *models.Migration) error { newMigrationDir := filepath.Join(mr.migrationsDirPath, mr.migrationDirname(migration)) err := os.Mkdir(newMigrationDir, 0755) @@ -66,7 +76,7 @@ func (mr *MigrationFsRepo) Create(migration *models.Migration) error { return nil } -func (mr *MigrationFsRepo) Exists(version string) (bool, error) { +func (mr migrationFsRepo) Exists(version string) (bool, error) { migrations, err := mr.List() if err != nil { return false, err @@ -76,7 +86,7 @@ func (mr *MigrationFsRepo) Exists(version string) (bool, error) { return ok, nil } -func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) { +func (mr migrationFsRepo) Get(version string) (*models.Migration, error) { migrations, err := mr.List() if err != nil { return nil, err @@ -90,7 +100,7 @@ func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) { return migration, nil } -func (mr *MigrationFsRepo) Latest() (*models.Migration, error) { +func (mr migrationFsRepo) Latest() (*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { return nil, err @@ -103,13 +113,13 @@ func (mr *MigrationFsRepo) Latest() (*models.Migration, error) { return nil, nil } -func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) { +func (mr migrationFsRepo) List() (map[string]*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { return nil, err } - var migrations = make(map[string]*models.Migration) + var migrations = make(map[string]*models.Migration, 0) for _, entry := range entries { migration, err := dirEntryToMigration(entry) if err != nil { @@ -137,9 +147,7 @@ func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) { }, nil } -func (mr *MigrationFsRepo) ReadUpgradeScript( - migration *models.Migration, -) (string, error) { +func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) { upgradeScriptPath := filepath.Join( mr.migrationsDirPath, mr.migrationDirname(migration), @@ -148,7 +156,7 @@ func (mr *MigrationFsRepo) ReadUpgradeScript( return mr.readScriptContents(upgradeScriptPath) } -func (mr *MigrationFsRepo) ReadDowngradeScript( +func (mr migrationFsRepo) ReadDowngradeScript( migration *models.Migration, ) (string, error) { downgradeScriptPath := filepath.Join( @@ -159,7 +167,7 @@ func (mr *MigrationFsRepo) ReadDowngradeScript( return mr.readScriptContents(downgradeScriptPath) } -func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error) { +func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) { contents, err := os.ReadFile(scriptPath) if err != nil { return "", err @@ -170,13 +178,13 @@ func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error) // migrationDirname creates the directory name that // should be used for this migration. -func (mr *MigrationFsRepo) migrationDirname(migration *models.Migration) string { +func (mr migrationFsRepo) migrationDirname(migration *models.Migration) string { return fmt.Sprintf("%s_%s", migration.Version, mr.normalizeMessage(migration)) } // normalizeMessage normalizes the Message of a migration // to be filesystem friendly. -func (mr *MigrationFsRepo) normalizeMessage(migration *models.Migration) string { +func (mr migrationFsRepo) normalizeMessage(migration *models.Migration) string { message := strings.ToLower(migration.Message) message = strings.TrimSpace(message) message = strings.ReplaceAll(message, " ", "_") diff --git a/internal/services/migration.go b/internal/services/migration.go index b6b00bb..48859d6 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -1,29 +1,62 @@ package services import ( + "database/sql" "fmt" "sort" "strconv" + "strings" "time" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/output" "github.com/eugenetriguba/bolt/internal/repositories" + "github.com/eugenetriguba/bolt/internal/sqlparse" ) type MigrationService struct { - dbRepo *repositories.MigrationDBRepo - fsRepo *repositories.MigrationFsRepo + dbRepo repositories.MigrationDBRepo + fsRepo repositories.MigrationFsRepo + cfg configloader.Config outputter output.Outputter } func NewMigrationService( - dbRepo *repositories.MigrationDBRepo, - fsRepo *repositories.MigrationFsRepo, + dbRepo repositories.MigrationDBRepo, + fsRepo repositories.MigrationFsRepo, + cfg configloader.Config, outputter output.Outputter, -) *MigrationService { - return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter} +) MigrationService { + return MigrationService{ + dbRepo: dbRepo, + fsRepo: fsRepo, + cfg: cfg, + outputter: outputter, + } +} + +func NewMigrationServiceFromConfig( + db *sql.DB, + outputter output.Outputter, + cfg configloader.Config, +) (MigrationService, error) { + migrationDBRepo, err := repositories.NewMigrationDBRepo(db) + if err != nil { + return MigrationService{}, err + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) + if err != nil { + return MigrationService{}, err + } + + return NewMigrationService( + migrationDBRepo, + migrationFsRepo, + cfg, + outputter, + ), nil } type sortOrder int @@ -33,7 +66,7 @@ const ( SortOrderAsc ) -func (ms *MigrationService) ApplyAllMigrations() error { +func (ms MigrationService) ApplyAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err @@ -55,7 +88,7 @@ func (ms *MigrationService) ApplyAllMigrations() error { return nil } -func (ms *MigrationService) ApplyUpToVersion(version string) error { +func (ms MigrationService) ApplyUpToVersion(version string) error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err @@ -98,7 +131,7 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error { return nil } -func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { +func (ms MigrationService) ApplyMigration(migration *models.Migration) error { ms.outputter.Output( fmt.Sprintf( "Applying migration %s_%s..", @@ -112,10 +145,22 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { return err } - err = ms.dbRepo.Apply(scriptContents, migration) + sqlParser := sqlparse.NewSqlParser() + execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) if err != nil { - return err + return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) } + + if execOptions.UseTransaction { + err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + } else { + err = ms.dbRepo.Apply(scriptContents, migration) + } + + if err != nil { + return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err) + } + ms.outputter.Output( fmt.Sprintf( "Successfully applied migration %s_%s!", @@ -127,7 +172,7 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) RevertAllMigrations() error { +func (ms MigrationService) RevertAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return err @@ -149,7 +194,7 @@ func (ms *MigrationService) RevertAllMigrations() error { return nil } -func (ms *MigrationService) RevertDownToVersion(version string) error { +func (ms MigrationService) RevertDownToVersion(version string) error { migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return err @@ -192,7 +237,7 @@ func (ms *MigrationService) RevertDownToVersion(version string) error { return nil } -func (ms *MigrationService) RevertMigration(migration *models.Migration) error { +func (ms MigrationService) RevertMigration(migration *models.Migration) error { ms.outputter.Output( fmt.Sprintf( "Reverting migration %s_%s..", @@ -205,10 +250,22 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return err } - err = ms.dbRepo.Revert(scriptContents, migration) + sqlParser := sqlparse.NewSqlParser() + execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) + if err != nil { + return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) + } + + if execOptions.UseTransaction { + err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + } else { + err = ms.dbRepo.Revert(scriptContents, migration) + } + if err != nil { return err } + ms.outputter.Output( fmt.Sprintf( "Successfully reverted migration %s_%s!", @@ -220,70 +277,152 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration( - versionStyle configloader.VersionStyle, - message string, -) error { +func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) { var migration *models.Migration - if versionStyle == configloader.VersionStyleTimestamp { + if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp { migration = models.NewTimestampMigration(time.Now(), message) } else { - var currentVerison uint64 = 0 - latestMigration, err := ms.fsRepo.Latest() + currentVersion, err := ms.getCurrentSequentialMigrationVersion() if err != nil { - return err + return nil, err } - if latestMigration != nil { - currentVerison, err = strconv.ParseUint(latestMigration.Version, 10, 64) - if err != nil { - return err - } - } - migration = models.NewSequentialMigration(currentVerison+1, message) + migration = models.NewSequentialMigration(currentVersion+1, message) } err := ms.fsRepo.Create(migration) if err != nil { - return err + return nil, err } ms.outputter.Output( fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message), ) - return nil + return migration, nil +} + +func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { + var currentVersion uint64 = 0 + + latestMigration, err := ms.fsRepo.Latest() + if err != nil { + return 0, err + } + + if latestMigration != nil { + currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) + if err != nil { + return 0, err + } + } + + return currentVersion, nil } -func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) { +func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) { // Assumption: All migration versions that have been applied // to the database exist locally in the list of filesystem migrations. // This would hold true unless someone decided to apply a migration and, - // for whatever reason, delete it later from their filesystem. - migrations, err := ms.fsRepo.List() + // for whatever reason, delete it later from their filesystem. If there is + // an applied migration that doesn't exist locally, it would not be shown + // here. + localMigrations, err := ms.fsRepo.List() if err != nil { - return nil, err + return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err) } appliedMigrations, err := ms.dbRepo.List() if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to list out applied migrations from remote db: %w", + err, + ) } - var flatMigrations []*models.Migration - for _, migration := range migrations { - _, ok := appliedMigrations[migration.Version] + migrations := make([]*models.Migration, 0) + for _, localMigration := range localMigrations { + _, ok := appliedMigrations[localMigration.Version] if ok { - migration.Applied = true + localMigration.Applied = true } - flatMigrations = append(flatMigrations, migration) + migrations = append(migrations, localMigration) } - sort.Slice(flatMigrations, func(i, j int) bool { - if order == SortOrderAsc { - return flatMigrations[i].Version < flatMigrations[j].Version + err = ms.sortMigrations(migrations, order) + if err != nil { + return nil, err + } + + return migrations, err +} + +func (ms MigrationService) sortMigrations(migrations []*models.Migration, order sortOrder) error { + sortErrs := make([]error, 0) + sort.Slice(migrations, func(i, j int) bool { + var comparison bool + var err error + + if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential { + comparison, err = ms.compareSequentialMigrations( + migrations[i], + migrations[j], + order, + ) } else { - return flatMigrations[i].Version > flatMigrations[j].Version + comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order) } + + if err != nil { + sortErrs = append(sortErrs, err) + } + + return comparison }) + if len(sortErrs) != 0 { + return fmt.Errorf("unable to sort migrations: %v", sortErrs) + } + return nil +} + +func (ms MigrationService) compareSequentialMigrations( + m1 *models.Migration, + m2 *models.Migration, + order sortOrder, +) (bool, error) { + m1Version, err := strconv.ParseInt(m1.Version, 10, 64) + if err != nil { + return false, err + } + + m2Version, err := strconv.ParseInt(m2.Version, 10, 64) + if err != nil { + return false, err + } - return flatMigrations, nil + if order == SortOrderAsc { + return m1Version < m2Version, nil + } else { + return m1Version > m2Version, nil + } +} + +func (ms MigrationService) compareTimestampMigrations( + m1 *models.Migration, + m2 *models.Migration, + order sortOrder, +) (bool, error) { + m1Version, err := time.Parse("20060102150405", m1.Version) + if err != nil { + return false, err + } + + m2Version, err := time.Parse("20060102150405", m2.Version) + if err != nil { + return false, err + } + + if order == SortOrderAsc { + return m1Version.Before(m2Version), nil + } else { + return m1Version.After(m2Version), nil + } } diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go new file mode 100644 index 0000000..73045ef --- /dev/null +++ b/internal/services/migration_test.go @@ -0,0 +1,595 @@ +package services + +import ( + "errors" + "testing" + "time" + + "github.com/eugenetriguba/bolt/internal/bolttest" + "github.com/eugenetriguba/bolt/internal/configloader" + "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" +) + +func TestListMigrations(t *testing.T) { + type test struct { + localFilesystemMigrations []*models.Migration + remoteAppliedMigrations []*models.Migration + cfg configloader.Config + sortOrder sortOrder + expectedMigrations []*models.Migration + } + + tests := []test{ + // Ensure no local or remote applied migrations means none are returned. + { + localFilesystemMigrations: []*models.Migration{}, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{}, + }, + // Ensure one local fs migration shows up as expected. + { + localFilesystemMigrations: []*models.Migration{ + {Version: "001", Applied: false}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "001", Applied: false}, + }, + }, + // Ensure that one local fs migration and one remote applied migration + // for the same version returns one applied migration. + { + localFilesystemMigrations: []*models.Migration{ + {Version: "001", Applied: false}, + }, + remoteAppliedMigrations: []*models.Migration{ + {Version: "001", Applied: true}, + }, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "001", Applied: true}, + }, + }, + // Ensure that any applied migrations that don't exist locally + // do not show up in the response. + { + localFilesystemMigrations: []*models.Migration{}, + remoteAppliedMigrations: []*models.Migration{ + {Version: "001", Applied: true}, + }, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{}, + }, + // Ensure that migrations are sorted in asc order for sequential migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, + }, + }, + // Ensure that migrations are sorted in desc order for sequential migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderDesc, + expectedMigrations: []*models.Migration{ + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, + }, + }, + // Ensure that migrations are sorted in asc order for timestamp migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20080509220905"}, + {Version: "20080509220405"}, + {Version: "20080509150405"}, + {Version: "20080502150405"}, + {Version: "20080102150405"}, + {Version: "20070102150405"}, + {Version: "20060102150405"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "20060102150405"}, + {Version: "20070102150405"}, + {Version: "20080102150405"}, + {Version: "20080502150405"}, + {Version: "20080509150405"}, + {Version: "20080509220405"}, + {Version: "20080509220905"}, + }, + }, + // Ensure that migrations are sorted in desc order for timestamp migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20060102150405"}, + {Version: "20070102150405"}, + {Version: "20080102150405"}, + {Version: "20080502150405"}, + {Version: "20080509150405"}, + {Version: "20080509220405"}, + {Version: "20080509220905"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, + sortOrder: SortOrderDesc, + expectedMigrations: []*models.Migration{ + {Version: "20080509220905"}, + {Version: "20080509220405"}, + {Version: "20080509150405"}, + {Version: "20080502150405"}, + {Version: "20080102150405"}, + {Version: "20070102150405"}, + {Version: "20060102150405"}, + }, + }, + } + + for _, tc := range tests { + localFsMigrationMap := make(map[string]*models.Migration, 0) + for _, localFsMigration := range tc.localFilesystemMigrations { + localFsMigrationMap[localFsMigration.Version] = localFsMigration + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: localFsMigrationMap, + Err: nil, + }, + } + + remoteAppliedMigrationMap := make(map[string]*models.Migration, 0) + for _, remoteAppliedMigration := range tc.remoteAppliedMigrations { + remoteAppliedMigrationMap[remoteAppliedMigration.Version] = remoteAppliedMigration + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: remoteAppliedMigrationMap, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + tc.cfg, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(tc.sortOrder) + + assert.Nil(t, err) + assert.DeepEqual(t, migrations, tc.expectedMigrations) + } +} + +func TestListMigrations_FsRepoListError(t *testing.T) { + expectedErr := errors.New("fs repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_DbRepoListError(t *testing.T) { + expectedErr := errors.New("db repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_ParseErrDuringSort(t *testing.T) { + type test struct { + m1 models.Migration + m2 models.Migration + versionStyle configloader.VersionStyle + } + + tests := []test{ + { + m1: models.Migration{Version: "abc"}, + m2: models.Migration{Version: "123"}, + versionStyle: configloader.VersionStyleSequential, + }, + { + m1: models.Migration{Version: "123"}, + m2: models.Migration{Version: "abc"}, + versionStyle: configloader.VersionStyleSequential, + }, + { + m1: models.Migration{Version: "20060102150405"}, + m2: models.Migration{Version: "abc"}, + versionStyle: configloader.VersionStyleTimestamp, + }, + { + m1: models.Migration{Version: "abc"}, + m2: models.Migration{Version: "20060102150405"}, + versionStyle: configloader.VersionStyleTimestamp, + }, + } + + for _, tc := range tests { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + tc.m1.Version: {Version: tc.m1.Version}, + tc.m2.Version: {Version: tc.m2.Version}, + }, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: tc.versionStyle, + }, + }, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorContains(t, err, "unable to sort migrations") + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) + } +} + +func TestCreateMigration_VersionStyleTimestamp(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, + bolttest.NullOutputter{}, + ) + + beforeCreateTs := time.Now().Add(-time.Second) + migration, err := svc.CreateMigration("new migration") + afterCreateTs := time.Now().Add(time.Second) + + assert.Nil(t, err) + version, err := time.Parse("20060102150405", migration.Version) + assert.Nil(t, err) + check.True(t, beforeCreateTs.Before(version)) + check.True(t, afterCreateTs.After(version)) + check.Equal(t, migration.Message, "new migration") + check.Equal(t, migration.Applied, false) +} + +func TestCreateMigration_VersionStyleSequential(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.Nil(t, err) + check.Equal(t, migration.Version, "001") + check.Equal(t, migration.Message, "new migration") + check.Equal(t, migration.Applied, false) +} + +func TestCreateMigration_CreateErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + CreateReturnValue: bolttest.CreateReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, migration) +} + +func TestCreateMigration_LatestErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, migration) +} + +func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Migration: &models.Migration{Version: "001"}, + Err: nil, + }, + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.Nil(t, err) + assert.Equal(t, migration.Version, "002") +} + +func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Migration: &models.Migration{Version: "abc"}, + Err: nil, + }, + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.ErrorContains(t, err, `parsing "abc": invalid syntax`) + assert.Nil(t, migration) +} + +func TestApplyMigration_WithTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) +} + +func TestApplyMigration_WithoutTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "-- bolt: no-transaction\nCREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) +} + +func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "", + Err: expectedErr, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go index 25960c5..e986068 100644 --- a/internal/sqlparse/sqlparse.go +++ b/internal/sqlparse/sqlparse.go @@ -7,22 +7,24 @@ import ( "strings" ) -type SqlParser struct { - reader io.Reader +type SqlParser interface { + Parse(reader io.Reader) (ExecutionOptions, error) } type ExecutionOptions struct { UseTransaction bool } -func NewSqlParser(reader io.Reader) *SqlParser { - return &SqlParser{reader: reader} +type sqlParser struct{} + +func NewSqlParser() SqlParser { + return sqlParser{} } -func (sp *SqlParser) Parse() (*ExecutionOptions, error) { - options := &ExecutionOptions{UseTransaction: true} +func (sp sqlParser) Parse(reader io.Reader) (ExecutionOptions, error) { + options := ExecutionOptions{UseTransaction: true} - scanner := bufio.NewScanner(sp.reader) + scanner := bufio.NewScanner(reader) if scanner.Scan() { firstLine := strings.ToLower(strings.TrimSpace(scanner.Text())) if strings.HasPrefix(firstLine, "-- bolt:") { @@ -38,7 +40,7 @@ func (sp *SqlParser) Parse() (*ExecutionOptions, error) { } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("parsing sql file encountered an error: %w", err) + return options, fmt.Errorf("parsing sql file encountered an error: %w", err) } return options, nil diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 5fe58fa..12a435d 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -14,42 +14,42 @@ import ( func TestSqlParser_Parse(t *testing.T) { testCases := []struct { buffer string - expectedExecutionOptions *sqlparse.ExecutionOptions + expectedExecutionOptions sqlparse.ExecutionOptions }{ { buffer: "-- bolt: no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, }, { buffer: "-- BOLT: no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, }, { buffer: "\n-- bolt: no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, }, { buffer: "", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, }, { buffer: "--bolt: no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, }, { buffer: "-- bolt : no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, }, { buffer: "-- bolt: no-transaction no-transaction", - expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false}, + expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, }, } for _, tc := range testCases { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) + sqlParser := sqlparse.NewSqlParser() - execOptions, err := sqlParser.Parse() + execOptions, err := sqlParser.Parse(strings.NewReader(tc.buffer)) assert.Nil(t, err) assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) @@ -79,8 +79,8 @@ func TestSqlParserParseError(t *testing.T) { Reader: strings.NewReader(" "), ErrCond: " ", } - sqlParser := sqlparse.NewSqlParser(reader) - _, err := sqlParser.Parse() + sqlParser := sqlparse.NewSqlParser() + _, err := sqlParser.Parse(reader) assert.NotNil(t, err) assert.ErrorContains(t, err, "unwanted input encountered") } diff --git a/internal/storage/db.go b/internal/storage/db.go index 02683b4..a9e35af 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -10,8 +10,10 @@ import ( ) var ( - ErrMalformedConnectionString = errors.New("malformed database connection parameters provided") - ErrUnableToConnect = errors.New("unable to open connection to database") + ErrMalformedConnectionString = errors.New( + "malformed database connection parameters provided", + ) + ErrUnableToConnect = errors.New("unable to open connection to database") ) // DBConnect establishes a connection to the database using the driver -- cgit v1.2.3 From 55d3999d9efdeb0a87959e93d473cb64c8ba8c48 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 07:35:21 -0500 Subject: Add proper tests for ApplyAllMigrations and ApplyUpToVersion --- internal/services/migration.go | 2 + internal/services/migration_test.go | 202 ++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) (limited to 'internal') diff --git a/internal/services/migration.go b/internal/services/migration.go index 48859d6..c0d043b 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -105,6 +105,8 @@ func (ms MigrationService) ApplyUpToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if targetMigration.Applied { + // Assumption: If the target migration is applied, all migrations before + // it must also be applied. return fmt.Errorf( "migration with version %s is already applied, nothing to apply", version, diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 73045ef..e3d3244 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -593,3 +593,205 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { assert.ErrorIs(t, err, expectedErr) } + +func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) +} + +func TestApplyAllMigrations_ApplyMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) +} + +func TestApplyUpToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("002") + + assert.ErrorContains(t, err, "migration with version 002 does not exist") +} + +func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + assert.ErrorContains(t, err, "migration with version 001 is already applied") +} + +func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, + "003": {Version: "003", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("002") + + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) +} -- cgit v1.2.3 From 75a1bfa521ef1e10514330b984f46b84e1cef926 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 08:31:26 -0500 Subject: Remove NewMigrationServiceFromConfig --- internal/commands/down.go | 20 +++++++++++++++----- internal/commands/status.go | 20 +++++++++++++++----- internal/commands/up.go | 20 +++++++++++++++----- internal/services/migration.go | 26 +------------------------- 4 files changed, 46 insertions(+), 40 deletions(-) (limited to 'internal') diff --git a/internal/commands/down.go b/internal/commands/down.go index 8e88fc7..116d9ce 100644 --- a/internal/commands/down.go +++ b/internal/commands/down.go @@ -7,6 +7,7 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" + "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -63,16 +64,25 @@ func (cmd *DownCmd) Execute( } defer db.Close() - migrationService, err := services.NewMigrationServiceFromConfig( - db, - consoleOutputter, - *cfg, - ) + migrationDBRepo, err := repositories.NewMigrationDBRepo(db) + if err != nil { + consoleOutputter.Error(err) + return subcommands.ExitFailure + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } + migrationService := services.NewMigrationService( + migrationDBRepo, + migrationFsRepo, + *cfg, + consoleOutputter, + ) + if cmd.version == "" { err = migrationService.RevertAllMigrations() if err != nil { diff --git a/internal/commands/status.go b/internal/commands/status.go index eaf4332..76f7427 100644 --- a/internal/commands/status.go +++ b/internal/commands/status.go @@ -7,6 +7,7 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" + "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -53,16 +54,25 @@ func (m *StatusCmd) Execute( } defer db.Close() - migrationService, err := services.NewMigrationServiceFromConfig( - db, - consoleOutputter, - *cfg, - ) + migrationDBRepo, err := repositories.NewMigrationDBRepo(db) + if err != nil { + consoleOutputter.Error(err) + return subcommands.ExitFailure + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } + migrationService := services.NewMigrationService( + migrationDBRepo, + migrationFsRepo, + *cfg, + consoleOutputter, + ) + migrations, err := migrationService.ListMigrations(services.SortOrderAsc) if err != nil { consoleOutputter.Error(fmt.Errorf("unable to list migrations: %w", err)) diff --git a/internal/commands/up.go b/internal/commands/up.go index e10386a..330864f 100644 --- a/internal/commands/up.go +++ b/internal/commands/up.go @@ -7,6 +7,7 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" + "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/services" "github.com/eugenetriguba/bolt/internal/storage" "github.com/google/subcommands" @@ -63,16 +64,25 @@ func (cmd *UpCmd) Execute( } defer db.Close() - migrationService, err := services.NewMigrationServiceFromConfig( - db, - consoleOutputter, - *cfg, - ) + migrationDBRepo, err := repositories.NewMigrationDBRepo(db) + if err != nil { + consoleOutputter.Error(err) + return subcommands.ExitFailure + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) if err != nil { consoleOutputter.Error(err) return subcommands.ExitFailure } + migrationService := services.NewMigrationService( + migrationDBRepo, + migrationFsRepo, + *cfg, + consoleOutputter, + ) + if cmd.version == "" { err = migrationService.ApplyAllMigrations() if err != nil { diff --git a/internal/services/migration.go b/internal/services/migration.go index c0d043b..bc4b9d1 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -1,7 +1,6 @@ package services import ( - "database/sql" "fmt" "sort" "strconv" @@ -36,29 +35,6 @@ func NewMigrationService( } } -func NewMigrationServiceFromConfig( - db *sql.DB, - outputter output.Outputter, - cfg configloader.Config, -) (MigrationService, error) { - migrationDBRepo, err := repositories.NewMigrationDBRepo(db) - if err != nil { - return MigrationService{}, err - } - - migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) - if err != nil { - return MigrationService{}, err - } - - return NewMigrationService( - migrationDBRepo, - migrationFsRepo, - cfg, - outputter, - ), nil -} - type sortOrder int const ( @@ -259,7 +235,7 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error { } if execOptions.UseTransaction { - err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + err = ms.dbRepo.RevertWithTx(scriptContents, migration) } else { err = ms.dbRepo.Revert(scriptContents, migration) } -- cgit v1.2.3 From 4daf960b457fe1ab27e405c1c1d56e9cfc7c904e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 08:50:27 -0500 Subject: Add proper tests to revert service methods --- internal/services/migration.go | 2 + internal/services/migration_test.go | 276 ++++++++++++++++++++++++++++++++++++ 2 files changed, 278 insertions(+) (limited to 'internal') diff --git a/internal/services/migration.go b/internal/services/migration.go index bc4b9d1..446f267 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -189,6 +189,8 @@ func (ms MigrationService) RevertDownToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if !targetMigration.Applied { + // Assumption: Every migration from the latest down to the target + // migration hasn't been applied. return fmt.Errorf( "migration with version %s isn't applied, nothing to revert", version, diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index e3d3244..1087398 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -795,3 +795,279 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { assert.Nil(t, err) assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } + +func TestRevertMigration_WithTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "DROP TABLE tmp;", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) +} + +func TestRevertMigration_WithoutTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "-- bolt: no-transaction\nDROP TABLE tmp;", + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertReturnValue: bolttest.RevertReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertCallCount, 1) +} + +func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "", + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("002") + + assert.ErrorContains(t, err, "migration with version 002 does not exist") +} + +func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.ErrorContains(t, err, "migration with version 001 isn't applied") +} + +func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + "003": {Version: "003", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) +} + +func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) +} + +func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) +} -- cgit v1.2.3 From 6760ae82d62449f41c68950264c02c40be81bfb3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 09:29:41 -0500 Subject: Improve coverage for separate Tx methods --- internal/bolttest/mock_sql_db.go | 53 +++++++ internal/repositories/migration_db_repo.go | 19 ++- internal/repositories/migration_db_repo_test.go | 188 +++++++++++++++++++----- 3 files changed, 216 insertions(+), 44 deletions(-) create mode 100644 internal/bolttest/mock_sql_db.go (limited to 'internal') diff --git a/internal/bolttest/mock_sql_db.go b/internal/bolttest/mock_sql_db.go new file mode 100644 index 0000000..0c12592 --- /dev/null +++ b/internal/bolttest/mock_sql_db.go @@ -0,0 +1,53 @@ +package bolttest + +import "database/sql" + +type MockSqlDb struct { + ExecReturnValue ExecReturnValue + ExecCallCount int + QueryReturnValue QueryReturnValue + QueryCallCount int + QueryRowReturnValue QueryRowReturnValue + QueryRowCallCount int + BeginReturnValue BeginReturnValue + BeginCallCount int +} + +type ExecReturnValue struct { + Result sql.Result + Err error +} + +type QueryReturnValue struct { + Rows *sql.Rows + Err error +} + +type QueryRowReturnValue struct { + Row *sql.Row +} + +type BeginReturnValue struct { + Tx *sql.Tx + Err error +} + +func (db *MockSqlDb) Exec(query string, args ...any) (sql.Result, error) { + db.ExecCallCount += 1 + return db.ExecReturnValue.Result, db.ExecReturnValue.Err +} + +func (db *MockSqlDb) Query(query string, args ...any) (*sql.Rows, error) { + db.QueryCallCount += 1 + return db.QueryReturnValue.Rows, db.QueryReturnValue.Err +} + +func (db *MockSqlDb) QueryRow(query string, args ...any) *sql.Row { + db.QueryRowCallCount += 1 + return db.QueryRowReturnValue.Row +} + +func (db *MockSqlDb) Begin() (*sql.Tx, error) { + db.BeginCallCount += 1 + return db.BeginReturnValue.Tx, db.BeginReturnValue.Err +} diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 500fea4..7a90a2a 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -9,10 +9,17 @@ import ( "github.com/eugenetriguba/bolt/internal/models" ) -type SQLExecutor interface { +type sqlExecutor interface { Exec(query string, args ...interface{}) (sql.Result, error) } +type SqlDb interface { + Exec(query string, args ...any) (sql.Result, error) + Query(query string, args ...any) (*sql.Rows, error) + QueryRow(query string, args ...any) *sql.Row + Begin() (*sql.Tx, error) +} + type MigrationDBRepo interface { List() (map[string]*models.Migration, error) IsApplied(version string) (bool, error) @@ -23,14 +30,14 @@ type MigrationDBRepo interface { } type migrationDBRepo struct { - db *sql.DB + db SqlDb } // NewMigrationDBRepo initializes the MigrationDBRepo with a // database. Furthermore, it ensures the migration table it // operates on exists. If it is unable to create or confirm // the table exists, an error is returned. -func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { +func NewMigrationDBRepo(db SqlDb) (MigrationDBRepo, error) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS bolt_migrations( version CHARACTER(14) PRIMARY KEY NOT NULL @@ -38,7 +45,7 @@ func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { `) if err != nil { return nil, fmt.Errorf( - "unable to create bolt_migrations database table: %w", + "unable to confirm bolt_migrations database table exists: %w", err, ) } @@ -157,7 +164,7 @@ func (mr migrationDBRepo) ApplyWithTx( } func applyMigration( - executor SQLExecutor, + executor sqlExecutor, upgradeScript string, migration models.Migration, ) error { @@ -229,7 +236,7 @@ func (mr migrationDBRepo) RevertWithTx( } func revertMigration( - executor SQLExecutor, + executor sqlExecutor, downgradeScript string, migration models.Migration, ) error { diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 9f4f9e8..2775faa 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -2,6 +2,7 @@ package repositories_test import ( "database/sql" + "errors" "testing" "time" @@ -11,7 +12,7 @@ import ( "github.com/eugenetriguba/checkmate/assert" ) -func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { +func TestNewMigrationDBRepo_CreatesTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") row := db.QueryRow(` SELECT 1 @@ -36,7 +37,7 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { assert.Nil(t, err) } -func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { +func TestNewMigrationDBRepo_TableAlreadyExists(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") _, err := db.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`) assert.Nil(t, err) @@ -53,7 +54,20 @@ func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { assert.Equal(t, scanResult, 1) } -func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { +func TestNewMigrationDBRepo_TableCreationErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + ExecReturnValue: bolttest.ExecReturnValue{ + Err: expectedErr, + }, + } + + _, err := repositories.NewMigrationDBRepo(&db) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestList_EmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -63,7 +77,22 @@ func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { assert.Equal(t, len(migrations), 0) } -func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { +func TestList_QueryErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + QueryReturnValue: bolttest.QueryReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) + assert.Nil(t, err) + + _, err = repo.List() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestList_SingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -82,7 +111,7 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { ) } -func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { +func TestList_ShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -101,7 +130,7 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { ) } -func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { +func TestIsApplied_WithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -112,7 +141,7 @@ func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { assert.Equal(t, applied, false) } -func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { +func TestIsApplied_WithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -126,7 +155,7 @@ func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Apply(t *testing.T) { +func TestApply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -150,7 +179,69 @@ func TestMigrationDBRepo_Apply(t *testing.T) { assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Revert(t *testing.T) { +func TestApply_MalformedSql(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.Apply("this is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) +} + +func TestApplyWithTx_BeginErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + BeginReturnValue: bolttest.BeginReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.ApplyWithTx("SELECT 1 FROM bolt_migrations;", migration) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyWithTx_ExecErr(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.ApplyWithTx("SELECT 1 FROM abc123donotexist;", migration) + + assert.ErrorContains(t, err, `relation "abc123donotexist" does not exist`) +} + +func TestApplyWithTx_SuccessfullyApplied(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + + migration := models.NewTimestampMigration(time.Now(), "test") + err = repo.ApplyWithTx(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, true) + + row := db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'tmp' + `) + var scanResult int + err = row.Scan(&scanResult) + assert.Nil(t, err) + applied, err := repo.IsApplied(migration.Version) + assert.Nil(t, err) + assert.Equal(t, applied, true) +} + +func TestRevert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -187,57 +278,78 @@ func TestMigrationDBRepo_Revert(t *testing.T) { assert.Equal(t, count, 0) } -func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { +func TestRevert_MalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) - _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - assert.Nil(t, err) - migration := models.NewTimestampMigration(time.Now(), "test") - // CREATE INDEX CONCURRENTLY cannot run inside a transaction. - err = repo.Apply( - `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, - migration, - ) - assert.Nil(t, err) + migration.Applied = true + + err = repo.Revert("this is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") assert.Equal(t, migration.Applied, true) +} - err = repo.Revert( - "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", - migration, - ) +func TestRevertWithTx_BeginErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + BeginReturnValue: bolttest.BeginReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) assert.Nil(t, err) - assert.Equal(t, migration.Applied, false) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.RevertWithTx("DROP TABLE bolt_migrations;", migration) + + assert.ErrorIs(t, err, expectedErr) } -func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { +func TestRevertWithTx_ExecErr(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") - err = repo.Apply("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + err = repo.RevertWithTx("DROP TABLE abc123donotexist;", migration) - err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + assert.ErrorContains(t, err, `table "abc123donotexist" does not exist`) } -func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { +func TestRevertWithTx_SuccessfullyReverted(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) + + _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + _, err = db.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + assert.Nil(t, err) migration.Applied = true - err = repo.Revert("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + err = repo.RevertWithTx(`DROP TABLE tmp;`, migration) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) - err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + row := db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'tmp' + `) + var scanResult int + err = row.Scan(&scanResult) + assert.ErrorIs(t, err, sql.ErrNoRows) + + row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) + var count int + err = row.Scan(&count) + assert.Nil(t, err) + assert.Equal(t, count, 0) } -- cgit v1.2.3 From 018083b9259c2265ad3ea2432f315641e5b996ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 10:18:41 -0500 Subject: Clean up fs repo and fs repo tests --- internal/bolttest/mock_migration_fs_repo.go | 23 +--- internal/repositories/migration_fs_repo.go | 46 +++----- internal/repositories/migration_fs_repo_test.go | 141 ++++++++++++++++++++++-- internal/services/migration.go | 5 +- 4 files changed, 152 insertions(+), 63 deletions(-) (limited to 'internal') diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go index 0932b37..ae933e3 100644 --- a/internal/bolttest/mock_migration_fs_repo.go +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -5,10 +5,6 @@ import "github.com/eugenetriguba/bolt/internal/models" type MockMigrationFsRepo struct { CreateReturnValue CreateReturnValue CreateCallCount int - ExistsReturnValue ExistsReturnValue - ExistsCallCount int - GetReturnValue GetReturnValue - GetCallCount int ListReturnValue ListReturnValue ListCallCount int LatestReturnValue LatestReturnValue @@ -23,18 +19,11 @@ type CreateReturnValue struct { Err error } -type ExistsReturnValue struct { - Exists bool - Err error -} - -type GetReturnValue struct { +type LatestReturnValue struct { Migration *models.Migration Err error } -type LatestReturnValue = GetReturnValue - type ReadUpgradeScriptReturnValue struct { ScriptContents string Err error @@ -47,16 +36,6 @@ func (repo *MockMigrationFsRepo) Create(migration *models.Migration) error { return repo.CreateReturnValue.Err } -func (repo *MockMigrationFsRepo) Exists(version string) (bool, error) { - repo.ExistsCallCount += 1 - return repo.ExistsReturnValue.Exists, repo.ExistsReturnValue.Err -} - -func (repo *MockMigrationFsRepo) Get(version string) (*models.Migration, error) { - repo.GetCallCount += 1 - return repo.GetReturnValue.Migration, repo.GetReturnValue.Err -} - func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) { repo.ListCallCount += 1 return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 9d21c59..9d6c3aa 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -25,8 +25,6 @@ func (e *ErrIsNotDir) Error() string { type MigrationFsRepo interface { Create(migration *models.Migration) error - Exists(version string) (bool, error) - Get(version string) (*models.Migration, error) List() (map[string]*models.Migration, error) Latest() (*models.Migration, error) ReadUpgradeScript(migration *models.Migration) (string, error) @@ -41,14 +39,22 @@ func NewMigrationFsRepo( migrationsConfig *configloader.MigrationsConfig, ) (MigrationFsRepo, error) { fileInfo, err := os.Stat(migrationsConfig.DirectoryPath) - if errors.Is(err, os.ErrNotExist) { + if errors.Is(err, fs.ErrNotExist) { err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to create migration directory at %s: %w", + migrationsConfig.DirectoryPath, + err, + ) } } else if err != nil { - return nil, err - } else if err == nil && !fileInfo.IsDir() { + return nil, fmt.Errorf( + "unable to check if migration directory at %s exists: %w", + migrationsConfig.DirectoryPath, + err, + ) + } else if !fileInfo.IsDir() { return nil, &ErrIsNotDir{path: migrationsConfig.DirectoryPath} } @@ -76,31 +82,11 @@ func (mr migrationFsRepo) Create(migration *models.Migration) error { return nil } -func (mr migrationFsRepo) Exists(version string) (bool, error) { - migrations, err := mr.List() - if err != nil { - return false, err - } - - _, ok := migrations[version] - return ok, nil -} - -func (mr migrationFsRepo) Get(version string) (*models.Migration, error) { - migrations, err := mr.List() - if err != nil { - return nil, err - } - - migration, ok := migrations[version] - if !ok { - return nil, fmt.Errorf("migration %s does not exist", version) - } - - return migration, nil -} - func (mr migrationFsRepo) Latest() (*models.Migration, error) { + // TODO: ReadDir returns migrations sorted by filename. + // To be correct, it seems like we will need to sort these + // migrations by the version style that is configured and then + // grab the latest. entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { return nil, err diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index 72ba792..d10bddc 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -18,18 +18,18 @@ func assertFileExists(t *testing.T, path string) { assert.Nil(t, err) } -func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) { +func TestNewMigrationFsRepo_CreatesDirIfNotExists(t *testing.T) { tempDir := t.TempDir() migrationsDir := filepath.Join(tempDir, "migrations") migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} _, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.Nil(t, err) + assert.Nil(t, err) assertFileExists(t, migrationsDir) } -func TestNewMigrationFsRepoMigrationDirIsFile(t *testing.T) { +func TestNewMigrationFsRepo_MigrationDirIsFile(t *testing.T) { tempDir := t.TempDir() migrationsDir := filepath.Join(tempDir, "migrations") migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} @@ -37,10 +37,19 @@ func TestNewMigrationFsRepoMigrationDirIsFile(t *testing.T) { assert.Nil(t, err) _, err = repositories.NewMigrationFsRepo(&migrationsConfig) + assert.ErrorContains(t, err, "is not a directory") } -func TestMigrationFsRepoCreate(t *testing.T) { +func TestNewMigrationFsRepo_UnknownStatErr(t *testing.T) { + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: "\000x"} + + _, err := repositories.NewMigrationFsRepo(&migrationsConfig) + + assert.ErrorContains(t, err, "unable to check if migration directory") +} + +func TestCreate_SuccessfullyCreated(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) @@ -56,13 +65,29 @@ func TestMigrationFsRepoCreate(t *testing.T) { assertFileExists(t, filepath.Join(tempDir, migrationDirName, "downgrade.sql")) } -func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { +func TestCreate_FailsToCreateDir(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) assert.Nil(t, err) - migration := models.NewTimestampMigration(time.Now(), "add users table") + migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) + _, err = os.Create(filepath.Join(tempDir, migrationDirName)) + assert.Nil(t, err) + + err = repo.Create(migration) + + assert.NotNil(t, err) + assert.ErrorContains(t, err, "file exists") +} + +func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) { + tempDir := t.TempDir() + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + + migration := models.NewSequentialMigration(1, "add users table") err = repo.Create(migration) assert.Nil(t, err) @@ -73,6 +98,35 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { expectedUpgradeScriptContents, 0755, ) + + upgradeScriptContents, err := repo.ReadUpgradeScript(migration) + assert.Nil(t, err) + assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) +} + +func TestReadUpgradeScript_FileDoesNotExist(t *testing.T) { + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: t.TempDir()} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + migration := models.NewSequentialMigration(1, "add users table") + + _, err = repo.ReadUpgradeScript(migration) + + assert.NotNil(t, err) + assert.ErrorContains(t, err, "no such file or directory") +} + +func TestReadDowngradeScript_SuccessfullyRead(t *testing.T) { + tempDir := t.TempDir() + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + + migration := models.NewSequentialMigration(1, "add users table") + err = repo.Create(migration) + assert.Nil(t, err) + + migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) expectedDowngradeScriptContents := []byte("DROP TABLE users;") os.WriteFile( filepath.Join(tempDir, migrationDirName, "downgrade.sql"), @@ -80,15 +134,24 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { 0755, ) - upgradeScriptContents, err := repo.ReadUpgradeScript(migration) - assert.Nil(t, err) - assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) downgradeScriptContents, err := repo.ReadDowngradeScript(migration) assert.Nil(t, err) assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) } -func TestMigrationFsRepo_List(t *testing.T) { +func TestReadDowngradeScript_FileDoesNotExist(t *testing.T) { + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: t.TempDir()} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + migration := models.NewSequentialMigration(1, "add users table") + + _, err = repo.ReadDowngradeScript(migration) + + assert.NotNil(t, err) + assert.ErrorContains(t, err, "no such file or directory") +} + +func TestList_Success(t *testing.T) { tempDir := t.TempDir() migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) @@ -113,3 +176,61 @@ func TestMigrationFsRepo_List(t *testing.T) { assert.DeepEqual(t, migrations[migration1.Version], migration1) assert.DeepEqual(t, migrations[migration2.Version], migration2) } + +func TestList_DirDoesNotExist(t *testing.T) { + migrationsDir := filepath.Join(t.TempDir(), "migrations") + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + err = os.RemoveAll(migrationsDir) + assert.Nil(t, err) + + _, err = repo.List() + + assert.NotNil(t, err) + assert.ErrorContains(t, err, "no such file or directory") +} + +func TestList_InvalidMigrationName(t *testing.T) { + migrationsDir := filepath.Join(t.TempDir(), "migrations") + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + err = os.Mkdir(filepath.Join(migrationsDir, "invalid"), 0755) + assert.Nil(t, err) + + _, err = repo.List() + + assert.NotNil(t, err) + assert.ErrorContains( + t, + err, + "expected a migration directory of the format _", + ) +} + +func TestLatest_DirDoesNotExist(t *testing.T) { + migrationsDir := filepath.Join(t.TempDir(), "migrations") + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + err = os.RemoveAll(migrationsDir) + assert.Nil(t, err) + + _, err = repo.Latest() + + assert.NotNil(t, err) + assert.ErrorContains(t, err, "no such file or directory") +} + +func TestLatest_NoMigrations(t *testing.T) { + migrationsDir := filepath.Join(t.TempDir(), "migrations") + migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} + repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + assert.Nil(t, err) + + latestMigration, err := repo.Latest() + + assert.Nil(t, err) + assert.Nil(t, latestMigration) +} diff --git a/internal/services/migration.go b/internal/services/migration.go index 446f267..a91ae2a 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -335,7 +335,10 @@ func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, return migrations, err } -func (ms MigrationService) sortMigrations(migrations []*models.Migration, order sortOrder) error { +func (ms MigrationService) sortMigrations( + migrations []*models.Migration, + order sortOrder, +) error { sortErrs := make([]error, 0) sort.Slice(migrations, func(i, j int) bool { var comparison bool -- cgit v1.2.3 From a37df93853def914dbda165cdcf09ae8e0305ec3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 11:04:00 -0500 Subject: Remove Latest() from migration fs repo in favor of using List on service --- internal/bolttest/mock_migration_fs_repo.go | 12 ------ internal/repositories/migration_fs_repo.go | 18 --------- internal/repositories/migration_fs_repo_test.go | 26 ------------- internal/services/migration.go | 5 ++- internal/services/migration_test.go | 50 +++++++++++++++++++++---- 5 files changed, 46 insertions(+), 65 deletions(-) (limited to 'internal') diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go index ae933e3..d392732 100644 --- a/internal/bolttest/mock_migration_fs_repo.go +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -7,8 +7,6 @@ type MockMigrationFsRepo struct { CreateCallCount int ListReturnValue ListReturnValue ListCallCount int - LatestReturnValue LatestReturnValue - LatestCallCount int ReadUpgradeScriptReturnValue ReadUpgradeScriptReturnValue ReadUpgradeScriptCallCount int ReadDowngradeScriptReturnValue ReadDowngradeScriptReturnValue @@ -19,11 +17,6 @@ type CreateReturnValue struct { Err error } -type LatestReturnValue struct { - Migration *models.Migration - Err error -} - type ReadUpgradeScriptReturnValue struct { ScriptContents string Err error @@ -41,11 +34,6 @@ func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) { return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err } -func (repo *MockMigrationFsRepo) Latest() (*models.Migration, error) { - repo.LatestCallCount += 1 - return repo.LatestReturnValue.Migration, repo.LatestReturnValue.Err -} - func (repo *MockMigrationFsRepo) ReadUpgradeScript( migration *models.Migration, ) (string, error) { diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 9d6c3aa..e535013 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -26,7 +26,6 @@ func (e *ErrIsNotDir) Error() string { type MigrationFsRepo interface { Create(migration *models.Migration) error List() (map[string]*models.Migration, error) - Latest() (*models.Migration, error) ReadUpgradeScript(migration *models.Migration) (string, error) ReadDowngradeScript(migration *models.Migration) (string, error) } @@ -82,23 +81,6 @@ func (mr migrationFsRepo) Create(migration *models.Migration) error { return nil } -func (mr migrationFsRepo) Latest() (*models.Migration, error) { - // TODO: ReadDir returns migrations sorted by filename. - // To be correct, it seems like we will need to sort these - // migrations by the version style that is configured and then - // grab the latest. - entries, err := os.ReadDir(mr.migrationsDirPath) - if err != nil { - return nil, err - } - - if len(entries) > 0 { - return dirEntryToMigration(entries[len(entries)-1]) - } - - return nil, nil -} - func (mr migrationFsRepo) List() (map[string]*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index d10bddc..32de193 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -208,29 +208,3 @@ func TestList_InvalidMigrationName(t *testing.T) { "expected a migration directory of the format _", ) } - -func TestLatest_DirDoesNotExist(t *testing.T) { - migrationsDir := filepath.Join(t.TempDir(), "migrations") - migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} - repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.Nil(t, err) - err = os.RemoveAll(migrationsDir) - assert.Nil(t, err) - - _, err = repo.Latest() - - assert.NotNil(t, err) - assert.ErrorContains(t, err, "no such file or directory") -} - -func TestLatest_NoMigrations(t *testing.T) { - migrationsDir := filepath.Join(t.TempDir(), "migrations") - migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} - repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.Nil(t, err) - - latestMigration, err := repo.Latest() - - assert.Nil(t, err) - assert.Nil(t, latestMigration) -} diff --git a/internal/services/migration.go b/internal/services/migration.go index a91ae2a..4be10cc 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -283,12 +283,13 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { var currentVersion uint64 = 0 - latestMigration, err := ms.fsRepo.Latest() + migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return 0, err } - if latestMigration != nil { + if len(migrations) > 0 { + latestMigration := migrations[0] currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) if err != nil { return 0, err diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 1087398..0c30c76 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -440,7 +440,7 @@ func TestCreateMigration_CreateErr(t *testing.T) { func TestCreateMigration_LatestErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ + ListReturnValue: bolttest.ListReturnValue{ Err: expectedErr, }, } @@ -464,9 +464,11 @@ func TestCreateMigration_LatestErr(t *testing.T) { func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ - Migration: &models.Migration{Version: "001"}, - Err: nil, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001"}, + }, + Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, @@ -490,11 +492,45 @@ func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { assert.Equal(t, migration.Version, "002") } +func TestCreateMigration_SequentialVersionIncrementsLatestVersion(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001"}, + "002": {Version: "002"}, + "003": {Version: "003"}, + }, + Err: nil, + }, + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.Nil(t, err) + assert.Equal(t, migration.Version, "004") +} + func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ - Migration: &models.Migration{Version: "abc"}, - Err: nil, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "abc": {Version: "abc"}, + }, + Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, -- cgit v1.2.3