Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@ var (

migrateDownCmd = &cobra.Command{
Use: "down",
Short: "Run database migrations",
Short: "Run database migration down one version",
Run: func(cmd *cobra.Command, args []string) {
dbClient, err := migrateDbClient()
if err != nil {
panic(fmt.Errorf("could not load config: %w", err))
}

res, err := dbClient.MigrationDown()
err = dbClient.MigrationDown(cmd.Context())
if err != nil {
panic(fmt.Errorf("migration down failed: %w", err))
}
fmt.Print("migration down applied: ", slog.Any("res", res))
fmt.Print("migration down applied successfully")
},
}
migrateUpCmd = &cobra.Command{
Use: "up",
Short: "Run database migrations up to the latest version",
Run: func(cmd *cobra.Command, args []string) {
dbClient, err := migrateDbClient()
if err != nil {
panic(fmt.Errorf("could not load config: %w", err))
}

count, err := dbClient.RunMigrations(cmd.Context())
if err != nil {
panic(fmt.Errorf("migration up failed: %w", err))
}
fmt.Print("migration up applied: ", slog.Any("versions up", count))
},
}
)
Expand All @@ -47,10 +63,10 @@ func migrateDbClient() (*db.Client, error) {
return nil, err
}
return dbClient, nil

}

func init() {
migrateCmd.AddCommand(migrateDownCmd)
migrateCmd.AddCommand(migrateUpCmd)
rootCmd.AddCommand(migrateCmd)
}
Loading