mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-30 08:12:27 -05:00
Add rollback command
This commit is contained in:
parent
0784aa3218
commit
64b2135685
3 changed files with 68 additions and 0 deletions
12
cmd/gotosocial/action/debug/rollback/rollback.go
Normal file
12
cmd/gotosocial/action/debug/rollback/rollback.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
configaction "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/debug/config"
|
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"
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,5 +42,17 @@ func debugCommands() *cobra.Command {
|
||||||
}
|
}
|
||||||
config.AddServerFlags(debugConfigCmd)
|
config.AddServerFlags(debugConfigCmd)
|
||||||
debugCmd.AddCommand(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
|
return debugCmd
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,49 @@ func doMigration(ctx context.Context, db *bun.DB) error {
|
||||||
return nil
|
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.
|
// 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.
|
// 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) {
|
func NewBunDBService(ctx context.Context, state *state.State) (db.DB, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue