From 00aa327eb204d504f1e03dbe5a7cdec4af4ec913 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 13 Jan 2024 18:23:48 -0500 Subject: Rework docs, add more output, and tweak tests --- internal/commands/create.go | 71 ----------------------------- internal/commands/down.go | 80 +++++++++++++++++++++++++++++++++ internal/commands/downgrade.go | 70 ----------------------------- internal/commands/list.go | 74 ------------------------------ internal/commands/migrate.go | 70 ----------------------------- internal/commands/new.go | 81 +++++++++++++++++++++++++++++++++ internal/commands/status.go | 100 +++++++++++++++++++++++++++++++++++++++++ internal/commands/up.go | 79 ++++++++++++++++++++++++++++++++ 8 files changed, 340 insertions(+), 285 deletions(-) delete mode 100644 internal/commands/create.go create mode 100644 internal/commands/down.go delete mode 100644 internal/commands/downgrade.go delete mode 100644 internal/commands/list.go delete mode 100644 internal/commands/migrate.go create mode 100644 internal/commands/new.go create mode 100644 internal/commands/status.go create mode 100644 internal/commands/up.go (limited to 'internal/commands') diff --git a/internal/commands/create.go b/internal/commands/create.go deleted file mode 100644 index 57f4c69..0000000 --- a/internal/commands/create.go +++ /dev/null @@ -1,71 +0,0 @@ -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/repositories" - "github.com/eugenetriguba/bolt/internal/services" - "github.com/google/subcommands" -) - -type CreateCmd struct { - message string -} - -func (*CreateCmd) Name() string { return "create" } -func (*CreateCmd) Synopsis() string { - return "Create a new database migration." -} -func (*CreateCmd) Usage() string { - return `create [-message]: - Create a new database migration. - ` -} - -func (cmd *CreateCmd) SetFlags(f *flag.FlagSet) { - f.StringVar( - &cmd.message, - "message", - "autogenerated", - "Message to use for the migration", - ) -} - -func (cmd *CreateCmd) Execute( - _ context.Context, - f *flag.FlagSet, - _ ...interface{}, -) subcommands.ExitStatus { - cfg, err := configloader.NewConfig() - 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(nil, migrationFsRepo) - if err != nil { - fmt.Fprintln(os.Stderr, err) - return subcommands.ExitFailure - } - - migration := models.NewMigration(time.Now(), cmd.message) - err = migrationService.CreateMigration(migration) - if err != nil { - fmt.Fprintln(os.Stderr, err) - return subcommands.ExitFailure - } - - return subcommands.ExitSuccess -} diff --git a/internal/commands/down.go b/internal/commands/down.go new file mode 100644 index 0000000..0a9ca99 --- /dev/null +++ b/internal/commands/down.go @@ -0,0 +1,80 @@ +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 DownCmd struct{} + +func (*DownCmd) Name() string { + return "down" +} + +func (*DownCmd) Synopsis() string { + return "downgrade migrations against the database" +} + +func (*DownCmd) Usage() string { + return `down: + Downgrade migrations against the database. + ` +} + +func (m *DownCmd) SetFlags(f *flag.FlagSet) {} + +func (m *DownCmd) 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, + ) + err = migrationService.RevertAllMigrations() + if err != nil { + consoleOutputter.Error(err.Error()) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} diff --git a/internal/commands/downgrade.go b/internal/commands/downgrade.go deleted file mode 100644 index f953286..0000000 --- a/internal/commands/downgrade.go +++ /dev/null @@ -1,70 +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 DowngradeCmd struct{} - -func (*DowngradeCmd) Name() string { return "downgrade" } - -func (*DowngradeCmd) Synopsis() string { return "Downgrade all migrations against the database." } -func (*DowngradeCmd) Usage() string { - return `downgrade: - Downgrade all migrations against the database. - ` -} - -func (m *DowngradeCmd) SetFlags(f *flag.FlagSet) {} - -func (m *DowngradeCmd) 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) - err = migrationService.RevertAllMigrations() - if err != nil { - fmt.Fprintln(os.Stderr, err) - return subcommands.ExitFailure - } - - return subcommands.ExitSuccess -} 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/migrate.go b/internal/commands/migrate.go deleted file mode 100644 index 8c86291..0000000 --- a/internal/commands/migrate.go +++ /dev/null @@ -1,70 +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 UpgradeCmd struct{} - -func (*UpgradeCmd) Name() string { return "upgrade" } - -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 (m *UpgradeCmd) SetFlags(f *flag.FlagSet) {} - -func (m *UpgradeCmd) 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) - err = migrationService.ApplyAllMigrations() - if err != nil { - fmt.Fprintln(os.Stderr, err) - return subcommands.ExitFailure - } - - return subcommands.ExitSuccess -} diff --git a/internal/commands/new.go b/internal/commands/new.go new file mode 100644 index 0000000..be1f49c --- /dev/null +++ b/internal/commands/new.go @@ -0,0 +1,81 @@ +package commands + +import ( + "context" + "flag" + "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 NewCmd struct { + message string +} + +func (*NewCmd) Name() string { + return "new" +} + +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 *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 *NewCmd) 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 + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir) + if err != nil { + consoleOutputter.Error(err.Error()) + return subcommands.ExitFailure + } + + migrationService := services.NewMigrationService( + nil, + migrationFsRepo, + consoleOutputter, + ) + if err != nil { + consoleOutputter.Error(err.Error()) + return subcommands.ExitFailure + } + + migration := models.NewMigration(time.Now(), cmd.message) + err = migrationService.CreateMigration(migration) + if err != nil { + consoleOutputter.Error(err.Error()) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} 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/up.go b/internal/commands/up.go new file mode 100644 index 0000000..c207a83 --- /dev/null +++ b/internal/commands/up.go @@ -0,0 +1,79 @@ +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 UpCmd struct{} + +func (*UpCmd) Name() string { + return "up" +} + +func (*UpCmd) Synopsis() string { + return "apply migrations against the database" +} + +func (*UpCmd) Usage() string { + return `up: + Apply migrations against the database + ` +} + +func (m *UpCmd) SetFlags(f *flag.FlagSet) {} + +func (m *UpCmd) 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, + ) + err = migrationService.ApplyAllMigrations() + if err != nil { + consoleOutputter.Error(err.Error()) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} -- cgit v1.2.3