aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-13 18:23:48 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-13 18:23:48 -0500
commit00aa327eb204d504f1e03dbe5a7cdec4af4ec913 (patch)
tree571a5bc5ce52a11e0039f9763c89b9316e7bf9d4 /internal
parentTweak readme (diff)
downloadcli-00aa327eb204d504f1e03dbe5a7cdec4af4ec913.zip
Rework docs, add more output, and tweak tests
Diffstat (limited to 'internal')
-rw-r--r--internal/commands/down.go (renamed from internal/commands/downgrade.go)42
-rw-r--r--internal/commands/list.go74
-rw-r--r--internal/commands/new.go (renamed from internal/commands/create.go)42
-rw-r--r--internal/commands/status.go100
-rw-r--r--internal/commands/up.go (renamed from internal/commands/migrate.go)41
-rw-r--r--internal/output/console.go60
-rw-r--r--internal/output/console_test.go76
-rw-r--r--internal/output/output.go7
-rw-r--r--internal/repositories/migration_db_repo.go10
-rw-r--r--internal/repositories/migration_db_repo_test.go5
-rw-r--r--internal/repositories/migration_fs_repo.go8
-rw-r--r--internal/repositories/migration_fs_repo_test.go10
-rw-r--r--internal/services/migration.go48
13 files changed, 390 insertions, 133 deletions
diff --git a/internal/commands/downgrade.go b/internal/commands/down.go
index f953286..0a9ca99 100644
--- a/internal/commands/downgrade.go
+++ b/internal/commands/down.go
@@ -3,37 +3,43 @@ package commands
import (
"context"
"flag"
- "fmt"
- "os"
"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"
)
-type DowngradeCmd struct{}
+type DownCmd struct{}
-func (*DowngradeCmd) Name() string { return "downgrade" }
+func (*DownCmd) Name() string {
+ return "down"
+}
+
+func (*DownCmd) Synopsis() string {
+ return "downgrade migrations against the database"
+}
-func (*DowngradeCmd) Synopsis() string { return "Downgrade all migrations against the database." }
-func (*DowngradeCmd) Usage() string {
- return `downgrade:
- Downgrade all migrations against the database.
+func (*DownCmd) Usage() string {
+ return `down:
+ Downgrade migrations against the database.
`
}
-func (m *DowngradeCmd) SetFlags(f *flag.FlagSet) {}
+func (m *DownCmd) SetFlags(f *flag.FlagSet) {}
-func (m *DowngradeCmd) Execute(
+func (m *DownCmd) Execute(
_ context.Context,
f *flag.FlagSet,
_ ...interface{},
) subcommands.ExitStatus {
+ consoleOutputter := output.ConsoleOutputter{}
+
cfg, err := configloader.NewConfig()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
@@ -42,27 +48,31 @@ func (m *DowngradeCmd) Execute(
storage.DBConnectionString(&cfg.Connection),
)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
defer db.Close()
migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(migrationDBRepo, migrationFsRepo)
+ migrationService := services.NewMigrationService(
+ migrationDBRepo,
+ migrationFsRepo,
+ consoleOutputter,
+ )
err = migrationService.RevertAllMigrations()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
diff --git a/internal/commands/list.go b/internal/commands/list.go
deleted file mode 100644
index 8224820..0000000
--- a/internal/commands/list.go
+++ /dev/null
@@ -1,74 +0,0 @@
-package commands
-
-import (
- "context"
- "flag"
- "fmt"
- "os"
-
- "github.com/eugenetriguba/bolt/internal/configloader"
- "github.com/eugenetriguba/bolt/internal/repositories"
- "github.com/eugenetriguba/bolt/internal/services"
- "github.com/eugenetriguba/bolt/internal/storage"
- "github.com/google/subcommands"
-)
-
-type ListCmd struct{}
-
-func (*ListCmd) Name() string { return "list" }
-
-func (*ListCmd) Synopsis() string { return "List the database migrations and their statuses." }
-func (*ListCmd) Usage() string {
- return `list:
- List the database migrations and their statuses.
- `
-}
-
-func (m *ListCmd) SetFlags(f *flag.FlagSet) {}
-
-func (m *ListCmd) Execute(
- _ context.Context,
- f *flag.FlagSet,
- _ ...interface{},
-) subcommands.ExitStatus {
- cfg, err := configloader.NewConfig()
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- return subcommands.ExitFailure
- }
-
- db, err := storage.DBConnect(
- cfg.Connection.Driver,
- storage.DBConnectionString(&cfg.Connection),
- )
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- return subcommands.ExitFailure
- }
- defer db.Close()
-
- migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- return subcommands.ExitFailure
- }
-
- migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- return subcommands.ExitFailure
- }
-
- migrationService := services.NewMigrationService(migrationDBRepo, migrationFsRepo)
- migrations, err := migrationService.ListMigrations(services.SortOrderAsc)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- return subcommands.ExitFailure
- }
-
- for _, migration := range migrations {
- fmt.Println(migration)
- }
-
- return subcommands.ExitSuccess
-}
diff --git a/internal/commands/create.go b/internal/commands/new.go
index 57f4c69..be1f49c 100644
--- a/internal/commands/create.go
+++ b/internal/commands/new.go
@@ -3,67 +3,77 @@ package commands
import (
"context"
"flag"
- "fmt"
- "os"
"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/services"
"github.com/google/subcommands"
)
-type CreateCmd struct {
+type NewCmd struct {
message string
}
-func (*CreateCmd) Name() string { return "create" }
-func (*CreateCmd) Synopsis() string {
- return "Create a new database migration."
+func (*NewCmd) Name() string {
+ return "new"
}
-func (*CreateCmd) Usage() string {
- return `create [-message]:
- Create a new database migration.
+
+func (*NewCmd) Synopsis() string {
+ return "create a new database migration"
+}
+
+func (*NewCmd) Usage() string {
+ return `create [-message|-m]:
+ Create a new database migration
`
}
-func (cmd *CreateCmd) SetFlags(f *flag.FlagSet) {
+func (cmd *NewCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(
&cmd.message,
"message",
"autogenerated",
"Message to use for the migration",
)
+ f.StringVar(&cmd.message, "m", cmd.message, "alias for -message")
}
-func (cmd *CreateCmd) Execute(
+func (cmd *NewCmd) Execute(
_ context.Context,
f *flag.FlagSet,
_ ...interface{},
) subcommands.ExitStatus {
+ consoleOutputter := output.ConsoleOutputter{}
+
cfg, err := configloader.NewConfig()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(nil, migrationFsRepo)
+ migrationService := services.NewMigrationService(
+ nil,
+ migrationFsRepo,
+ consoleOutputter,
+ )
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
migration := models.NewMigration(time.Now(), cmd.message)
err = migrationService.CreateMigration(migration)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
diff --git a/internal/commands/status.go b/internal/commands/status.go
new file mode 100644
index 0000000..c20abd3
--- /dev/null
+++ b/internal/commands/status.go
@@ -0,0 +1,100 @@
+package commands
+
+import (
+ "context"
+ "flag"
+
+ "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"
+)
+
+type StatusCmd struct{}
+
+func (*StatusCmd) Name() string {
+ return "status"
+}
+
+func (*StatusCmd) Synopsis() string {
+ return "list the database migrations and their statuses"
+}
+
+func (*StatusCmd) Usage() string {
+ return `status:
+ List the database migrations and their statuses
+ `
+}
+
+func (m *StatusCmd) SetFlags(f *flag.FlagSet) {}
+
+func (m *StatusCmd) Execute(
+ _ context.Context,
+ f *flag.FlagSet,
+ _ ...interface{},
+) subcommands.ExitStatus {
+ consoleOutputter := output.ConsoleOutputter{}
+
+ cfg, err := configloader.NewConfig()
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ db, err := storage.DBConnect(
+ cfg.Connection.Driver,
+ storage.DBConnectionString(&cfg.Connection),
+ )
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+ defer db.Close()
+
+ migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ migrationService := services.NewMigrationService(
+ migrationDBRepo,
+ migrationFsRepo,
+ consoleOutputter,
+ )
+ migrations, err := migrationService.ListMigrations(services.SortOrderAsc)
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ if len(migrations) == 0 {
+ consoleOutputter.Output(
+ "No migrations have been created.\n" +
+ "Run 'bolt create' to create your first migration.",
+ )
+ return subcommands.ExitSuccess
+ }
+
+ headers := []string{"Version", "Message", "Applied"}
+ rows := make([][]string, len(migrations))
+ for i, migration := range migrations {
+ applied := ""
+ if migration.Applied {
+ applied = "X"
+ }
+
+ rows[i] = []string{migration.Version, migration.Message, applied}
+ }
+
+ consoleOutputter.Table(headers, rows)
+ return subcommands.ExitSuccess
+}
diff --git a/internal/commands/migrate.go b/internal/commands/up.go
index 8c86291..c207a83 100644
--- a/internal/commands/migrate.go
+++ b/internal/commands/up.go
@@ -3,37 +3,42 @@ package commands
import (
"context"
"flag"
- "fmt"
- "os"
"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"
)
-type UpgradeCmd struct{}
+type UpCmd struct{}
-func (*UpgradeCmd) Name() string { return "upgrade" }
+func (*UpCmd) Name() string {
+ return "up"
+}
+
+func (*UpCmd) Synopsis() string {
+ return "apply migrations against the database"
+}
-func (*UpgradeCmd) Synopsis() string { return "Upgrade the database to the latest migration." }
-func (*UpgradeCmd) Usage() string {
- return `upgrade:
- Upgrade the database to the latest migration.
+func (*UpCmd) Usage() string {
+ return `up:
+ Apply migrations against the database
`
}
-func (m *UpgradeCmd) SetFlags(f *flag.FlagSet) {}
+func (m *UpCmd) SetFlags(f *flag.FlagSet) {}
-func (m *UpgradeCmd) Execute(
+func (m *UpCmd) Execute(
_ context.Context,
f *flag.FlagSet,
_ ...interface{},
) subcommands.ExitStatus {
+ consoleOutputter := output.ConsoleOutputter{}
cfg, err := configloader.NewConfig()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
@@ -42,27 +47,31 @@ func (m *UpgradeCmd) Execute(
storage.DBConnectionString(&cfg.Connection),
)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
defer db.Close()
migrationDBRepo, err := repositories.NewMigrationDBRepo(db)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
- migrationService := services.NewMigrationService(migrationDBRepo, migrationFsRepo)
+ migrationService := services.NewMigrationService(
+ migrationDBRepo,
+ migrationFsRepo,
+ consoleOutputter,
+ )
err = migrationService.ApplyAllMigrations()
if err != nil {
- fmt.Fprintln(os.Stderr, err)
+ consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
}
diff --git a/internal/output/console.go b/internal/output/console.go
new file mode 100644
index 0000000..d2946ed
--- /dev/null
+++ b/internal/output/console.go
@@ -0,0 +1,60 @@
+package output
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "text/tabwriter"
+)
+
+type ConsoleOutputter struct {
+ stdout io.Writer
+ stderr io.Writer
+}
+
+func NewConsoleOutputter() ConsoleOutputter {
+ return ConsoleOutputter{stdout: os.Stdout, stderr: os.Stderr}
+}
+
+func (c ConsoleOutputter) Output(message string) error {
+ _, err := fmt.Fprintln(c.stdout, message)
+ return err
+}
+
+func (c ConsoleOutputter) Error(message string) error {
+ _, err := fmt.Fprintln(c.stderr, message)
+ return err
+}
+
+func (c ConsoleOutputter) Table(headers []string, rows [][]string) error {
+ w := tabwriter.NewWriter(c.stdout, 4, 4, 4, ' ', 0)
+
+ for _, header := range headers {
+ _, err := fmt.Fprintf(w, "%s\t", header)
+ if err != nil {
+ return err
+ }
+ }
+ if len(headers) > 0 {
+ _, err := fmt.Fprint(w, "\n")
+ if err != nil {
+ return err
+ }
+ }
+
+ for _, row := range rows {
+ for _, cell := range row {
+ _, err := fmt.Fprintf(w, "%s\t", cell)
+ if err != nil {
+ return err
+ }
+ }
+ _, err := fmt.Fprint(w, "\n")
+ if err != nil {
+ return err
+ }
+ }
+
+ w.Flush()
+ return nil
+}
diff --git a/internal/output/console_test.go b/internal/output/console_test.go
new file mode 100644
index 0000000..be369ab
--- /dev/null
+++ b/internal/output/console_test.go
@@ -0,0 +1,76 @@
+package output
+
+import (
+ "bytes"
+ "io"
+ "testing"
+
+ "gotest.tools/v3/assert"
+)
+
+func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter {
+ return ConsoleOutputter{stdout: stdout, stderr: stderr}
+}
+
+func TestConsoleOutputter_Output(t *testing.T) {
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+ consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr)
+
+ consoleOutputter.Output("test string")
+
+ assert.Equal(t, stdout.String(), "test string\n")
+ assert.Equal(t, stderr.String(), "")
+}
+
+func TestConsoleOutputter_Error(t *testing.T) {
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+ consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr)
+
+ consoleOutputter.Error("test string")
+
+ assert.Equal(t, stdout.String(), "")
+ assert.Equal(t, stderr.String(), "test string\n")
+}
+
+func TestConsoleOutputter_Table(t *testing.T) {
+ type test struct {
+ headers []string
+ rows [][]string
+ expectedStdout string
+ }
+
+ tests := []test{
+ {
+ headers: []string{},
+ rows: make([][]string, 0),
+ expectedStdout: "",
+ },
+ {
+ headers: []string{"Version"},
+ rows: [][]string{{"test1"}, {"test2"}},
+ expectedStdout: "Version \n" +
+ "test1 \n" +
+ "test2 \n",
+ },
+ {
+ headers: []string{"Version", "Message"},
+ rows: [][]string{{"v1", "m1"}, {"v1", "m2"}},
+ expectedStdout: "Version Message \n" +
+ "v1 m1 \n" +
+ "v1 m2 \n",
+ },
+ }
+
+ for _, tc := range tests {
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+ consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr)
+
+ consoleOutputter.Table(tc.headers, tc.rows)
+
+ assert.Equal(t, stdout.String(), tc.expectedStdout)
+ assert.Equal(t, stderr.String(), "")
+ }
+}
diff --git a/internal/output/output.go b/internal/output/output.go
new file mode 100644
index 0000000..c51553b
--- /dev/null
+++ b/internal/output/output.go
@@ -0,0 +1,7 @@
+package output
+
+type Outputter interface {
+ Output(message string) error
+ Error(message string) error
+ Table(header []string, rows [][]string) error
+}
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go
index 7f8fc10..6a37e7b 100644
--- a/internal/repositories/migration_db_repo.go
+++ b/internal/repositories/migration_db_repo.go
@@ -83,7 +83,10 @@ 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(upgradeScript string, migration *models.Migration) error {
+func (mr *MigrationDBRepo) Apply(
+ upgradeScript string,
+ migration *models.Migration,
+) error {
tx, err := mr.db.Begin()
if err != nil {
return err
@@ -114,7 +117,10 @@ func (mr *MigrationDBRepo) Apply(upgradeScript string, migration *models.Migrati
// 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(downgradeScript string, migration *models.Migration) error {
+func (mr *MigrationDBRepo) Revert(
+ downgradeScript string,
+ migration *models.Migration,
+) error {
tx, err := mr.db.Begin()
if err != nil {
return err
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index 264b3be..a38875f 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -159,7 +159,10 @@ func TestMigrationDBRepo_Revert(t *testing.T) {
assert.NilError(t, err)
migration := models.NewMigration(time.Now(), "test")
- _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version)
+ _, err = db.Exec(
+ `INSERT INTO bolt_migrations(version) VALUES ($1);`,
+ migration.Version,
+ )
assert.NilError(t, err)
migration.Applied = true
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index d347176..8a7ec8a 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -88,7 +88,9 @@ func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) {
return migrations, 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),
@@ -97,7 +99,9 @@ func (mr *MigrationFsRepo) ReadUpgradeScript(migration *models.Migration) (strin
return mr.readScriptContents(upgradeScriptPath)
}
-func (mr *MigrationFsRepo) ReadDowngradeScript(migration *models.Migration) (string, error) {
+func (mr *MigrationFsRepo) ReadDowngradeScript(
+ migration *models.Migration,
+) (string, error) {
downgradeScriptPath := filepath.Join(
mr.migrationsDirPath,
mr.migrationDirname(migration),
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index a0448a5..bb12097 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -88,8 +88,14 @@ func TestMigrationFsRepo_List(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(tempDir)
assert.NilError(t, err)
- migration1 := models.NewMigration(time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC), "migration_1")
- migration2 := models.NewMigration(time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC), "migration_2")
+ migration1 := models.NewMigration(
+ time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
+ "migration_1",
+ )
+ migration2 := models.NewMigration(
+ time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
+ "migration_2",
+ )
err = repo.Create(migration1)
assert.NilError(t, err)
repo.Create(migration2)
diff --git a/internal/services/migration.go b/internal/services/migration.go
index de03d3c..ef6a509 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -1,22 +1,26 @@
package services
import (
+ "fmt"
"sort"
"github.com/eugenetriguba/bolt/internal/models"
+ "github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
)
type MigrationService struct {
- dbRepo *repositories.MigrationDBRepo
- fsRepo *repositories.MigrationFsRepo
+ dbRepo *repositories.MigrationDBRepo
+ fsRepo *repositories.MigrationFsRepo
+ outputter output.Outputter
}
func NewMigrationService(
dbRepo *repositories.MigrationDBRepo,
fsRepo *repositories.MigrationFsRepo,
+ outputter output.Outputter,
) *MigrationService {
- return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo}
+ return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter}
}
type sortOrder int
@@ -42,6 +46,14 @@ func (ms *MigrationService) ApplyAllMigrations() error {
}
func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
+ ms.outputter.Output(
+ fmt.Sprintf(
+ "Applying migration %s_%s..",
+ migration.Version,
+ migration.Message,
+ ),
+ )
+
scriptContents, err := ms.fsRepo.ReadUpgradeScript(migration)
if err != nil {
return err
@@ -51,6 +63,13 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
if err != nil {
return err
}
+ ms.outputter.Output(
+ fmt.Sprintf(
+ "Successfully applied migration %s_%s!",
+ migration.Version,
+ migration.Message,
+ ),
+ )
return nil
}
@@ -71,6 +90,13 @@ func (ms *MigrationService) RevertAllMigrations() error {
}
func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
+ ms.outputter.Output(
+ fmt.Sprintf(
+ "Reverting migration %s_%s..",
+ migration.Version,
+ migration.Message,
+ ),
+ )
scriptContents, err := ms.fsRepo.ReadDowngradeScript(migration)
if err != nil {
return err
@@ -80,12 +106,26 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
if err != nil {
return err
}
+ ms.outputter.Output(
+ fmt.Sprintf(
+ "Successfully reverted migration %s_%s!",
+ migration.Version,
+ migration.Message,
+ ),
+ )
return nil
}
func (ms *MigrationService) CreateMigration(migration *models.Migration) error {
- return ms.fsRepo.Create(migration)
+ err := ms.fsRepo.Create(migration)
+ if err != nil {
+ return err
+ }
+ ms.outputter.Output(
+ fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
+ )
+ return nil
}
func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {