aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration_db_repo_test.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-08-18 14:51:00 -0400
committerGitHub <noreply@github.com>2024-08-18 14:51:00 -0400
commitd2186ed58d907a81648aa62ffa17ec20fb2d213e (patch)
treea5b2e26f1f96f333d48a8b20126ecc9ead697029 /internal/repositories/migration_db_repo_test.go
parentMultiple issue fixes in readme (#3) (diff)
downloadcli-d2186ed58d907a81648aa62ffa17ec20fb2d213e.zip
GH-4: Add configurable migrations table name (#6)
* Add configurable migrations table name * Update codecov to be informational * Add mockdb and couple of extra tests * remove extra files * Add list query error test * Add readme update for custom migration table
Diffstat (limited to 'internal/repositories/migration_db_repo_test.go')
-rw-r--r--internal/repositories/migration_db_repo_test.go112
1 files changed, 97 insertions, 15 deletions
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index 37efe37..1163b9c 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -1,6 +1,8 @@
package repositories_test
import (
+ "database/sql"
+ "errors"
"testing"
"time"
@@ -16,7 +18,7 @@ func TestNewMigrationDBRepo_CreatesTable(t *testing.T) {
assert.Nil(t, err)
assert.False(t, exists)
- _, err = repositories.NewMigrationDBRepo(testdb)
+ _, err = repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
exists, err = testdb.TableExists("bolt_migrations")
@@ -24,6 +26,31 @@ func TestNewMigrationDBRepo_CreatesTable(t *testing.T) {
assert.True(t, exists)
}
+func TestNewMigrationDBRepo_CreateTableExecError(t *testing.T) {
+ mockDB := &bolttest.MockDB{
+ TableExistsFunc: func(tableName string) (bool, error) {
+ return false, nil
+ },
+ ExecFunc: func(query string, args ...interface{}) (sql.Result, error) {
+ return nil, errors.New("exec error")
+ },
+ }
+ _, err := repositories.NewMigrationDBRepo("bolt_migrations", mockDB)
+ assert.NotNil(t, err)
+ assert.ErrorContains(t, err, "unable to create 'bolt_migrations' database table: exec error")
+}
+
+func TestNewMigrationDBRepo_TableExistsError(t *testing.T) {
+ mockDB := &bolttest.MockDB{
+ TableExistsFunc: func(tableName string) (bool, error) {
+ return false, errors.New("table exists failed")
+ },
+ }
+ _, err := repositories.NewMigrationDBRepo("bolt_migrations", mockDB)
+ assert.NotNil(t, err)
+ assert.ErrorContains(t, err, "unable to confirm 'bolt_migrations' database table exists: table exists failed")
+}
+
func TestNewMigrationDBRepo_TableAlreadyExists(t *testing.T) {
testdb := bolttest.NewTestDB(t)
_, err := testdb.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`)
@@ -31,7 +58,7 @@ func TestNewMigrationDBRepo_TableAlreadyExists(t *testing.T) {
_, err = testdb.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`)
assert.Nil(t, err)
- _, err = repositories.NewMigrationDBRepo(testdb)
+ _, err = repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
var count int
@@ -46,7 +73,7 @@ func TestNewMigrationDBRepo_TableAlreadyExists(t *testing.T) {
func TestList_EmptyTable(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
migrations, err := repo.List()
@@ -54,9 +81,26 @@ func TestList_EmptyTable(t *testing.T) {
assert.Equal(t, len(migrations), 0)
}
+func TestList_QueryError(t *testing.T) {
+ mockDB := &bolttest.MockDB{
+ TableExistsFunc: func(tableName string) (bool, error) {
+ return true, nil
+ },
+ QueryFunc: func(query string, args ...interface{}) (*sql.Rows, error) {
+ return nil, errors.New("query error")
+ },
+ }
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", mockDB)
+ assert.Nil(t, err)
+
+ _, err = repo.List()
+ assert.NotNil(t, err)
+ assert.ErrorContains(t, err, "unable to execute query to select versions from 'bolt_migrations' database table: query error")
+}
+
func TestList_SingleResult(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
version := "20230101000000"
@@ -75,7 +119,7 @@ func TestList_SingleResult(t *testing.T) {
func TestList_ShortVersion(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
version := "20230101"
@@ -94,7 +138,7 @@ func TestList_ShortVersion(t *testing.T) {
func TestIsApplied_WithNotApplied(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
version := "20230101010101"
@@ -105,7 +149,7 @@ func TestIsApplied_WithNotApplied(t *testing.T) {
func TestIsApplied_WithApplied(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
version := "20230101010101"
@@ -117,9 +161,24 @@ func TestIsApplied_WithApplied(t *testing.T) {
assert.Equal(t, applied, true)
}
+func TestIsApplied_SQLInjectionAttempt(t *testing.T) {
+ db := bolttest.NewTestDB(t)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
+ assert.Nil(t, err)
+
+ version := "20230101010101'; DROP TABLE bolt_migrations; --"
+ applied, err := repo.IsApplied(version)
+ assert.Nil(t, err)
+ assert.Equal(t, applied, false)
+
+ exists, err := db.TableExists("bolt_migrations")
+ assert.Nil(t, err)
+ assert.True(t, exists)
+}
+
func TestApply(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(testdb)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
@@ -137,7 +196,7 @@ func TestApply(t *testing.T) {
func TestApply_MalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
@@ -149,7 +208,7 @@ func TestApply_MalformedSql(t *testing.T) {
func TestApplyWithTx_ExecErr(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
@@ -160,7 +219,7 @@ func TestApplyWithTx_ExecErr(t *testing.T) {
func TestApplyWithTx_SuccessfullyApplied(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(testdb)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
@@ -178,7 +237,7 @@ func TestApplyWithTx_SuccessfullyApplied(t *testing.T) {
func TestRevert(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(testdb)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
_, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
@@ -207,7 +266,7 @@ func TestRevert(t *testing.T) {
func TestRevert_MalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
migration.Applied = true
@@ -219,7 +278,7 @@ func TestRevert_MalformedSql(t *testing.T) {
func TestRevertWithTx_ExecErr(t *testing.T) {
db := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(db)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
@@ -230,7 +289,7 @@ func TestRevertWithTx_ExecErr(t *testing.T) {
func TestRevertWithTx_SuccessfullyReverted(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- repo, err := repositories.NewMigrationDBRepo(testdb)
+ repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
_, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
@@ -256,3 +315,26 @@ func TestRevertWithTx_SuccessfullyReverted(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, count, 0)
}
+
+func TestNewMigrationDBRepo_InvalidTableName(t *testing.T) {
+ db := bolttest.NewTestDB(t)
+ invalidTableNames := []string{
+ "bolt_migrations; DROP TABLE users; --", // SQL injection attempt
+ "bolt_migrations`", // Backtick is not allowed
+ "bolt_migrations'", // Single quote is not allowed
+ "bolt_migrations\"", // Double quote is not allowed
+ "bolt_migrations;", // Semicolon could terminate the SQL statement
+ "bolt_migrations\\", // Backslash is not allowed
+ "myschema..migrations_table", // Double dot, which is invalid for schema.table format
+ "myschema.migrations_table.", // Trailing dot is not allowed
+ "myschema.migrations_table;", // Semicolon in schema-qualified name is not allowed
+ ".migrations_table", // Leading dot is not allowed
+ "myschema.migrations_table$", // Dollar sign is not allowed
+ }
+
+ for _, tableName := range invalidTableNames {
+ _, err := repositories.NewMigrationDBRepo(tableName, db)
+ assert.NotNil(t, err)
+ assert.ErrorContains(t, err, "invalid migration table name")
+ }
+}