mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-18 13:34:08 +01:00
43 lines
930 B
Go
43 lines
930 B
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/pressly/goose/v3"
|
|
"github.com/spf13/cobra"
|
|
|
|
migrations "github.com/forfarm/backend/internal"
|
|
)
|
|
|
|
func MigrateCmd(ctx context.Context, dbDriver, dbSource string) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "migrate",
|
|
Short: "Run database migrations",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
|
|
|
db, err := sql.Open(dbDriver, dbSource)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
goose.SetBaseFS(migrations.EmbedMigrations)
|
|
|
|
if err := goose.UpContext(ctx, db, "migrations"); err != nil {
|
|
return fmt.Errorf("failed to run migrations: %w", err)
|
|
}
|
|
|
|
logger.Info("Database migrations have been applied successfully")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|