From 64b2135685a69c6b7dab2ffcebb8ecbc19b0902e Mon Sep 17 00:00:00 2001 From: dysphoricUnicorn Date: Sat, 28 Dec 2024 22:24:08 +0100 Subject: [PATCH] Add rollback command --- .../action/debug/rollback/rollback.go | 12 ++++++ cmd/gotosocial/debug.go | 13 ++++++ internal/db/bundb/bundb.go | 43 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 cmd/gotosocial/action/debug/rollback/rollback.go diff --git a/cmd/gotosocial/action/debug/rollback/rollback.go b/cmd/gotosocial/action/debug/rollback/rollback.go new file mode 100644 index 000000000..3d443fa2d --- /dev/null +++ b/cmd/gotosocial/action/debug/rollback/rollback.go @@ -0,0 +1,12 @@ +package rollback + +import ( + "context" + + "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" + "github.com/superseriousbusiness/gotosocial/internal/db/bundb" +) + +var Rollback action.GTSAction = func(ctx context.Context) (err error) { + return bundb.DoRollback(ctx) +} diff --git a/cmd/gotosocial/debug.go b/cmd/gotosocial/debug.go index c01baeb8b..f08ec2b16 100644 --- a/cmd/gotosocial/debug.go +++ b/cmd/gotosocial/debug.go @@ -20,6 +20,7 @@ package main import ( "github.com/spf13/cobra" configaction "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/debug/config" + rollbackaction "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/debug/rollback" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -41,5 +42,17 @@ func debugCommands() *cobra.Command { } config.AddServerFlags(debugConfigCmd) debugCmd.AddCommand(debugConfigCmd) + + debugRollbackCmd := &cobra.Command{ + Use: "rollback", + Short: "roll back the last run database migration", + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd.Context(), rollbackaction.Rollback) + }, + } + + config.AddServerFlags(debugRollbackCmd) + debugCmd.AddCommand(debugRollbackCmd) + return debugCmd } diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index cf612fd2e..b9b85f162 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -130,6 +130,49 @@ func doMigration(ctx context.Context, db *bun.DB) error { return nil } +func DoRollback(ctx context.Context) error { + var sqldb *sql.DB + var dialect func() schema.Dialect + var err error + + switch t := strings.ToLower(config.GetDbType()); t { + case "postgres": + sqldb, dialect, err = pgConn(ctx) + if err != nil { + return err + } + case "sqlite": + sqldb, dialect, err = sqliteConn(ctx) + if err != nil { + return err + } + default: + return fmt.Errorf("database type %s not supported for bundb", t) + } + + db := bunDB(sqldb, dialect) + + migrator := migrate.NewMigrator(db, migrations.Migrations) + + if err := migrator.Lock(ctx); err != nil { + return err + } + defer migrator.Unlock(ctx) //nolint:errcheck + + group, err := migrator.Rollback(ctx) + if err != nil { + return err + } + + if group.IsZero() { + fmt.Printf("there are no groups to roll back\n") + return nil + } + + fmt.Printf("rolled back %s\n", group) + return nil +} + // NewBunDBService returns a bunDB derived from the provided config, which implements the go-fed DB interface. // Under the hood, it uses https://github.com/uptrace/bun to create and maintain a database connection. func NewBunDBService(ctx context.Context, state *state.State) (db.DB, error) {