diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 23:21:39 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 23:21:39 -0500 |
| commit | 2212743dcf34fa34aeebbb2292152cc4d9d6dcdf (patch) | |
| tree | b3b2370325f62685102ba6be01ff985ddab30b73 /internal/repositories | |
| parent | Update configuration format and env names (diff) | |
| download | cli-2212743dcf34fa34aeebbb2292152cc4d9d6dcdf.zip | |
Add support for turning off transaction execution
Diffstat (limited to 'internal/repositories')
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 107 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo_test.go | 55 |
2 files changed, 130 insertions, 32 deletions
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 6a37e7b..42cf0e8 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/bolt/internal/sqlparse" ) type MigrationDBRepo struct { @@ -87,27 +88,48 @@ func (mr *MigrationDBRepo) Apply( upgradeScript string, migration *models.Migration, ) error { - tx, err := mr.db.Begin() + sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript)) + execOptions, err := sqlParser.Parse() if err != nil { return err } - defer tx.Rollback() - _, err = tx.Exec(upgradeScript) - if err != nil { - return err - } + if execOptions.UseTransaction { + tx, err := mr.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() - _, err = tx.Exec( - `INSERT INTO bolt_migrations(version) VALUES ($1);`, - migration.Version, - ) - if err != nil { - return err - } - err = tx.Commit() - if err != nil { - return err + _, err = tx.Exec(upgradeScript) + if err != nil { + return err + } + + _, err = tx.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + if err != nil { + return err + } + err = tx.Commit() + if err != nil { + return err + } + } else { + _, err = mr.db.Exec(upgradeScript) + if err != nil { + return err + } + + _, err = mr.db.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + if err != nil { + return err + } } migration.Applied = true @@ -121,28 +143,49 @@ func (mr *MigrationDBRepo) Revert( downgradeScript string, migration *models.Migration, ) error { - tx, err := mr.db.Begin() + sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript)) + execOptions, err := sqlParser.Parse() if err != nil { return err } - defer tx.Rollback() - _, err = tx.Exec(downgradeScript) - if err != nil { - return err - } + if execOptions.UseTransaction { + tx, err := mr.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() - _, err = tx.Exec( - `DELETE FROM bolt_migrations WHERE version = $1;`, - migration.Version, - ) - if err != nil { - return err - } + _, err = tx.Exec(downgradeScript) + if err != nil { + return err + } - err = tx.Commit() - if err != nil { - return err + _, err = tx.Exec( + `DELETE FROM bolt_migrations WHERE version = $1;`, + migration.Version, + ) + if err != nil { + return err + } + + err = tx.Commit() + if err != nil { + return err + } + } else { + _, err = mr.db.Exec(downgradeScript) + if err != nil { + return err + } + + _, err = mr.db.Exec( + `DELETE FROM bolt_migrations WHERE version = $1;`, + migration.Version, + ) + if err != nil { + return err + } } migration.Applied = false diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index a38875f..55b8eb2 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -186,3 +186,58 @@ func TestMigrationDBRepo_Revert(t *testing.T) { assert.NilError(t, err) assert.Equal(t, count, 0) } + +func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(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.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 TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + migration := models.NewMigration(time.Now(), "test") + + err = repo.Apply("this is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) + + err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) +} + +func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + migration := models.NewMigration(time.Now(), "test") + migration.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\nthis is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, true) +} |