diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-29 19:53:20 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-29 19:53:20 -0500 |
| commit | bf1d3b3e1fc42132678b244e479839416a03a6ce (patch) | |
| tree | fbe4f5e7e41b77bc990ebdece673c7f16102f61f /internal/repositories | |
| parent | Tweak error and doc for storage module (diff) | |
| download | cli-bf1d3b3e1fc42132678b244e479839416a03a6ce.zip | |
Add service tests
Diffstat (limited to 'internal/repositories')
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 235 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo_test.go | 2 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo.go | 40 |
3 files changed, 155 insertions, 122 deletions
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index b9e2d78..500fea4 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -7,10 +7,22 @@ import ( "strings" "github.com/eugenetriguba/bolt/internal/models" - "github.com/eugenetriguba/bolt/internal/sqlparse" ) -type MigrationDBRepo struct { +type SQLExecutor interface { + Exec(query string, args ...interface{}) (sql.Result, error) +} + +type MigrationDBRepo interface { + List() (map[string]*models.Migration, error) + IsApplied(version string) (bool, error) + Apply(upgradeScript string, migration *models.Migration) error + ApplyWithTx(upgradeScript string, migration *models.Migration) error + Revert(downgradeScript string, migration *models.Migration) error + RevertWithTx(downgradeScript string, migration *models.Migration) error +} + +type migrationDBRepo struct { db *sql.DB } @@ -18,7 +30,7 @@ type MigrationDBRepo struct { // database. Furthermore, it ensures the migration table it // operates on exists. If it is unable to create or confirm // the table exists, an error is returned. -func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { +func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS bolt_migrations( version CHARACTER(14) PRIMARY KEY NOT NULL @@ -31,14 +43,14 @@ func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) { ) } - return &MigrationDBRepo{db: db}, nil + return &migrationDBRepo{db: db}, nil } // List retrieves a map of migration models that can be // looked up by their `Version`. All migrations retrieved // will be ones that have been applied, and their message // will always be an empty string. -func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { +func (mr migrationDBRepo) List() (map[string]*models.Migration, error) { rows, err := mr.db.Query(`SELECT version FROM bolt_migrations;`) if err != nil { return nil, fmt.Errorf( @@ -49,7 +61,7 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { } defer rows.Close() - var migrations = make(map[string]*models.Migration) + var migrations = make(map[string]*models.Migration, 0) for rows.Next() { var version string err := rows.Scan(&version) @@ -78,15 +90,19 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { // applied will be false when the version isn't applied and // when the version might be applied, but there was an error. // Check err first before looking at whether the version is applied. -func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) { +func (mr migrationDBRepo) IsApplied(version string) (bool, error) { row := mr.db.QueryRow(`SELECT 1 FROM bolt_migrations WHERE version = $1`, version) var scanResult int - err = row.Scan(&scanResult) + err := row.Scan(&scanResult) if errors.Is(err, sql.ErrNoRows) { return false, nil } if err != nil { - return false, err + return false, fmt.Errorf( + "unable to check whether the migration %s is applied: %w", + version, + err, + ) } return true, nil } @@ -95,134 +111,143 @@ func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) { // and adding the applied migration version into the migrations table. When // successfully applied, the `migration` model's `Applied` field will be set // to true. -func (mr *MigrationDBRepo) Apply( +func (mr migrationDBRepo) Apply( upgradeScript string, migration *models.Migration, ) error { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript)) - execOptions, err := sqlParser.Parse() + err := applyMigration(mr.db, upgradeScript, *migration) if err != nil { - return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) + return err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return fmt.Errorf( - "unable to start transaction during upgrade script execution: %w", - err, - ) - } - defer tx.Rollback() - - _, err = tx.Exec(upgradeScript) - if err != nil { - return fmt.Errorf("unable to execute upgrade script: %w", err) - } + migration.Applied = true + return nil +} - _, err = tx.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, +// ApplyWithTx applies a migration like Apply. However, it +// wraps the operation is a database transaction. +func (mr migrationDBRepo) ApplyWithTx( + upgradeScript string, + migration *models.Migration, +) error { + tx, err := mr.db.Begin() + if err != nil { + return fmt.Errorf( + "unable to start transaction during upgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to insert applied migration to bolt_migrations table: %w", - err, - ) - } - err = tx.Commit() - if err != nil { - return fmt.Errorf( - "unable to commit transaction during upgrade script execution: %w", - err, - ) - } - } else { - _, err = mr.db.Exec(upgradeScript) - if err != nil { - return fmt.Errorf("unable to execute upgrade script: %w", err) - } + } + defer tx.Rollback() - _, err = mr.db.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, + err = applyMigration(tx, upgradeScript, *migration) + if err != nil { + return err + } + + err = tx.Commit() + if err != nil { + return fmt.Errorf( + "unable to commit transaction during upgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to insert applied migration to bolt_migrations table: %w", - err, - ) - } } migration.Applied = true return nil } +func applyMigration( + executor SQLExecutor, + upgradeScript string, + migration models.Migration, +) error { + _, err := executor.Exec(upgradeScript) + if err != nil { + return fmt.Errorf("unable to execute upgrade script: %w", err) + } + + _, err = executor.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + if err != nil { + return fmt.Errorf( + "unable to insert applied migration: %w", + err, + ) + } + + return nil +} + // Revert reverts a migration by executing the corresponding downgrade script // and deleting the migration version from the migrations table. When successfully // reverted, the `migration` model's `Applied` field will be set to false. -func (mr *MigrationDBRepo) Revert( +func (mr migrationDBRepo) Revert( downgradeScript string, migration *models.Migration, ) error { - sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript)) - execOptions, err := sqlParser.Parse() + err := revertMigration(mr.db, downgradeScript, *migration) if err != nil { - return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) + return err } - if execOptions.UseTransaction { - tx, err := mr.db.Begin() - if err != nil { - return fmt.Errorf( - "unable to start transaction during downgrade script execution: %w", - err, - ) - } - defer tx.Rollback() - - _, err = tx.Exec(downgradeScript) - if err != nil { - return fmt.Errorf("unable to execute downgrade script: %w", err) - } + migration.Applied = false + return nil +} - _, err = tx.Exec( - `DELETE FROM bolt_migrations WHERE version = $1;`, - migration.Version, +// RevertWithTx reverts a migration like Revert. However, it +// wraps the operation is a database transaction. +func (mr migrationDBRepo) RevertWithTx( + downgradeScript string, + migration *models.Migration, +) error { + tx, err := mr.db.Begin() + if err != nil { + return fmt.Errorf( + "unable to start transaction during downgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to remove reverted migration from bolt_migrations table: %w", - err, - ) - } + } + defer tx.Rollback() - err = tx.Commit() - if err != nil { - return fmt.Errorf( - "unable to commit transaction during downgrade script execution: %w", - err, - ) - } - } else { - _, err = mr.db.Exec(downgradeScript) - if err != nil { - return fmt.Errorf("unable to execute downgrade script: %w", err) - } + err = revertMigration(tx, downgradeScript, *migration) + if err != nil { + return err + } - _, err = mr.db.Exec( - `DELETE FROM bolt_migrations WHERE version = $1;`, - migration.Version, + err = tx.Commit() + if err != nil { + return fmt.Errorf( + "unable to commit transaction during downgrade script execution: %w", + err, ) - if err != nil { - return fmt.Errorf( - "unable to remove reverted migration from bolt_migrations table: %w", - err, - ) - } } migration.Applied = false return nil } + +func revertMigration( + executor SQLExecutor, + downgradeScript string, + migration models.Migration, +) error { + _, err := executor.Exec(downgradeScript) + if err != nil { + return fmt.Errorf("unable to execute downgrade script: %w", err) + } + + _, err = executor.Exec( + `DELETE FROM bolt_migrations WHERE version = $1;`, + migration.Version, + ) + if err != nil { + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) + } + + return nil +} diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 4e41b36..9f4f9e8 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -197,7 +197,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. err = repo.Apply( - `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, + `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) assert.Nil(t, err) diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index c541006..9d21c59 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -23,13 +23,23 @@ func (e *ErrIsNotDir) Error() string { ) } -type MigrationFsRepo struct { +type MigrationFsRepo interface { + Create(migration *models.Migration) error + Exists(version string) (bool, error) + Get(version string) (*models.Migration, error) + List() (map[string]*models.Migration, error) + Latest() (*models.Migration, error) + ReadUpgradeScript(migration *models.Migration) (string, error) + ReadDowngradeScript(migration *models.Migration) (string, error) +} + +type migrationFsRepo struct { migrationsDirPath string } func NewMigrationFsRepo( migrationsConfig *configloader.MigrationsConfig, -) (*MigrationFsRepo, error) { +) (MigrationFsRepo, error) { fileInfo, err := os.Stat(migrationsConfig.DirectoryPath) if errors.Is(err, os.ErrNotExist) { err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755) @@ -42,10 +52,10 @@ func NewMigrationFsRepo( return nil, &ErrIsNotDir{path: migrationsConfig.DirectoryPath} } - return &MigrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil + return &migrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil } -func (mr *MigrationFsRepo) Create(migration *models.Migration) error { +func (mr migrationFsRepo) Create(migration *models.Migration) error { newMigrationDir := filepath.Join(mr.migrationsDirPath, mr.migrationDirname(migration)) err := os.Mkdir(newMigrationDir, 0755) @@ -66,7 +76,7 @@ func (mr *MigrationFsRepo) Create(migration *models.Migration) error { return nil } -func (mr *MigrationFsRepo) Exists(version string) (bool, error) { +func (mr migrationFsRepo) Exists(version string) (bool, error) { migrations, err := mr.List() if err != nil { return false, err @@ -76,7 +86,7 @@ func (mr *MigrationFsRepo) Exists(version string) (bool, error) { return ok, nil } -func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) { +func (mr migrationFsRepo) Get(version string) (*models.Migration, error) { migrations, err := mr.List() if err != nil { return nil, err @@ -90,7 +100,7 @@ func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) { return migration, nil } -func (mr *MigrationFsRepo) Latest() (*models.Migration, error) { +func (mr migrationFsRepo) Latest() (*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { return nil, err @@ -103,13 +113,13 @@ func (mr *MigrationFsRepo) Latest() (*models.Migration, error) { return nil, nil } -func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) { +func (mr migrationFsRepo) List() (map[string]*models.Migration, error) { entries, err := os.ReadDir(mr.migrationsDirPath) if err != nil { return nil, err } - var migrations = make(map[string]*models.Migration) + var migrations = make(map[string]*models.Migration, 0) for _, entry := range entries { migration, err := dirEntryToMigration(entry) if err != nil { @@ -137,9 +147,7 @@ func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) { }, nil } -func (mr *MigrationFsRepo) ReadUpgradeScript( - migration *models.Migration, -) (string, error) { +func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) { upgradeScriptPath := filepath.Join( mr.migrationsDirPath, mr.migrationDirname(migration), @@ -148,7 +156,7 @@ func (mr *MigrationFsRepo) ReadUpgradeScript( return mr.readScriptContents(upgradeScriptPath) } -func (mr *MigrationFsRepo) ReadDowngradeScript( +func (mr migrationFsRepo) ReadDowngradeScript( migration *models.Migration, ) (string, error) { downgradeScriptPath := filepath.Join( @@ -159,7 +167,7 @@ func (mr *MigrationFsRepo) ReadDowngradeScript( return mr.readScriptContents(downgradeScriptPath) } -func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error) { +func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) { contents, err := os.ReadFile(scriptPath) if err != nil { return "", err @@ -170,13 +178,13 @@ func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error) // migrationDirname creates the directory name that // should be used for this migration. -func (mr *MigrationFsRepo) migrationDirname(migration *models.Migration) string { +func (mr migrationFsRepo) migrationDirname(migration *models.Migration) string { return fmt.Sprintf("%s_%s", migration.Version, mr.normalizeMessage(migration)) } // normalizeMessage normalizes the Message of a migration // to be filesystem friendly. -func (mr *MigrationFsRepo) normalizeMessage(migration *models.Migration) string { +func (mr migrationFsRepo) normalizeMessage(migration *models.Migration) string { message := strings.ToLower(migration.Message) message = strings.TrimSpace(message) message = strings.ReplaceAll(message, " ", "_") |