aboutsummaryrefslogtreecommitdiffstats
path: root/internal/commands
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
parentTweak migrations dir error handling (diff)
downloadcli-6a414432a2cc90523eced1dccb3f5060b5252a08.zip
Add working create, list, and migrate commands
Diffstat (limited to 'internal/commands')
-rw-r--r--internal/commands/create.go55
-rw-r--r--internal/commands/list.go58
-rw-r--r--internal/commands/migrate.go33
3 files changed, 125 insertions, 21 deletions
diff --git a/internal/commands/create.go b/internal/commands/create.go
index ab37eca..b9d63df 100644
--- a/internal/commands/create.go
+++ b/internal/commands/create.go
@@ -6,36 +6,61 @@ import (
"fmt"
"os"
+ "github.com/eugenetriguba/bolt/internal/config"
"github.com/eugenetriguba/bolt/internal/db"
+ "github.com/eugenetriguba/bolt/internal/models"
+ "github.com/eugenetriguba/bolt/internal/repositories"
"github.com/google/subcommands"
)
-type CreateCmd struct{}
+type CreateCmd struct {
+ message string
+}
-func (*CreateCmd) Name() string { return "create" }
-func (*CreateCmd) Synopsis() string { return "Create a new database migration." }
+func (*CreateCmd) Name() string { return "create" }
+func (*CreateCmd) Synopsis() string {
+ return "Create a new database migration."
+}
func (*CreateCmd) Usage() string {
- return `create:
+ return `create [-message]:
Create a new database migration.
`
}
-func (m *CreateCmd) SetFlags(f *flag.FlagSet) {}
+func (c *CreateCmd) SetFlags(f *flag.FlagSet) {
+ f.StringVar(
+ &c.message,
+ "message",
+ "autogenerated",
+ "Message to use for the migration",
+ )
+}
+
+func (m *CreateCmd) Execute(
+ _ context.Context,
+ f *flag.FlagSet,
+ _ ...interface{},
+) subcommands.ExitStatus {
+ conn, err := db.Connect()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
+
+ c, err := config.NewConfig()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
-func (m *CreateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
- db, err := db.Connect()
+ migrationRepo, err := repositories.NewMigrationRepo(conn, c)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return subcommands.ExitFailure
}
- defer db.Close()
-
- sqlStatement := `
- CREATE TABLE IF NOT EXISTS applied_migration(
- version CHARACTER(32) PRIMARY KEY NOT NULL
- );
- `
- _, err = db.Exec(sqlStatement)
+
+ migration := models.NewMigration(m.message)
+ err = migrationRepo.Create(migration)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return subcommands.ExitFailure
diff --git a/internal/commands/list.go b/internal/commands/list.go
new file mode 100644
index 0000000..ba7958d
--- /dev/null
+++ b/internal/commands/list.go
@@ -0,0 +1,58 @@
+package commands
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "os"
+
+ "github.com/eugenetriguba/bolt/internal/config"
+ "github.com/eugenetriguba/bolt/internal/db"
+ "github.com/eugenetriguba/bolt/internal/repositories"
+ "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 {
+ db, err := db.Connect()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return subcommands.ExitFailure
+ }
+ defer db.Close()
+
+ 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 {
+ fmt.Println(migration)
+ }
+
+ return subcommands.ExitSuccess
+}
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
}