aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration_db_repo_test.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-13 23:21:39 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-13 23:21:39 -0500
commit2212743dcf34fa34aeebbb2292152cc4d9d6dcdf (patch)
treeb3b2370325f62685102ba6be01ff985ddab30b73 /internal/repositories/migration_db_repo_test.go
parentUpdate configuration format and env names (diff)
downloadcli-2212743dcf34fa34aeebbb2292152cc4d9d6dcdf.zip
Add support for turning off transaction execution
Diffstat (limited to 'internal/repositories/migration_db_repo_test.go')
-rw-r--r--internal/repositories/migration_db_repo_test.go55
1 files changed, 55 insertions, 0 deletions
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)
+}