diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 17:47:31 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 17:47:31 -0500 |
| commit | 95eea2a07198576e3f4b0a246dd5e2b0cd4ef219 (patch) | |
| tree | 7b63135924558a706322d441ae78b59d4bbd2d71 /internal/repositories | |
| parent | Add test for invalid version style in config (diff) | |
| download | cli-95eea2a07198576e3f4b0a246dd5e2b0cd4ef219.zip | |
Wrap db repo and console errs to give better errors
Diffstat (limited to 'internal/repositories')
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 53 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo.go | 4 |
2 files changed, 42 insertions, 15 deletions
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 42cf0e8..93b9c73 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -3,6 +3,7 @@ package repositories import ( "database/sql" "errors" + "fmt" "strings" "github.com/eugenetriguba/bolt/internal/models" @@ -91,19 +92,22 @@ func (mr *MigrationDBRepo) Apply( sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript)) execOptions, err := sqlParser.Parse() if err != nil { - return err + return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) } if execOptions.UseTransaction { tx, err := mr.db.Begin() if err != nil { - return err + return fmt.Errorf( + "unable to start transaction during upgrade script execution: %w", + err, + ) } defer tx.Rollback() _, err = tx.Exec(upgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute upgrade script: %w", err) } _, err = tx.Exec( @@ -111,16 +115,22 @@ func (mr *MigrationDBRepo) Apply( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to insert applied migration to bolt_migrations table: %w", + err, + ) } err = tx.Commit() if err != nil { - return err + return fmt.Errorf( + "unable to commit transaction during upgrade script execution: %w", + err, + ) } } else { _, err = mr.db.Exec(upgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute upgrade script: %w", err) } _, err = mr.db.Exec( @@ -128,7 +138,10 @@ func (mr *MigrationDBRepo) Apply( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to insert applied migration to bolt_migrations table: %w", + err, + ) } } @@ -146,19 +159,22 @@ func (mr *MigrationDBRepo) Revert( sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript)) execOptions, err := sqlParser.Parse() if err != nil { - return err + return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) } if execOptions.UseTransaction { tx, err := mr.db.Begin() if err != nil { - return err + return fmt.Errorf( + "unable to start transaction during downgrade script execution: %w", + err, + ) } defer tx.Rollback() _, err = tx.Exec(downgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute downgrade script: %w", err) } _, err = tx.Exec( @@ -166,17 +182,23 @@ func (mr *MigrationDBRepo) Revert( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) } err = tx.Commit() if err != nil { - return err + return fmt.Errorf( + "unable to commit transaction during downgrade script execution: %w", + err, + ) } } else { _, err = mr.db.Exec(downgradeScript) if err != nil { - return err + return fmt.Errorf("unable to execute downgrade script: %w", err) } _, err = mr.db.Exec( @@ -184,7 +206,10 @@ func (mr *MigrationDBRepo) Revert( migration.Version, ) if err != nil { - return err + return fmt.Errorf( + "unable to remove reverted migration from bolt_migrations table: %w", + err, + ) } } diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 65afe5f..c541006 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -27,7 +27,9 @@ type MigrationFsRepo struct { migrationsDirPath string } -func NewMigrationFsRepo(migrationsConfig *configloader.MigrationsConfig) (*MigrationFsRepo, error) { +func NewMigrationFsRepo( + migrationsConfig *configloader.MigrationsConfig, +) (*MigrationFsRepo, error) { fileInfo, err := os.Stat(migrationsConfig.DirectoryPath) if errors.Is(err, os.ErrNotExist) { err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755) |