diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 09:29:41 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 09:29:41 -0500 |
| commit | 6760ae82d62449f41c68950264c02c40be81bfb3 (patch) | |
| tree | f071906933642e93e789b5def170985f366171ce /internal/repositories/migration_db_repo.go | |
| parent | Add proper tests to revert service methods (diff) | |
| download | cli-6760ae82d62449f41c68950264c02c40be81bfb3.zip | |
Improve coverage for separate Tx methods
Diffstat (limited to 'internal/repositories/migration_db_repo.go')
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 500fea4..7a90a2a 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -9,10 +9,17 @@ import ( "github.com/eugenetriguba/bolt/internal/models" ) -type SQLExecutor interface { +type sqlExecutor interface { Exec(query string, args ...interface{}) (sql.Result, error) } +type SqlDb interface { + Exec(query string, args ...any) (sql.Result, error) + Query(query string, args ...any) (*sql.Rows, error) + QueryRow(query string, args ...any) *sql.Row + Begin() (*sql.Tx, error) +} + type MigrationDBRepo interface { List() (map[string]*models.Migration, error) IsApplied(version string) (bool, error) @@ -23,14 +30,14 @@ type MigrationDBRepo interface { } type migrationDBRepo struct { - db *sql.DB + db SqlDb } // NewMigrationDBRepo initializes the MigrationDBRepo with a // database. Furthermore, it ensures the migration table it // operates on exists. If it is unable to create or confirm // the table exists, an error is returned. -func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { +func NewMigrationDBRepo(db SqlDb) (MigrationDBRepo, error) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS bolt_migrations( version CHARACTER(14) PRIMARY KEY NOT NULL @@ -38,7 +45,7 @@ func NewMigrationDBRepo(db *sql.DB) (MigrationDBRepo, error) { `) if err != nil { return nil, fmt.Errorf( - "unable to create bolt_migrations database table: %w", + "unable to confirm bolt_migrations database table exists: %w", err, ) } @@ -157,7 +164,7 @@ func (mr migrationDBRepo) ApplyWithTx( } func applyMigration( - executor SQLExecutor, + executor sqlExecutor, upgradeScript string, migration models.Migration, ) error { @@ -229,7 +236,7 @@ func (mr migrationDBRepo) RevertWithTx( } func revertMigration( - executor SQLExecutor, + executor sqlExecutor, downgradeScript string, migration models.Migration, ) error { |