aboutsummaryrefslogtreecommitdiffstats
path: root/internal/commands/migrate.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2023-12-30 23:04:22 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2023-12-30 23:04:22 -0500
commit6a414432a2cc90523eced1dccb3f5060b5252a08 (patch)
tree4c9c06a1cd73ccdad31dbb8f2f7c6b7465631e9b /internal/commands/migrate.go
parentTweak migrations dir error handling (diff)
downloadcli-6a414432a2cc90523eced1dccb3f5060b5252a08.zip
Add working create, list, and migrate commands
Diffstat (limited to 'internal/commands/migrate.go')
-rw-r--r--internal/commands/migrate.go33
1 files changed, 27 insertions, 6 deletions
diff --git a/internal/commands/migrate.go b/internal/commands/migrate.go
index fdbddfe..edcfc81 100644
--- a/internal/commands/migrate.go
+++ b/internal/commands/migrate.go
@@ -6,7 +6,9 @@ import (
"fmt"
"os"
+ "github.com/eugenetriguba/bolt/internal/config"
"github.com/eugenetriguba/bolt/internal/db"
+ "github.com/eugenetriguba/bolt/internal/repositories"
"github.com/google/subcommands"
)
@@ -30,16 +32,35 @@ func (m *MigrateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
}
defer db.Close()
- sqlStatement := `
- CREATE TABLE IF NOT EXISTS applied_migration(
- version CHARACTER(32) PRIMARY KEY NOT NULL
- );
- `
- _, err = db.Exec(sqlStatement)
+ c, err := config.NewConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return subcommands.ExitFailure
}
+ migrationRepo, err := repositories.NewMigrationRepo(db, c)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
+
+ migrations, err := migrationRepo.List()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
+
+ for _, migration := range migrations {
+ if !migration.Applied {
+ fmt.Printf("Applying migration for %s..\n", migration.Dirname())
+ err = migrationRepo.Apply(migration)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
+ fmt.Printf("%s [x]\n", migration.Dirname())
+ }
+ }
+
return subcommands.ExitSuccess
}