diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 11:07:30 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 11:07:30 -0500 |
| commit | 571bc9bf2efa841b29bf036f4d307664bfbe155d (patch) | |
| tree | 41fb3a459db99d1f0ec2ed2e00d4ca580a5d0c46 /internal | |
| parent | Update readme (diff) | |
| parent | Remove Latest() from migration fs repo in favor of using List on service (diff) | |
| download | cli-571bc9bf2efa841b29bf036f4d307664bfbe155d.zip | |
Merge pull request #1 from eugenetriguba/0.3.0
Diffstat (limited to 'internal')
31 files changed, 2336 insertions, 436 deletions
diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index de2dcfa..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" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig { port, err := strconv.Atoi(os.Getenv("DB_PORT")) - assert.NilError(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) - assert.NilError(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 $$; `) - assert.NilError(t, err) + assert.Nil(t, err) err = db.Close() - assert.NilError(t, err) + assert.Nil(t, err) }) return db } diff --git a/internal/bolttest/fs.go b/internal/bolttest/fs.go index 9edd894..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" - "gotest.tools/v3/assert" + "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) - assert.NilError(t, err) + assert.Nil(t, err) err = f.Close() - assert.NilError(t, err) + assert.Nil(t, err) } func CreateTempFile(t *testing.T, filePath string) *os.File { f, err := os.Create(filePath) - assert.NilError(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() - assert.NilError(t, err) + assert.Nil(t, err) err = os.Chdir(path) - assert.NilError(t, err) + assert.Nil(t, err) t.Cleanup(func() { err = os.Chdir(dir) 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..d392732 --- /dev/null +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -0,0 +1,49 @@ +package bolttest + +import "github.com/eugenetriguba/bolt/internal/models" + +type MockMigrationFsRepo struct { + CreateReturnValue CreateReturnValue + CreateCallCount int + ListReturnValue ListReturnValue + ListCallCount int + ReadUpgradeScriptReturnValue ReadUpgradeScriptReturnValue + ReadUpgradeScriptCallCount int + ReadDowngradeScriptReturnValue ReadDowngradeScriptReturnValue + ReadDowngradeScriptCallCount int +} + +type CreateReturnValue struct { + Err error +} + +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) List() (map[string]*models.Migration, error) { + repo.ListCallCount += 1 + return repo.ListReturnValue.Migrations, repo.ListReturnValue.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/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/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/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)) +} diff --git a/internal/commands/down.go b/internal/commands/down.go index 262e229..116d9ce 100644 --- a/internal/commands/down.go +++ b/internal/commands/down.go @@ -3,6 +3,7 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" @@ -49,7 +50,7 @@ func (cmd *DownCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -58,39 +59,40 @@ func (cmd *DownCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + 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.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 } migrationService := services.NewMigrationService( migrationDBRepo, migrationFsRepo, + *cfg, consoleOutputter, ) if cmd.version == "" { err = migrationService.RevertAllMigrations() if err != nil { - consoleOutputter.Error(err.Error()) + 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.Error()) + 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 a4eeff4..96ddd98 100644 --- a/internal/commands/new.go +++ b/internal/commands/new.go @@ -3,10 +3,9 @@ package commands import ( "context" "flag" - "time" + "fmt" "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" @@ -50,30 +49,28 @@ func (cmd *NewCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + 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.Error()) + 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.Error()) - return subcommands.ExitFailure - } - migration := models.NewMigration(time.Now(), cmd.message) - err = migrationService.CreateMigration(migration) + _, err = migrationService.CreateMigration(cmd.message) if err != nil { - consoleOutputter.Error(err.Error()) + 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 a58d587..76f7427 100644 --- a/internal/commands/status.go +++ b/internal/commands/status.go @@ -3,6 +3,7 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" @@ -39,7 +40,7 @@ func (m *StatusCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -48,31 +49,33 @@ func (m *StatusCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + 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.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 } migrationService := services.NewMigrationService( migrationDBRepo, migrationFsRepo, + *cfg, consoleOutputter, ) + migrations, err := migrationService.ListMigrations(services.SortOrderAsc) if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(fmt.Errorf("unable to list migrations: %w", err)) return subcommands.ExitFailure } @@ -95,6 +98,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 ba74299..330864f 100644 --- a/internal/commands/up.go +++ b/internal/commands/up.go @@ -3,6 +3,7 @@ package commands import ( "context" "flag" + "fmt" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/output" @@ -49,7 +50,7 @@ func (cmd *UpCmd) Execute( cfg, err := configloader.NewConfig() if err != nil { - consoleOutputter.Error(err.Error()) + consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err)) return subcommands.ExitFailure } @@ -58,39 +59,40 @@ func (cmd *UpCmd) Execute( storage.DBConnectionString(&cfg.Connection), ) if err != nil { - consoleOutputter.Error(err.Error()) + 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.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 } migrationService := services.NewMigrationService( migrationDBRepo, migrationFsRepo, + *cfg, consoleOutputter, ) if cmd.version == "" { err = migrationService.ApplyAllMigrations() if err != nil { - consoleOutputter.Error(err.Error()) + 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.Error()) + consoleOutputter.Error(fmt.Errorf("unable to apply migrations up to %s: %w", cmd.version, 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/configloader/configloader.go b/internal/configloader/configloader.go index c50ec24..c2c05e7 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,8 @@ 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"` } type ConnectionConfig struct { @@ -49,12 +64,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: VersionStyleTimestamp, + }, + } + if !errors.Is(err, ErrConfigFileNotFound) { _, err = toml.DecodeFile(filePath, &cfg) if err != nil { return nil, err @@ -66,6 +86,11 @@ func NewConfig() (*Config, error) { return nil, err } + if cfg.Migrations.VersionStyle != VersionStyleSequential && + cfg.Migrations.VersionStyle != VersionStyleTimestamp { + return nil, ErrInvalidVersionStyle + } + return &cfg, nil } @@ -91,7 +116,7 @@ func findConfigFilePath() (filePath string, err error) { } if dir == rootDir { - return "", errConfigFileNotFound + return "", ErrConfigFileNotFound } dir = filepath.Dir(dir) diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index a41c2be..0ad6930 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -7,21 +7,39 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" ) -func TestNewConfigMigrationsDirPathDefault(t *testing.T) { +func TestNewConfigDefaults(t *testing.T) { bolttest.ChangeCwd(t, os.TempDir()) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + assert.Nil(t, err) - assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations") + check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") + check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp) +} + +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{DirectoryPath: "myfancymigrations"}, + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "myfancymigrations", + VersionStyle: configloader.VersionStyleSequential, + }, Connection: configloader.ConnectionConfig{ Host: "testhost", Port: 1234, @@ -34,13 +52,16 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml") cfg, err := configloader.NewConfig() - assert.NilError(t, err) + assert.Nil(t, err) assert.DeepEqual(t, *cfg, expectedCfg) } 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 +74,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 +87,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)) @@ -72,6 +97,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { t.Setenv("BOLT_DB_CONN_DRIVER", envCfg.Connection.Driver) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + 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 dcd8ebd..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 @@ -8,21 +8,24 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) 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") homedir, err := os.UserHomeDir() - assert.NilError(t, err) + assert.Nil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + assert.Nil(t, err) assert.DeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go index 42f2978..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 @@ -9,23 +9,26 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) 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) - assert.NilError(t, err) + assert.Nil(t, err) homedir, err := os.UserHomeDir() - assert.NilError(t, err) + assert.Nil(t, err) bolttest.ChangeCwd(t, homedir) cfg, err := configloader.NewConfig() - assert.NilError(t, err) + assert.Nil(t, err) assert.DeepEqual(t, *cfg, expectedCfg) } diff --git a/internal/models/migration.go b/internal/models/migration.go index f647a1b..b63fec9 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -11,30 +11,26 @@ 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, - ) +func (m *Migration) Name() string { + return fmt.Sprintf("%s_%s", m.Version, m.Message) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 8d1fb06..b6d3205 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -1,33 +1,39 @@ package models_test import ( + "fmt" "testing" "time" - "gotest.tools/v3/assert" - "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/checkmate/check" ) -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) + check.Equal(t, m.Version, "20200101000000") + check.Equal(t, m.Message, "test message") + check.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") + + 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.String(), "20200101000000 - test_message - [ ]") - m.Applied = true - assert.Equal(t, m.String(), "20200101000000 - test_message - [x]") + check.Equal(t, m.Version, "100") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) } -func TestEmptyMessageMigration(t *testing.T) { - m := models.NewMigration(timestamp, "") - assert.Equal(t, "20200101000000 - [ ]", m.String()) +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/output/console.go b/internal/output/console.go index d2946ed..1a4efad 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -18,12 +18,19 @@ func NewConsoleOutputter() ConsoleOutputter { func (c ConsoleOutputter) Output(message string) error { _, err := fmt.Fprintln(c.stdout, message) - return 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 err +func (c ConsoleOutputter) Error(err error) error { + msg := err.Error() + _, printErr := fmt.Fprintln(c.stderr, msg) + 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 { @@ -32,13 +39,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 +53,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/output/console_test.go b/internal/output/console_test.go index be369ab..492d0a2 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -2,10 +2,11 @@ package output import ( "bytes" + "errors" "io" "testing" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/check" ) func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { @@ -19,8 +20,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) { @@ -28,10 +29,10 @@ func TestConsoleOutputter_Error(t *testing.T) { var stderr bytes.Buffer consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) - consoleOutputter.Error("test string") + consoleOutputter.Error(errors.New("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 +71,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(), "") } } 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 } diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 42cf0e8..7a90a2a 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -3,50 +3,80 @@ package repositories import ( "database/sql" "errors" + "fmt" "strings" "github.com/eugenetriguba/bolt/internal/models" - "github.com/eugenetriguba/bolt/internal/sqlparse" ) -type MigrationDBRepo struct { - db *sql.DB +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) + 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 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 ); `) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to confirm bolt_migrations database table exists: %w", + err, + ) } - 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, err + return nil, fmt.Errorf( + "unable to execute query to select versions from "+ + "bolt_migrations database table: %w", + err, + ) } 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) 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{ @@ -67,15 +97,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 } @@ -84,110 +118,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 err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return err - } - defer tx.Rollback() - - _, err = tx.Exec(upgradeScript) - if err != nil { - return 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 err - } - err = tx.Commit() - if err != nil { - return err - } - } else { - _, err = mr.db.Exec(upgradeScript) - if err != nil { - return err - } + } + defer tx.Rollback() + + err = applyMigration(tx, upgradeScript, *migration) + if err != nil { + return err + } - _, err = mr.db.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, + err = tx.Commit() + if err != nil { + return fmt.Errorf( + "unable to commit transaction during upgrade script execution: %w", + err, ) - if err != nil { - return 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 err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return err - } - defer tx.Rollback() - - _, err = tx.Exec(downgradeScript) - if err != nil { - return 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 err - } + } + defer tx.Rollback() - err = tx.Commit() - if err != nil { - return err - } - } else { - _, err = mr.db.Exec(downgradeScript) - if err != nil { - return 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 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 55b8eb2..2775faa 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -2,16 +2,17 @@ package repositories_test import ( "database/sql" + "errors" "testing" "time" "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/assert" ) -func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { +func TestNewMigrationDBRepo_CreatesTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") row := db.QueryRow(` SELECT 1 @@ -24,7 +25,7 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { assert.ErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,47 +34,75 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - assert.NilError(t, err) + 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.NilError(t, err) + assert.Nil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - assert.NilError(t, err) + assert.Nil(t, err) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) row := db.QueryRow(`SELECT id FROM bolt_migrations`) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + assert.Nil(t, err) 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.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) 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.NilError(t, err) + assert.Nil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.DeepEqual( t, @@ -82,17 +111,17 @@ 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.NilError(t, err) + assert.Nil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.DeepEqual( t, @@ -101,39 +130,101 @@ 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.NilError(t, err) + assert.Nil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - assert.NilError(t, err) + assert.Nil(t, err) 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.NilError(t, err) + assert.Nil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(version) - assert.NilError(t, err) + assert.Nil(t, err) 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.NilError(t, err) + assert.Nil(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.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 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(` @@ -144,30 +235,30 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(migration.Version) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Revert(t *testing.T) { +func TestRevert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - assert.NilError(t, err) + assert.Nil(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, ) - assert.NilError(t, err) + assert.Nil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, migration.Applied, false) row := db.QueryRow(` @@ -183,61 +274,82 @@ func TestMigrationDBRepo_Revert(t *testing.T) { row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) var count int err = row.Scan(&count) - assert.NilError(t, err) + assert.Nil(t, err) 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.NilError(t, err) - _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - assert.NilError(t, err) - - migration := models.NewMigration(time.Now(), "test") - // CREATE INDEX CONCURRENTLY cannot run inside a transaction. - err = repo.Apply( - `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, - migration, - ) - assert.NilError(t, err) + assert.Nil(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) +} - err = repo.Revert( - "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", - migration, - ) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, false) +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) + 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.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + 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.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + 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) } diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 5478b5c..e535013 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" @@ -22,27 +23,44 @@ func (e *ErrIsNotDir) Error() string { ) } -type MigrationFsRepo struct { +type MigrationFsRepo interface { + Create(migration *models.Migration) error + List() (map[string]*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) { +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} } - 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) @@ -63,59 +81,41 @@ 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) 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 { - 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 <version>_<message>", - 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 (mr *MigrationFsRepo) ReadUpgradeScript( - migration *models.Migration, -) (string, error) { +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 <version>_<message>", + entry.Name(), + ) + } + return &models.Migration{ + Version: parts[0], + Message: parts[1], + Applied: false, + }, nil +} + +func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) { upgradeScriptPath := filepath.Join( mr.migrationsDirPath, mr.migrationDirname(migration), @@ -124,7 +124,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( @@ -135,7 +135,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 @@ -146,13 +146,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/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index 8fdcd8f..32de193 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -10,45 +10,54 @@ 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/assert" ) func assertFileExists(t *testing.T, path string) { _, err := os.Stat(path) - assert.NilError(t, err) + 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.NilError(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} _, err := os.Create(migrationsDir) - assert.NilError(t, err) + 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) - assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "add users table") + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "add users table") err = repo.Create(migration) - assert.NilError(t, err) + assert.Nil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) assertFileExists(t, filepath.Join(tempDir, migrationDirName)) @@ -56,15 +65,31 @@ 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.NilError(t, err) + assert.Nil(t, err) - migration := models.NewMigration(time.Now(), "add users table") + migration := models.NewSequentialMigration(1, "add users table") err = repo.Create(migration) - assert.NilError(t, err) + assert.Nil(t, err) migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);") @@ -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,36 +134,77 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { 0755, ) - upgradeScriptContents, err := repo.ReadUpgradeScript(migration) - assert.NilError(t, err) - assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) downgradeScriptContents, err := repo.ReadDowngradeScript(migration) - assert.NilError(t, err) + 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) - assert.NilError(t, err) + assert.Nil(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", ) err = repo.Create(migration1) - assert.NilError(t, err) + assert.Nil(t, err) repo.Create(migration2) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 2) 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 <version>_<message>", + ) +} diff --git a/internal/services/migration.go b/internal/services/migration.go index 8b5898e..4be10cc 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -3,24 +3,36 @@ package services import ( "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, + } } type sortOrder int @@ -30,7 +42,7 @@ const ( SortOrderAsc ) -func (ms *MigrationService) ApplyAllMigrations() error { +func (ms MigrationService) ApplyAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err @@ -38,14 +50,21 @@ 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, + ) + } } } 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 @@ -62,6 +81,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, @@ -72,7 +93,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, + ) } } @@ -84,7 +109,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..", @@ -98,10 +123,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!", @@ -113,7 +150,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 @@ -121,14 +158,21 @@ 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, + ) + } } } 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 @@ -145,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, @@ -155,7 +201,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, + ) } } @@ -167,7 +217,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..", @@ -180,10 +230,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.RevertWithTx(scriptContents, migration) + } else { + err = ms.dbRepo.Revert(scriptContents, migration) + } + if err != nil { return err } + ms.outputter.Output( fmt.Sprintf( "Successfully reverted migration %s_%s!", @@ -195,48 +257,156 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration(migration *models.Migration) error { +func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) { + var migration *models.Migration + + if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp { + migration = models.NewTimestampMigration(time.Now(), message) + } else { + currentVersion, err := ms.getCurrentSequentialMigrationVersion() + if err != nil { + return nil, err + } + 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) ListMigrations(order sortOrder) ([]*models.Migration, error) { +func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { + var currentVersion uint64 = 0 + + migrations, err := ms.ListMigrations(SortOrderDesc) + if err != nil { + return 0, err + } + + if len(migrations) > 0 { + latestMigration := migrations[0] + 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) { // 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 +} - return flatMigrations, 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 + } + + 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..0c30c76 --- /dev/null +++ b/internal/services/migration_test.go @@ -0,0 +1,1109 @@ +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{ + ListReturnValue: bolttest.ListReturnValue{ + 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{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {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_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{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "abc": {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) +} + +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) +} + +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) +} diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go index 9ded618..e986068 100644 --- a/internal/sqlparse/sqlparse.go +++ b/internal/sqlparse/sqlparse.go @@ -2,26 +2,29 @@ package sqlparse import ( "bufio" + "fmt" "io" "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:") { @@ -37,7 +40,7 @@ func (sp *SqlParser) Parse() (*ExecutionOptions, error) { } if err := scanner.Err(); err != nil { - return nil, 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 51b93b1..12a435d 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -1,56 +1,86 @@ package sqlparse_test import ( + "bytes" + "errors" + "io" "strings" "testing" "github.com/eugenetriguba/bolt/internal/sqlparse" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) func TestSqlParser_Parse(t *testing.T) { - type test struct { + testCases := []struct { buffer string - expectedExecutionOptions *sqlparse.ExecutionOptions - } - - tests := []test{ + 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 tests { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer)) + for _, tc := range testCases { + sqlParser := sqlparse.NewSqlParser() - execOptions, err := sqlParser.Parse() - assert.NilError(t, err) + execOptions, err := sqlParser.Parse(strings.NewReader(tc.buffer)) + assert.Nil(t, err) 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() + _, 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 9032dc3..a9e35af 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -2,29 +2,40 @@ package storage import ( "database/sql" + "errors" "fmt" "github.com/eugenetriguba/bolt/internal/configloader" _ "github.com/lib/pq" ) +var ( + 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, err + return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, 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("%w: %v", ErrUnableToConnect, err) } return db, nil diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index d9e22d0..e85cc57 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -7,29 +7,31 @@ 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/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)) - assert.NilError(t, err) + assert.Nil(t, err) defer conn.Close() _, err = conn.Exec("SELECT 1;") - assert.NilError(t, err) + 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.ErrMalformedConnectionString) assert.ErrorContains(t, err, `unknown driver "redis"`) } |