aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-02-29 19:53:20 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-02-29 19:53:20 -0500
commitbf1d3b3e1fc42132678b244e479839416a03a6ce (patch)
treefbe4f5e7e41b77bc990ebdece673c7f16102f61f
parentTweak error and doc for storage module (diff)
downloadcli-bf1d3b3e1fc42132678b244e479839416a03a6ce.zip
Add service tests
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--internal/bolttest/mock_migration_db_repo.go78
-rw-r--r--internal/bolttest/mock_migration_fs_repo.go82
-rw-r--r--internal/bolttest/null_outputer.go19
-rw-r--r--internal/commands/down.go28
-rw-r--r--internal/commands/new.go16
-rw-r--r--internal/commands/status.go34
-rw-r--r--internal/commands/up.go28
-rw-r--r--internal/configloader/configloader.go2
-rw-r--r--internal/configloader/configloader_test.go2
-rw-r--r--internal/models/migration_test.go6
-rw-r--r--internal/repositories/migration_db_repo.go235
-rw-r--r--internal/repositories/migration_db_repo_test.go2
-rw-r--r--internal/repositories/migration_fs_repo.go40
-rw-r--r--internal/services/migration.go233
-rw-r--r--internal/services/migration_test.go595
-rw-r--r--internal/sqlparse/sqlparse.go18
-rw-r--r--internal/sqlparse/sqlparse_test.go24
-rw-r--r--internal/storage/db.go6
20 files changed, 1197 insertions, 255 deletions
diff --git a/go.mod b/go.mod
index d16b144..0ff1308 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.21.6
require (
github.com/BurntSushi/toml v1.3.2
- github.com/eugenetriguba/checkmate v0.3.0
+ github.com/eugenetriguba/checkmate v0.3.2
github.com/google/subcommands v1.2.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/lib/pq v1.10.9
diff --git a/go.sum b/go.sum
index 9fed2a4..92017b6 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/eugenetriguba/checkmate v0.3.0 h1:JqeefDNUGga9VG+jusq2UlVKCFKBlRN1p5JQbl1CmLI=
github.com/eugenetriguba/checkmate v0.3.0/go.mod h1:IY38Cl4zlNI1dg+wcDYwrbWZfJ9i2/ouWf8C0N8c/do=
+github.com/eugenetriguba/checkmate v0.3.2 h1:vcALwpLLpt0FBxckjuobNFYtBLtlCT2p55Xk4p0SAhk=
+github.com/eugenetriguba/checkmate v0.3.2/go.mod h1:IY38Cl4zlNI1dg+wcDYwrbWZfJ9i2/ouWf8C0N8c/do=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
diff --git a/internal/bolttest/mock_migration_db_repo.go b/internal/bolttest/mock_migration_db_repo.go
new file mode 100644
index 0000000..bdcc928
--- /dev/null
+++ b/internal/bolttest/mock_migration_db_repo.go
@@ -0,0 +1,78 @@
+package bolttest
+
+import "github.com/eugenetriguba/bolt/internal/models"
+
+type MockMigrationDBRepo struct {
+ ListReturnValue ListReturnValue
+ ListCallCount int
+ IsAppliedReturnValue IsAppliedReturnValue
+ IsAppliedCallCount int
+ ApplyReturnValue ApplyReturnValue
+ ApplyCallCount int
+ ApplyWithTxReturnValue ApplyWithTxReturnValue
+ ApplyWithTxCallCount int
+ RevertReturnValue RevertReturnValue
+ RevertCallCount int
+ RevertWithTxReturnValue RevertWithTxReturnValue
+ RevertWithTxCallCount int
+}
+
+type ListReturnValue struct {
+ Migrations map[string]*models.Migration
+ Err error
+}
+
+type IsAppliedReturnValue struct {
+ IsApplied bool
+ Err error
+}
+
+type ApplyReturnValue struct {
+ Err error
+}
+
+type ApplyWithTxReturnValue = ApplyReturnValue
+type RevertReturnValue = ApplyReturnValue
+type RevertWithTxReturnValue = ApplyReturnValue
+
+func (repo *MockMigrationDBRepo) List() (map[string]*models.Migration, error) {
+ repo.ListCallCount += 1
+ return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err
+}
+
+func (repo *MockMigrationDBRepo) IsApplied(version string) (bool, error) {
+ repo.IsAppliedCallCount += 1
+ return repo.IsAppliedReturnValue.IsApplied, repo.IsAppliedReturnValue.Err
+}
+
+func (repo *MockMigrationDBRepo) Apply(
+ upgradeScript string,
+ migration *models.Migration,
+) error {
+ repo.ApplyCallCount += 1
+ return repo.ApplyReturnValue.Err
+}
+
+func (repo *MockMigrationDBRepo) ApplyWithTx(
+ upgradeScript string,
+ migration *models.Migration,
+) error {
+ repo.ApplyWithTxCallCount += 1
+ return repo.ApplyWithTxReturnValue.Err
+}
+
+func (repo *MockMigrationDBRepo) Revert(
+ downgradeScript string,
+ migration *models.Migration,
+) error {
+ repo.RevertCallCount += 1
+ return repo.RevertReturnValue.Err
+}
+
+func (repo *MockMigrationDBRepo) RevertWithTx(
+ downgradeScript string,
+ migration *models.Migration,
+) error {
+ repo.RevertWithTxCallCount += 1
+ return repo.RevertWithTxReturnValue.Err
+}
diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go
new file mode 100644
index 0000000..0932b37
--- /dev/null
+++ b/internal/bolttest/mock_migration_fs_repo.go
@@ -0,0 +1,82 @@
+package bolttest
+
+import "github.com/eugenetriguba/bolt/internal/models"
+
+type MockMigrationFsRepo struct {
+ CreateReturnValue CreateReturnValue
+ CreateCallCount int
+ ExistsReturnValue ExistsReturnValue
+ ExistsCallCount int
+ GetReturnValue GetReturnValue
+ GetCallCount int
+ ListReturnValue ListReturnValue
+ ListCallCount int
+ LatestReturnValue LatestReturnValue
+ LatestCallCount int
+ ReadUpgradeScriptReturnValue ReadUpgradeScriptReturnValue
+ ReadUpgradeScriptCallCount int
+ ReadDowngradeScriptReturnValue ReadDowngradeScriptReturnValue
+ ReadDowngradeScriptCallCount int
+}
+
+type CreateReturnValue struct {
+ Err error
+}
+
+type ExistsReturnValue struct {
+ Exists bool
+ Err error
+}
+
+type GetReturnValue struct {
+ Migration *models.Migration
+ Err error
+}
+
+type LatestReturnValue = GetReturnValue
+
+type ReadUpgradeScriptReturnValue struct {
+ ScriptContents string
+ Err error
+}
+
+type ReadDowngradeScriptReturnValue = ReadUpgradeScriptReturnValue
+
+func (repo *MockMigrationFsRepo) Create(migration *models.Migration) error {
+ repo.CreateCallCount += 1
+ return repo.CreateReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) Exists(version string) (bool, error) {
+ repo.ExistsCallCount += 1
+ return repo.ExistsReturnValue.Exists, repo.ExistsReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) Get(version string) (*models.Migration, error) {
+ repo.GetCallCount += 1
+ return repo.GetReturnValue.Migration, repo.GetReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) {
+ repo.ListCallCount += 1
+ return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) Latest() (*models.Migration, error) {
+ repo.LatestCallCount += 1
+ return repo.LatestReturnValue.Migration, repo.LatestReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) ReadUpgradeScript(
+ migration *models.Migration,
+) (string, error) {
+ repo.ReadUpgradeScriptCallCount += 1
+ return repo.ReadUpgradeScriptReturnValue.ScriptContents, repo.ReadUpgradeScriptReturnValue.Err
+}
+
+func (repo *MockMigrationFsRepo) ReadDowngradeScript(
+ migration *models.Migration,
+) (string, error) {
+ repo.ReadDowngradeScriptCallCount += 1
+ return repo.ReadDowngradeScriptReturnValue.ScriptContents, repo.ReadDowngradeScriptReturnValue.Err
+}
diff --git a/internal/bolttest/null_outputer.go b/internal/bolttest/null_outputer.go
new file mode 100644
index 0000000..f70b002
--- /dev/null
+++ b/internal/bolttest/null_outputer.go
@@ -0,0 +1,19 @@
+package bolttest
+
+type NullOutputter struct {
+ OutputLogs []string
+ ErrorLogs []string
+ TableLogs []string
+}
+
+func (o NullOutputter) Output(message string) error {
+ return nil
+}
+
+func (o NullOutputter) Error(err error) error {
+ return nil
+}
+
+func (o NullOutputter) Table(header []string, rows [][]string) error {
+ return nil
+}
diff --git a/internal/commands/down.go b/internal/commands/down.go
index 2b98011..8e88fc7 100644
--- a/internal/commands/down.go
+++ b/internal/commands/down.go
@@ -3,10 +3,10 @@ package commands
import (
"context"
"flag"
+ "fmt"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/output"
- "github.com/eugenetriguba/bolt/internal/repositories"
"github.com/eugenetriguba/bolt/internal/services"
"github.com/eugenetriguba/bolt/internal/storage"
"github.com/google/subcommands"
@@ -49,7 +49,7 @@ func (cmd *DownCmd) Execute(
cfg, err := configloader.NewConfig()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err))
return subcommands.ExitFailure
}
@@ -58,39 +58,31 @@ func (cmd *DownCmd) Execute(
storage.DBConnectionString(&cfg.Connection),
)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err))
return subcommands.ExitFailure
}
defer db.Close()
- migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
- if err != nil {
- consoleOutputter.Error(err)
- return subcommands.ExitFailure
- }
-
- migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
+ migrationService, err := services.NewMigrationServiceFromConfig(
+ db,
+ consoleOutputter,
+ *cfg,
+ )
if err != nil {
consoleOutputter.Error(err)
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(
- migrationDBRepo,
- migrationFsRepo,
- consoleOutputter,
- )
-
if cmd.version == "" {
err = migrationService.RevertAllMigrations()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to revert all migrations: %w", err))
return subcommands.ExitFailure
}
} else {
err = migrationService.RevertDownToVersion(cmd.version)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to revert migrations down to %s: %w", cmd.version, err))
return subcommands.ExitFailure
}
}
diff --git a/internal/commands/new.go b/internal/commands/new.go
index 7298dad..96ddd98 100644
--- a/internal/commands/new.go
+++ b/internal/commands/new.go
@@ -3,6 +3,7 @@ package commands
import (
"context"
"flag"
+ "fmt"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/output"
@@ -48,29 +49,28 @@ func (cmd *NewCmd) Execute(
cfg, err := configloader.NewConfig()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err))
return subcommands.ExitFailure
}
migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(
+ fmt.Errorf("unable to setup local migrations directory: %w", err),
+ )
return subcommands.ExitFailure
}
migrationService := services.NewMigrationService(
nil,
migrationFsRepo,
+ *cfg,
consoleOutputter,
)
- if err != nil {
- consoleOutputter.Error(err)
- return subcommands.ExitFailure
- }
- err = migrationService.CreateMigration(cfg.Migrations.VersionStyle, cmd.message)
+ _, err = migrationService.CreateMigration(cmd.message)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to create new migration: %w", err))
return subcommands.ExitFailure
}
diff --git a/internal/commands/status.go b/internal/commands/status.go
index 85f3824..eaf4332 100644
--- a/internal/commands/status.go
+++ b/internal/commands/status.go
@@ -3,10 +3,10 @@ package commands
import (
"context"
"flag"
+ "fmt"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/output"
- "github.com/eugenetriguba/bolt/internal/repositories"
"github.com/eugenetriguba/bolt/internal/services"
"github.com/eugenetriguba/bolt/internal/storage"
"github.com/google/subcommands"
@@ -39,7 +39,7 @@ func (m *StatusCmd) Execute(
cfg, err := configloader.NewConfig()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err))
return subcommands.ExitFailure
}
@@ -48,31 +48,24 @@ func (m *StatusCmd) Execute(
storage.DBConnectionString(&cfg.Connection),
)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err))
return subcommands.ExitFailure
}
defer db.Close()
- migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
- if err != nil {
- consoleOutputter.Error(err)
- return subcommands.ExitFailure
- }
-
- migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
+ migrationService, err := services.NewMigrationServiceFromConfig(
+ db,
+ consoleOutputter,
+ *cfg,
+ )
if err != nil {
consoleOutputter.Error(err)
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(
- migrationDBRepo,
- migrationFsRepo,
- consoleOutputter,
- )
migrations, err := migrationService.ListMigrations(services.SortOrderAsc)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to list migrations: %w", err))
return subcommands.ExitFailure
}
@@ -95,6 +88,13 @@ func (m *StatusCmd) Execute(
rows[i] = []string{migration.Version, migration.Message, applied}
}
- consoleOutputter.Table(headers, rows)
+ err = consoleOutputter.Table(headers, rows)
+ if err != nil {
+ consoleOutputter.Error(
+ fmt.Errorf("unable to output migrations as table: %w", err),
+ )
+ return subcommands.ExitFailure
+ }
+
return subcommands.ExitSuccess
}
diff --git a/internal/commands/up.go b/internal/commands/up.go
index 5cbc262..e10386a 100644
--- a/internal/commands/up.go
+++ b/internal/commands/up.go
@@ -3,10 +3,10 @@ package commands
import (
"context"
"flag"
+ "fmt"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/output"
- "github.com/eugenetriguba/bolt/internal/repositories"
"github.com/eugenetriguba/bolt/internal/services"
"github.com/eugenetriguba/bolt/internal/storage"
"github.com/google/subcommands"
@@ -49,7 +49,7 @@ func (cmd *UpCmd) Execute(
cfg, err := configloader.NewConfig()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err))
return subcommands.ExitFailure
}
@@ -58,39 +58,31 @@ func (cmd *UpCmd) Execute(
storage.DBConnectionString(&cfg.Connection),
)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to connect to database: %w", err))
return subcommands.ExitFailure
}
defer db.Close()
- migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
- if err != nil {
- consoleOutputter.Error(err)
- return subcommands.ExitFailure
- }
-
- migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
+ migrationService, err := services.NewMigrationServiceFromConfig(
+ db,
+ consoleOutputter,
+ *cfg,
+ )
if err != nil {
consoleOutputter.Error(err)
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(
- migrationDBRepo,
- migrationFsRepo,
- consoleOutputter,
- )
-
if cmd.version == "" {
err = migrationService.ApplyAllMigrations()
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to apply all migrations: %w", err))
return subcommands.ExitFailure
}
} else {
err = migrationService.ApplyUpToVersion(cmd.version)
if err != nil {
- consoleOutputter.Error(err)
+ consoleOutputter.Error(fmt.Errorf("unable to apply migrations up to %s: %w", cmd.version, err))
return subcommands.ExitFailure
}
}
diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go
index 6155d56..c2c05e7 100644
--- a/internal/configloader/configloader.go
+++ b/internal/configloader/configloader.go
@@ -71,7 +71,7 @@ func NewConfig() (*Config, error) {
cfg := Config{
Migrations: MigrationsConfig{
DirectoryPath: "migrations",
- VersionStyle: VersionStyleSequential,
+ VersionStyle: VersionStyleTimestamp,
},
}
if !errors.Is(err, ErrConfigFileNotFound) {
diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go
index 6ab21e0..0ad6930 100644
--- a/internal/configloader/configloader_test.go
+++ b/internal/configloader/configloader_test.go
@@ -18,7 +18,7 @@ func TestNewConfigDefaults(t *testing.T) {
assert.Nil(t, err)
check.Equal(t, cfg.Migrations.DirectoryPath, "migrations")
- check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential)
+ check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp)
}
func TestNewConfigWithInvalidVersionStyle(t *testing.T) {
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index a511c12..b6d3205 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -1,6 +1,7 @@
package models_test
import (
+ "fmt"
"testing"
"time"
@@ -31,3 +32,8 @@ func TestNewSequentialMigration(t *testing.T) {
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
}
+
+func TestName(t *testing.T) {
+ m := models.NewSequentialMigration(1, "test message")
+ check.Equal(t, m.Name(), fmt.Sprintf("%s_%s", m.Version, m.Message))
+}
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go
index b9e2d78..500fea4 100644
--- a/internal/repositories/migration_db_repo.go
+++ b/internal/repositories/migration_db_repo.go
@@ -7,10 +7,22 @@ import (
"strings"
"github.com/eugenetriguba/bolt/internal/models"
- "github.com/eugenetriguba/bolt/internal/sqlparse"
)
-type MigrationDBRepo struct {
+type SQLExecutor interface {
+ Exec(query string, args ...interface{}) (sql.Result, error)
+}
+
+type MigrationDBRepo interface {
+ List() (map[string]*models.Migration, error)
+ IsApplied(version string) (bool, error)
+ Apply(upgradeScript string, migration *models.Migration) error
+ ApplyWithTx(upgradeScript string, migration *models.Migration) error
+ Revert(downgradeScript string, migration *models.Migration) error
+ RevertWithTx(downgradeScript string, migration *models.Migration) error
+}
+
+type migrationDBRepo struct {
db *sql.DB
}
@@ -18,7 +30,7 @@ type MigrationDBRepo struct {
// 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 *sql.DB) (MigrationDBRepo, error) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS bolt_migrations(
version CHARACTER(14) PRIMARY KEY NOT NULL
@@ -31,14 +43,14 @@ func NewMigrationDBRepo(db *sql.DB) (*MigrationDBRepo, error) {
)
}
- return &MigrationDBRepo{db: db}, nil
+ return &migrationDBRepo{db: db}, nil
}
// List retrieves a map of migration models that can be
// looked up by their `Version`. All migrations retrieved
// will be ones that have been applied, and their message
// will always be an empty string.
-func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) {
+func (mr migrationDBRepo) List() (map[string]*models.Migration, error) {
rows, err := mr.db.Query(`SELECT version FROM bolt_migrations;`)
if err != nil {
return nil, fmt.Errorf(
@@ -49,7 +61,7 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) {
}
defer rows.Close()
- var migrations = make(map[string]*models.Migration)
+ var migrations = make(map[string]*models.Migration, 0)
for rows.Next() {
var version string
err := rows.Scan(&version)
@@ -78,15 +90,19 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) {
// applied will be false when the version isn't applied and
// when the version might be applied, but there was an error.
// Check err first before looking at whether the version is applied.
-func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) {
+func (mr migrationDBRepo) IsApplied(version string) (bool, error) {
row := mr.db.QueryRow(`SELECT 1 FROM bolt_migrations WHERE version = $1`, version)
var scanResult int
- err = row.Scan(&scanResult)
+ err := row.Scan(&scanResult)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
- return false, err
+ return false, fmt.Errorf(
+ "unable to check whether the migration %s is applied: %w",
+ version,
+ err,
+ )
}
return true, nil
}
@@ -95,134 +111,143 @@ func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) {
// and adding the applied migration version into the migrations table. When
// successfully applied, the `migration` model's `Applied` field will be set
// to true.
-func (mr *MigrationDBRepo) Apply(
+func (mr migrationDBRepo) Apply(
upgradeScript string,
migration *models.Migration,
) error {
- sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript))
- execOptions, err := sqlParser.Parse()
+ err := applyMigration(mr.db, upgradeScript, *migration)
if err != nil {
- return fmt.Errorf("unable to parse sql file for upgrade script: %w", err)
+ return err
}
- if execOptions.UseTransaction {
- tx, err := mr.db.Begin()
- if err != nil {
- return fmt.Errorf(
- "unable to start transaction during upgrade script execution: %w",
- err,
- )
- }
- defer tx.Rollback()
-
- _, err = tx.Exec(upgradeScript)
- if err != nil {
- return fmt.Errorf("unable to execute upgrade script: %w", err)
- }
+ migration.Applied = true
+ return nil
+}
- _, err = tx.Exec(
- `INSERT INTO bolt_migrations(version) VALUES ($1);`,
- migration.Version,
+// ApplyWithTx applies a migration like Apply. However, it
+// wraps the operation is a database transaction.
+func (mr migrationDBRepo) ApplyWithTx(
+ upgradeScript string,
+ migration *models.Migration,
+) error {
+ tx, err := mr.db.Begin()
+ if err != nil {
+ return fmt.Errorf(
+ "unable to start transaction during upgrade script execution: %w",
+ err,
)
- if err != nil {
- return fmt.Errorf(
- "unable to insert applied migration to bolt_migrations table: %w",
- err,
- )
- }
- err = tx.Commit()
- if err != nil {
- return fmt.Errorf(
- "unable to commit transaction during upgrade script execution: %w",
- err,
- )
- }
- } else {
- _, err = mr.db.Exec(upgradeScript)
- if err != nil {
- return fmt.Errorf("unable to execute upgrade script: %w", err)
- }
+ }
+ defer tx.Rollback()
- _, err = mr.db.Exec(
- `INSERT INTO bolt_migrations(version) VALUES ($1);`,
- migration.Version,
+ err = applyMigration(tx, upgradeScript, *migration)
+ if err != nil {
+ return err
+ }
+
+ err = tx.Commit()
+ if err != nil {
+ return fmt.Errorf(
+ "unable to commit transaction during upgrade script execution: %w",
+ err,
)
- if err != nil {
- return fmt.Errorf(
- "unable to insert applied migration to bolt_migrations table: %w",
- err,
- )
- }
}
migration.Applied = true
return nil
}
+func applyMigration(
+ executor SQLExecutor,
+ upgradeScript string,
+ migration models.Migration,
+) error {
+ _, err := executor.Exec(upgradeScript)
+ if err != nil {
+ return fmt.Errorf("unable to execute upgrade script: %w", err)
+ }
+
+ _, err = executor.Exec(
+ `INSERT INTO bolt_migrations(version) VALUES ($1);`,
+ migration.Version,
+ )
+ if err != nil {
+ return fmt.Errorf(
+ "unable to insert applied migration: %w",
+ err,
+ )
+ }
+
+ return nil
+}
+
// Revert reverts a migration by executing the corresponding downgrade script
// and deleting the migration version from the migrations table. When successfully
// reverted, the `migration` model's `Applied` field will be set to false.
-func (mr *MigrationDBRepo) Revert(
+func (mr migrationDBRepo) Revert(
downgradeScript string,
migration *models.Migration,
) error {
- sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript))
- execOptions, err := sqlParser.Parse()
+ err := revertMigration(mr.db, downgradeScript, *migration)
if err != nil {
- return fmt.Errorf("unable to parse sql file for downgrade script: %w", err)
+ return err
}
- if execOptions.UseTransaction {
- tx, err := mr.db.Begin()
- if err != nil {
- return fmt.Errorf(
- "unable to start transaction during downgrade script execution: %w",
- err,
- )
- }
- defer tx.Rollback()
-
- _, err = tx.Exec(downgradeScript)
- if err != nil {
- return fmt.Errorf("unable to execute downgrade script: %w", err)
- }
+ migration.Applied = false
+ return nil
+}
- _, err = tx.Exec(
- `DELETE FROM bolt_migrations WHERE version = $1;`,
- migration.Version,
+// RevertWithTx reverts a migration like Revert. However, it
+// wraps the operation is a database transaction.
+func (mr migrationDBRepo) RevertWithTx(
+ downgradeScript string,
+ migration *models.Migration,
+) error {
+ tx, err := mr.db.Begin()
+ if err != nil {
+ return fmt.Errorf(
+ "unable to start transaction during downgrade script execution: %w",
+ err,
)
- if err != nil {
- return fmt.Errorf(
- "unable to remove reverted migration from bolt_migrations table: %w",
- err,
- )
- }
+ }
+ defer tx.Rollback()
- err = tx.Commit()
- if err != nil {
- return fmt.Errorf(
- "unable to commit transaction during downgrade script execution: %w",
- err,
- )
- }
- } else {
- _, err = mr.db.Exec(downgradeScript)
- if err != nil {
- return fmt.Errorf("unable to execute downgrade script: %w", err)
- }
+ err = revertMigration(tx, downgradeScript, *migration)
+ if err != nil {
+ return err
+ }
- _, err = mr.db.Exec(
- `DELETE FROM bolt_migrations WHERE version = $1;`,
- migration.Version,
+ err = tx.Commit()
+ if err != nil {
+ return fmt.Errorf(
+ "unable to commit transaction during downgrade script execution: %w",
+ err,
)
- if err != nil {
- return fmt.Errorf(
- "unable to remove reverted migration from bolt_migrations table: %w",
- err,
- )
- }
}
migration.Applied = false
return nil
}
+
+func revertMigration(
+ executor SQLExecutor,
+ downgradeScript string,
+ migration models.Migration,
+) error {
+ _, err := executor.Exec(downgradeScript)
+ if err != nil {
+ return fmt.Errorf("unable to execute downgrade script: %w", err)
+ }
+
+ _, err = executor.Exec(
+ `DELETE FROM bolt_migrations WHERE version = $1;`,
+ migration.Version,
+ )
+ if err != nil {
+ return fmt.Errorf(
+ "unable to remove reverted migration from bolt_migrations table: %w",
+ err,
+ )
+ }
+
+ return nil
+}
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index 4e41b36..9f4f9e8 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -197,7 +197,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) {
migration := models.NewTimestampMigration(time.Now(), "test")
// CREATE INDEX CONCURRENTLY cannot run inside a transaction.
err = repo.Apply(
- `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`,
+ `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`,
migration,
)
assert.Nil(t, err)
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index c541006..9d21c59 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -23,13 +23,23 @@ func (e *ErrIsNotDir) Error() string {
)
}
-type MigrationFsRepo struct {
+type MigrationFsRepo interface {
+ Create(migration *models.Migration) error
+ Exists(version string) (bool, error)
+ Get(version string) (*models.Migration, error)
+ List() (map[string]*models.Migration, error)
+ Latest() (*models.Migration, error)
+ ReadUpgradeScript(migration *models.Migration) (string, error)
+ ReadDowngradeScript(migration *models.Migration) (string, error)
+}
+
+type migrationFsRepo struct {
migrationsDirPath string
}
func NewMigrationFsRepo(
migrationsConfig *configloader.MigrationsConfig,
-) (*MigrationFsRepo, error) {
+) (MigrationFsRepo, error) {
fileInfo, err := os.Stat(migrationsConfig.DirectoryPath)
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755)
@@ -42,10 +52,10 @@ func NewMigrationFsRepo(
return nil, &ErrIsNotDir{path: migrationsConfig.DirectoryPath}
}
- return &MigrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil
+ return &migrationFsRepo{migrationsDirPath: migrationsConfig.DirectoryPath}, nil
}
-func (mr *MigrationFsRepo) Create(migration *models.Migration) error {
+func (mr migrationFsRepo) Create(migration *models.Migration) error {
newMigrationDir := filepath.Join(mr.migrationsDirPath, mr.migrationDirname(migration))
err := os.Mkdir(newMigrationDir, 0755)
@@ -66,7 +76,7 @@ func (mr *MigrationFsRepo) Create(migration *models.Migration) error {
return nil
}
-func (mr *MigrationFsRepo) Exists(version string) (bool, error) {
+func (mr migrationFsRepo) Exists(version string) (bool, error) {
migrations, err := mr.List()
if err != nil {
return false, err
@@ -76,7 +86,7 @@ func (mr *MigrationFsRepo) Exists(version string) (bool, error) {
return ok, nil
}
-func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) {
+func (mr migrationFsRepo) Get(version string) (*models.Migration, error) {
migrations, err := mr.List()
if err != nil {
return nil, err
@@ -90,7 +100,7 @@ func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) {
return migration, nil
}
-func (mr *MigrationFsRepo) Latest() (*models.Migration, error) {
+func (mr migrationFsRepo) Latest() (*models.Migration, error) {
entries, err := os.ReadDir(mr.migrationsDirPath)
if err != nil {
return nil, err
@@ -103,13 +113,13 @@ func (mr *MigrationFsRepo) Latest() (*models.Migration, error) {
return nil, nil
}
-func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) {
+func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
entries, err := os.ReadDir(mr.migrationsDirPath)
if err != nil {
return nil, err
}
- var migrations = make(map[string]*models.Migration)
+ var migrations = make(map[string]*models.Migration, 0)
for _, entry := range entries {
migration, err := dirEntryToMigration(entry)
if err != nil {
@@ -137,9 +147,7 @@ func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
}, nil
}
-func (mr *MigrationFsRepo) ReadUpgradeScript(
- migration *models.Migration,
-) (string, error) {
+func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) {
upgradeScriptPath := filepath.Join(
mr.migrationsDirPath,
mr.migrationDirname(migration),
@@ -148,7 +156,7 @@ func (mr *MigrationFsRepo) ReadUpgradeScript(
return mr.readScriptContents(upgradeScriptPath)
}
-func (mr *MigrationFsRepo) ReadDowngradeScript(
+func (mr migrationFsRepo) ReadDowngradeScript(
migration *models.Migration,
) (string, error) {
downgradeScriptPath := filepath.Join(
@@ -159,7 +167,7 @@ func (mr *MigrationFsRepo) ReadDowngradeScript(
return mr.readScriptContents(downgradeScriptPath)
}
-func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error) {
+func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) {
contents, err := os.ReadFile(scriptPath)
if err != nil {
return "", err
@@ -170,13 +178,13 @@ func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error)
// migrationDirname creates the directory name that
// should be used for this migration.
-func (mr *MigrationFsRepo) migrationDirname(migration *models.Migration) string {
+func (mr migrationFsRepo) migrationDirname(migration *models.Migration) string {
return fmt.Sprintf("%s_%s", migration.Version, mr.normalizeMessage(migration))
}
// normalizeMessage normalizes the Message of a migration
// to be filesystem friendly.
-func (mr *MigrationFsRepo) normalizeMessage(migration *models.Migration) string {
+func (mr migrationFsRepo) normalizeMessage(migration *models.Migration) string {
message := strings.ToLower(migration.Message)
message = strings.TrimSpace(message)
message = strings.ReplaceAll(message, " ", "_")
diff --git a/internal/services/migration.go b/internal/services/migration.go
index b6b00bb..48859d6 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -1,29 +1,62 @@
package services
import (
+ "database/sql"
"fmt"
"sort"
"strconv"
+ "strings"
"time"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
+ "github.com/eugenetriguba/bolt/internal/sqlparse"
)
type MigrationService struct {
- dbRepo *repositories.MigrationDBRepo
- fsRepo *repositories.MigrationFsRepo
+ dbRepo repositories.MigrationDBRepo
+ fsRepo repositories.MigrationFsRepo
+ cfg configloader.Config
outputter output.Outputter
}
func NewMigrationService(
- dbRepo *repositories.MigrationDBRepo,
- fsRepo *repositories.MigrationFsRepo,
+ dbRepo repositories.MigrationDBRepo,
+ fsRepo repositories.MigrationFsRepo,
+ cfg configloader.Config,
outputter output.Outputter,
-) *MigrationService {
- return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter}
+) MigrationService {
+ return MigrationService{
+ dbRepo: dbRepo,
+ fsRepo: fsRepo,
+ cfg: cfg,
+ outputter: outputter,
+ }
+}
+
+func NewMigrationServiceFromConfig(
+ db *sql.DB,
+ outputter output.Outputter,
+ cfg configloader.Config,
+) (MigrationService, error) {
+ migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
+ if err != nil {
+ return MigrationService{}, err
+ }
+
+ migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
+ if err != nil {
+ return MigrationService{}, err
+ }
+
+ return NewMigrationService(
+ migrationDBRepo,
+ migrationFsRepo,
+ cfg,
+ outputter,
+ ), nil
}
type sortOrder int
@@ -33,7 +66,7 @@ const (
SortOrderAsc
)
-func (ms *MigrationService) ApplyAllMigrations() error {
+func (ms MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
@@ -55,7 +88,7 @@ func (ms *MigrationService) ApplyAllMigrations() error {
return nil
}
-func (ms *MigrationService) ApplyUpToVersion(version string) error {
+func (ms MigrationService) ApplyUpToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
@@ -98,7 +131,7 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error {
return nil
}
-func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
+func (ms MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Applying migration %s_%s..",
@@ -112,10 +145,22 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
return err
}
- err = ms.dbRepo.Apply(scriptContents, migration)
+ sqlParser := sqlparse.NewSqlParser()
+ execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents))
if err != nil {
- return err
+ return fmt.Errorf("unable to parse sql file for upgrade script: %w", err)
}
+
+ if execOptions.UseTransaction {
+ err = ms.dbRepo.ApplyWithTx(scriptContents, migration)
+ } else {
+ err = ms.dbRepo.Apply(scriptContents, migration)
+ }
+
+ if err != nil {
+ return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err)
+ }
+
ms.outputter.Output(
fmt.Sprintf(
"Successfully applied migration %s_%s!",
@@ -127,7 +172,7 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
return nil
}
-func (ms *MigrationService) RevertAllMigrations() error {
+func (ms MigrationService) RevertAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
@@ -149,7 +194,7 @@ func (ms *MigrationService) RevertAllMigrations() error {
return nil
}
-func (ms *MigrationService) RevertDownToVersion(version string) error {
+func (ms MigrationService) RevertDownToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
@@ -192,7 +237,7 @@ func (ms *MigrationService) RevertDownToVersion(version string) error {
return nil
}
-func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
+func (ms MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Reverting migration %s_%s..",
@@ -205,10 +250,22 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
return err
}
- err = ms.dbRepo.Revert(scriptContents, migration)
+ sqlParser := sqlparse.NewSqlParser()
+ execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents))
+ if err != nil {
+ return fmt.Errorf("unable to parse sql file for downgrade script: %w", err)
+ }
+
+ if execOptions.UseTransaction {
+ err = ms.dbRepo.ApplyWithTx(scriptContents, migration)
+ } else {
+ err = ms.dbRepo.Revert(scriptContents, migration)
+ }
+
if err != nil {
return err
}
+
ms.outputter.Output(
fmt.Sprintf(
"Successfully reverted migration %s_%s!",
@@ -220,70 +277,152 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
return nil
}
-func (ms *MigrationService) CreateMigration(
- versionStyle configloader.VersionStyle,
- message string,
-) error {
+func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) {
var migration *models.Migration
- if versionStyle == configloader.VersionStyleTimestamp {
+ if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp {
migration = models.NewTimestampMigration(time.Now(), message)
} else {
- var currentVerison uint64 = 0
- latestMigration, err := ms.fsRepo.Latest()
+ currentVersion, err := ms.getCurrentSequentialMigrationVersion()
if err != nil {
- return err
+ return nil, err
}
- if latestMigration != nil {
- currentVerison, err = strconv.ParseUint(latestMigration.Version, 10, 64)
- if err != nil {
- return err
- }
- }
- migration = models.NewSequentialMigration(currentVerison+1, message)
+ migration = models.NewSequentialMigration(currentVersion+1, message)
}
err := ms.fsRepo.Create(migration)
if err != nil {
- return err
+ return nil, err
}
ms.outputter.Output(
fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
- return nil
+ return migration, nil
+}
+
+func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
+ var currentVersion uint64 = 0
+
+ latestMigration, err := ms.fsRepo.Latest()
+ if err != nil {
+ return 0, err
+ }
+
+ if latestMigration != nil {
+ currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ }
+
+ return currentVersion, nil
}
-func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
+func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
// Assumption: All migration versions that have been applied
// to the database exist locally in the list of filesystem migrations.
// This would hold true unless someone decided to apply a migration and,
- // for whatever reason, delete it later from their filesystem.
- migrations, err := ms.fsRepo.List()
+ // for whatever reason, delete it later from their filesystem. If there is
+ // an applied migration that doesn't exist locally, it would not be shown
+ // here.
+ localMigrations, err := ms.fsRepo.List()
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
appliedMigrations, err := ms.dbRepo.List()
if err != nil {
- return nil, err
+ return nil, fmt.Errorf(
+ "unable to list out applied migrations from remote db: %w",
+ err,
+ )
}
- var flatMigrations []*models.Migration
- for _, migration := range migrations {
- _, ok := appliedMigrations[migration.Version]
+ migrations := make([]*models.Migration, 0)
+ for _, localMigration := range localMigrations {
+ _, ok := appliedMigrations[localMigration.Version]
if ok {
- migration.Applied = true
+ localMigration.Applied = true
}
- flatMigrations = append(flatMigrations, migration)
+ migrations = append(migrations, localMigration)
}
- sort.Slice(flatMigrations, func(i, j int) bool {
- if order == SortOrderAsc {
- return flatMigrations[i].Version < flatMigrations[j].Version
+ err = ms.sortMigrations(migrations, order)
+ if err != nil {
+ return nil, err
+ }
+
+ return migrations, err
+}
+
+func (ms MigrationService) sortMigrations(migrations []*models.Migration, order sortOrder) error {
+ sortErrs := make([]error, 0)
+ sort.Slice(migrations, func(i, j int) bool {
+ var comparison bool
+ var err error
+
+ if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential {
+ comparison, err = ms.compareSequentialMigrations(
+ migrations[i],
+ migrations[j],
+ order,
+ )
} else {
- return flatMigrations[i].Version > flatMigrations[j].Version
+ comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order)
}
+
+ if err != nil {
+ sortErrs = append(sortErrs, err)
+ }
+
+ return comparison
})
+ if len(sortErrs) != 0 {
+ return fmt.Errorf("unable to sort migrations: %v", sortErrs)
+ }
+ return nil
+}
+
+func (ms MigrationService) compareSequentialMigrations(
+ m1 *models.Migration,
+ m2 *models.Migration,
+ order sortOrder,
+) (bool, error) {
+ m1Version, err := strconv.ParseInt(m1.Version, 10, 64)
+ if err != nil {
+ return false, err
+ }
+
+ m2Version, err := strconv.ParseInt(m2.Version, 10, 64)
+ if err != nil {
+ return false, err
+ }
- return flatMigrations, nil
+ if order == SortOrderAsc {
+ return m1Version < m2Version, nil
+ } else {
+ return m1Version > m2Version, nil
+ }
+}
+
+func (ms MigrationService) compareTimestampMigrations(
+ m1 *models.Migration,
+ m2 *models.Migration,
+ order sortOrder,
+) (bool, error) {
+ m1Version, err := time.Parse("20060102150405", m1.Version)
+ if err != nil {
+ return false, err
+ }
+
+ m2Version, err := time.Parse("20060102150405", m2.Version)
+ if err != nil {
+ return false, err
+ }
+
+ if order == SortOrderAsc {
+ return m1Version.Before(m2Version), nil
+ } else {
+ return m1Version.After(m2Version), nil
+ }
}
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go
new file mode 100644
index 0000000..73045ef
--- /dev/null
+++ b/internal/services/migration_test.go
@@ -0,0 +1,595 @@
+package services
+
+import (
+ "errors"
+ "testing"
+ "time"
+
+ "github.com/eugenetriguba/bolt/internal/bolttest"
+ "github.com/eugenetriguba/bolt/internal/configloader"
+ "github.com/eugenetriguba/bolt/internal/models"
+ "github.com/eugenetriguba/checkmate/assert"
+ "github.com/eugenetriguba/checkmate/check"
+)
+
+func TestListMigrations(t *testing.T) {
+ type test struct {
+ localFilesystemMigrations []*models.Migration
+ remoteAppliedMigrations []*models.Migration
+ cfg configloader.Config
+ sortOrder sortOrder
+ expectedMigrations []*models.Migration
+ }
+
+ tests := []test{
+ // Ensure no local or remote applied migrations means none are returned.
+ {
+ localFilesystemMigrations: []*models.Migration{},
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{},
+ },
+ // Ensure one local fs migration shows up as expected.
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "001", Applied: false},
+ },
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{
+ {Version: "001", Applied: false},
+ },
+ },
+ // Ensure that one local fs migration and one remote applied migration
+ // for the same version returns one applied migration.
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "001", Applied: false},
+ },
+ remoteAppliedMigrations: []*models.Migration{
+ {Version: "001", Applied: true},
+ },
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{
+ {Version: "001", Applied: true},
+ },
+ },
+ // Ensure that any applied migrations that don't exist locally
+ // do not show up in the response.
+ {
+ localFilesystemMigrations: []*models.Migration{},
+ remoteAppliedMigrations: []*models.Migration{
+ {Version: "001", Applied: true},
+ },
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{},
+ },
+ // Ensure that migrations are sorted in asc order for sequential migrations
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "20000"},
+ {Version: "10000"},
+ {Version: "1010"},
+ {Version: "1009"},
+ {Version: "190"},
+ {Version: "110"},
+ {Version: "001"},
+ },
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{
+ {Version: "001"},
+ {Version: "110"},
+ {Version: "190"},
+ {Version: "1009"},
+ {Version: "1010"},
+ {Version: "10000"},
+ {Version: "20000"},
+ },
+ },
+ // Ensure that migrations are sorted in desc order for sequential migrations
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "001"},
+ {Version: "110"},
+ {Version: "190"},
+ {Version: "1009"},
+ {Version: "1010"},
+ {Version: "10000"},
+ {Version: "20000"},
+ },
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ sortOrder: SortOrderDesc,
+ expectedMigrations: []*models.Migration{
+ {Version: "20000"},
+ {Version: "10000"},
+ {Version: "1010"},
+ {Version: "1009"},
+ {Version: "190"},
+ {Version: "110"},
+ {Version: "001"},
+ },
+ },
+ // Ensure that migrations are sorted in asc order for timestamp migrations
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "20080509220905"},
+ {Version: "20080509220405"},
+ {Version: "20080509150405"},
+ {Version: "20080502150405"},
+ {Version: "20080102150405"},
+ {Version: "20070102150405"},
+ {Version: "20060102150405"},
+ },
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ },
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{
+ {Version: "20060102150405"},
+ {Version: "20070102150405"},
+ {Version: "20080102150405"},
+ {Version: "20080502150405"},
+ {Version: "20080509150405"},
+ {Version: "20080509220405"},
+ {Version: "20080509220905"},
+ },
+ },
+ // Ensure that migrations are sorted in desc order for timestamp migrations
+ {
+ localFilesystemMigrations: []*models.Migration{
+ {Version: "20060102150405"},
+ {Version: "20070102150405"},
+ {Version: "20080102150405"},
+ {Version: "20080502150405"},
+ {Version: "20080509150405"},
+ {Version: "20080509220405"},
+ {Version: "20080509220905"},
+ },
+ remoteAppliedMigrations: []*models.Migration{},
+ cfg: configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ },
+ sortOrder: SortOrderDesc,
+ expectedMigrations: []*models.Migration{
+ {Version: "20080509220905"},
+ {Version: "20080509220405"},
+ {Version: "20080509150405"},
+ {Version: "20080502150405"},
+ {Version: "20080102150405"},
+ {Version: "20070102150405"},
+ {Version: "20060102150405"},
+ },
+ },
+ }
+
+ for _, tc := range tests {
+ localFsMigrationMap := make(map[string]*models.Migration, 0)
+ for _, localFsMigration := range tc.localFilesystemMigrations {
+ localFsMigrationMap[localFsMigration.Version] = localFsMigration
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: localFsMigrationMap,
+ Err: nil,
+ },
+ }
+
+ remoteAppliedMigrationMap := make(map[string]*models.Migration, 0)
+ for _, remoteAppliedMigration := range tc.remoteAppliedMigrations {
+ remoteAppliedMigrationMap[remoteAppliedMigration.Version] = remoteAppliedMigration
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: remoteAppliedMigrationMap,
+ Err: nil,
+ },
+ }
+
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ tc.cfg,
+ bolttest.NullOutputter{},
+ )
+ migrations, err := svc.ListMigrations(tc.sortOrder)
+
+ assert.Nil(t, err)
+ assert.DeepEqual(t, migrations, tc.expectedMigrations)
+ }
+}
+
+func TestListMigrations_FsRepoListError(t *testing.T) {
+ expectedErr := errors.New("fs repo error")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+ migrations, err := svc.ListMigrations(SortOrderAsc)
+
+ assert.ErrorIs(t, err, expectedErr)
+ var expectedMigrations []*models.Migration
+ assert.DeepEqual(t, migrations, expectedMigrations)
+}
+
+func TestListMigrations_DbRepoListError(t *testing.T) {
+ expectedErr := errors.New("db repo error")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+ migrations, err := svc.ListMigrations(SortOrderAsc)
+
+ assert.ErrorIs(t, err, expectedErr)
+ var expectedMigrations []*models.Migration
+ assert.DeepEqual(t, migrations, expectedMigrations)
+}
+
+func TestListMigrations_ParseErrDuringSort(t *testing.T) {
+ type test struct {
+ m1 models.Migration
+ m2 models.Migration
+ versionStyle configloader.VersionStyle
+ }
+
+ tests := []test{
+ {
+ m1: models.Migration{Version: "abc"},
+ m2: models.Migration{Version: "123"},
+ versionStyle: configloader.VersionStyleSequential,
+ },
+ {
+ m1: models.Migration{Version: "123"},
+ m2: models.Migration{Version: "abc"},
+ versionStyle: configloader.VersionStyleSequential,
+ },
+ {
+ m1: models.Migration{Version: "20060102150405"},
+ m2: models.Migration{Version: "abc"},
+ versionStyle: configloader.VersionStyleTimestamp,
+ },
+ {
+ m1: models.Migration{Version: "abc"},
+ m2: models.Migration{Version: "20060102150405"},
+ versionStyle: configloader.VersionStyleTimestamp,
+ },
+ }
+
+ for _, tc := range tests {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ tc.m1.Version: {Version: tc.m1.Version},
+ tc.m2.Version: {Version: tc.m2.Version},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: tc.versionStyle,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+ migrations, err := svc.ListMigrations(SortOrderAsc)
+
+ assert.ErrorContains(t, err, "unable to sort migrations")
+ var expectedMigrations []*models.Migration
+ assert.DeepEqual(t, migrations, expectedMigrations)
+ }
+}
+
+func TestCreateMigration_VersionStyleTimestamp(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ beforeCreateTs := time.Now().Add(-time.Second)
+ migration, err := svc.CreateMigration("new migration")
+ afterCreateTs := time.Now().Add(time.Second)
+
+ assert.Nil(t, err)
+ version, err := time.Parse("20060102150405", migration.Version)
+ assert.Nil(t, err)
+ check.True(t, beforeCreateTs.Before(version))
+ check.True(t, afterCreateTs.After(version))
+ check.Equal(t, migration.Message, "new migration")
+ check.Equal(t, migration.Applied, false)
+}
+
+func TestCreateMigration_VersionStyleSequential(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ migration, err := svc.CreateMigration("new migration")
+
+ assert.Nil(t, err)
+ check.Equal(t, migration.Version, "001")
+ check.Equal(t, migration.Message, "new migration")
+ check.Equal(t, migration.Applied, false)
+}
+
+func TestCreateMigration_CreateErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ migration, err := svc.CreateMigration("new migration")
+
+ assert.ErrorIs(t, err, expectedErr)
+ assert.Nil(t, migration)
+}
+
+func TestCreateMigration_LatestErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ LatestReturnValue: bolttest.LatestReturnValue{
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ migration, err := svc.CreateMigration("new migration")
+
+ assert.ErrorIs(t, err, expectedErr)
+ assert.Nil(t, migration)
+}
+
+func TestCreateMigration_SequentialVersionIncrements(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ LatestReturnValue: bolttest.LatestReturnValue{
+ Migration: &models.Migration{Version: "001"},
+ Err: nil,
+ },
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ migration, err := svc.CreateMigration("new migration")
+
+ assert.Nil(t, err)
+ assert.Equal(t, migration.Version, "002")
+}
+
+func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ LatestReturnValue: bolttest.LatestReturnValue{
+ Migration: &models.Migration{Version: "abc"},
+ Err: nil,
+ },
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ migration, err := svc.CreateMigration("new migration")
+
+ assert.ErrorContains(t, err, `parsing "abc": invalid syntax`)
+ assert.Nil(t, migration)
+}
+
+func TestApplyMigration_WithTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ ScriptContents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);",
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ Err: nil,
+ },
+ }
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyMigration(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+}
+
+func TestApplyMigration_WithoutTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ ScriptContents: "-- bolt: no-transaction\nCREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);",
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyReturnValue: bolttest.ApplyReturnValue{
+ Err: nil,
+ },
+ }
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyMigration(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 1)
+}
+
+func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ ScriptContents: "",
+ Err: expectedErr,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.ApplyMigration(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go
index 25960c5..e986068 100644
--- a/internal/sqlparse/sqlparse.go
+++ b/internal/sqlparse/sqlparse.go
@@ -7,22 +7,24 @@ import (
"strings"
)
-type SqlParser struct {
- reader io.Reader
+type SqlParser interface {
+ Parse(reader io.Reader) (ExecutionOptions, error)
}
type ExecutionOptions struct {
UseTransaction bool
}
-func NewSqlParser(reader io.Reader) *SqlParser {
- return &SqlParser{reader: reader}
+type sqlParser struct{}
+
+func NewSqlParser() SqlParser {
+ return sqlParser{}
}
-func (sp *SqlParser) Parse() (*ExecutionOptions, error) {
- options := &ExecutionOptions{UseTransaction: true}
+func (sp sqlParser) Parse(reader io.Reader) (ExecutionOptions, error) {
+ options := ExecutionOptions{UseTransaction: true}
- scanner := bufio.NewScanner(sp.reader)
+ scanner := bufio.NewScanner(reader)
if scanner.Scan() {
firstLine := strings.ToLower(strings.TrimSpace(scanner.Text()))
if strings.HasPrefix(firstLine, "-- bolt:") {
@@ -38,7 +40,7 @@ func (sp *SqlParser) Parse() (*ExecutionOptions, error) {
}
if err := scanner.Err(); err != nil {
- return nil, fmt.Errorf("parsing sql file encountered an error: %w", err)
+ return options, fmt.Errorf("parsing sql file encountered an error: %w", err)
}
return options, nil
diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go
index 5fe58fa..12a435d 100644
--- a/internal/sqlparse/sqlparse_test.go
+++ b/internal/sqlparse/sqlparse_test.go
@@ -14,42 +14,42 @@ import (
func TestSqlParser_Parse(t *testing.T) {
testCases := []struct {
buffer string
- expectedExecutionOptions *sqlparse.ExecutionOptions
+ expectedExecutionOptions sqlparse.ExecutionOptions
}{
{
buffer: "-- bolt: no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
},
{
buffer: "-- BOLT: no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
},
{
buffer: "\n-- bolt: no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
},
{
buffer: "",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
},
{
buffer: "--bolt: no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
},
{
buffer: "-- bolt : no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: true},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
},
{
buffer: "-- bolt: no-transaction no-transaction",
- expectedExecutionOptions: &sqlparse.ExecutionOptions{UseTransaction: false},
+ expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
},
}
for _, tc := range testCases {
- sqlParser := sqlparse.NewSqlParser(strings.NewReader(tc.buffer))
+ sqlParser := sqlparse.NewSqlParser()
- execOptions, err := sqlParser.Parse()
+ execOptions, err := sqlParser.Parse(strings.NewReader(tc.buffer))
assert.Nil(t, err)
assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions)
@@ -79,8 +79,8 @@ func TestSqlParserParseError(t *testing.T) {
Reader: strings.NewReader(" "),
ErrCond: " ",
}
- sqlParser := sqlparse.NewSqlParser(reader)
- _, err := sqlParser.Parse()
+ sqlParser := sqlparse.NewSqlParser()
+ _, err := sqlParser.Parse(reader)
assert.NotNil(t, err)
assert.ErrorContains(t, err, "unwanted input encountered")
}
diff --git a/internal/storage/db.go b/internal/storage/db.go
index 02683b4..a9e35af 100644
--- a/internal/storage/db.go
+++ b/internal/storage/db.go
@@ -10,8 +10,10 @@ import (
)
var (
- ErrMalformedConnectionString = errors.New("malformed database connection parameters provided")
- ErrUnableToConnect = errors.New("unable to open connection to database")
+ ErrMalformedConnectionString = errors.New(
+ "malformed database connection parameters provided",
+ )
+ ErrUnableToConnect = errors.New("unable to open connection to database")
)
// DBConnect establishes a connection to the database using the driver