Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Sep 20, 2024
1 parent 5650198 commit 3b1ca9d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/migrate/internal/commands/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func DriftCommand(logger log.Logger) *cobra.Command {
)

driftCmd := &cobra.Command{
Use: "drift",
Use: "drift 'description.json'",
Short: "Compare the current database schema against the expected schema",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
46 changes: 46 additions & 0 deletions cmd/migrate/internal/commands/force_write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"context"
"fmt"
"strconv"

"github.com/go-nacelle/log/v2"
"github.com/go-nacelle/pgutil/cmd/migrate/internal/database"
"github.com/go-nacelle/pgutil/cmd/migrate/internal/flags"
"github.com/spf13/cobra"
)

func WriteMigrationLogCommand(logger log.Logger) *cobra.Command {
var (
databaseURL string
migrationsDirectory string
)

writeMigrationLogCmd := &cobra.Command{
Use: "write-migration-log <migration-id>",
Short: "Write a successful migration log entry without running a migration",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
migrationID, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("invalid migration ID: %v", err)
}

return writeMigrationLog(databaseURL, migrationsDirectory, logger, migrationID)
},
}

flags.RegisterDatabaseURLFlag(writeMigrationLogCmd, &databaseURL)
flags.RegisterMigrationsDirectoryFlag(writeMigrationLogCmd, &migrationsDirectory)
return writeMigrationLogCmd
}

func writeMigrationLog(databaseURL string, migrationsDirectory string, logger log.Logger, migrationID int) error {
runner, err := database.CreateRunner(databaseURL, migrationsDirectory, logger)
if err != nil {
return err
}

return runner.WriteMigrationLog(context.Background(), migrationID)
}
2 changes: 1 addition & 1 deletion cmd/migrate/internal/commands/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UndoCommand(logger log.Logger) *cobra.Command {
)

undoCmd := &cobra.Command{
Use: "undo [migration_id]",
Use: "undo <migration-id>",
Short: "Undo migrations up to and including the specified migration ID",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/internal/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UpCommand(logger log.Logger) *cobra.Command {
)

upCmd := &cobra.Command{
Use: "up [migration_id]?",
Use: "up [migration_id]",
Short: "Run migrations up to and including the specified migration ID",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func init() {
rootCmd.AddCommand(commands.UpCommand(logger))
rootCmd.AddCommand(commands.UndoCommand(logger))
rootCmd.AddCommand(commands.StatCommand(logger))
rootCmd.AddCommand(commands.WriteMigrationLogCommand(logger))
rootCmd.AddCommand(commands.DescribeCommand(logger))
rootCmd.AddCommand(commands.DriftCommand(logger))
}
Expand Down
4 changes: 2 additions & 2 deletions drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func Compare(a, b SchemaDescription) (statements []string) {
}{
{"drop", "trigger", sortByKey},
{"drop", "view", sortDropViews},
{"drop", "constraint", sortDropViews},
{"drop", "index", sortByKey},
{"drop", "constraint", sortByKey}, // TODO - needs to be primary key/unique, fkey, others
{"drop", "column", sortByKey},
{"drop", "sequence", sortByKey},
{"drop", "table", sortByKey},
Expand All @@ -163,8 +163,8 @@ func Compare(a, b SchemaDescription) (statements []string) {
{"replace", "sequence", sortByKey},
{"create", "column", sortByKey},
{"replace", "column", sortByKey},
{"create", "constraint", sortByKey},
{"create", "index", sortByKey},
{"create", "constraint", sortByKey},
{"create", "view", sortCreateViews},
{"create", "trigger", sortByKey},
}
Expand Down
23 changes: 23 additions & 0 deletions migration_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,29 @@ func (r *Runner) MigrationLogs(ctx context.Context) (map[int]MigrationLog, error
return logMap, nil
}

func (r *Runner) WriteMigrationLog(ctx context.Context, id int) error {
if err := r.ensureMigrationLogsTable(ctx); err != nil {
return err
}

var definition *Definition
for _, def := range r.definitions {
if def.ID == id {
definition = &def
break
}
}

if definition == nil {
return errors.New("migration not found")
}

return r.withMigrationLog(ctx, *definition, false, func(_ int) error {
r.logger.InfoWithFields(log.LogFields{"id": id}, "Forcing writing migration log")
return nil
})
}

func (r *Runner) withMigrationLog(ctx context.Context, definition Definition, reverse bool, f func(id int) error) (err error) {
id, _, err := ScanInt(r.db.Query(ctx, Query(`
INSERT INTO migration_logs (migration_id, reverse)
Expand Down

0 comments on commit 3b1ca9d

Please sign in to comment.