//go:build dev /* Package cmd Copyright © 2024 Shane C. */ package cmd import ( "context" "github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/stdlib" "github.com/spf13/cobra" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/migrate" "gitlab.com/omnibill/tui/textinput" "log" "omnibill.net/omnibill/migrations" "omnibill.net/omnibill/shared" "strings" "unicode" ) // migrationCmd represents the migration command var migrationCmd = &cobra.Command{ Use: "migration", Short: "Generates a migration", Run: func(cmd *cobra.Command, args []string) { migrationName, err := textinput.New(textinput.InputData{ Question: "Name of Migration?", }) if err != nil { log.Fatalln(err) } dsn := shared.GetPostgresURI() config, err := pgxpool.ParseConfig(dsn) if err != nil { log.Fatalln(err) } pool, err := pgxpool.NewWithConfig(context.Background(), config) if err != nil { log.Fatalln(err) } sqldb := stdlib.OpenDBFromPool(pool) db := bun.NewDB(sqldb, pgdialect.New()) migrator := migrate.NewMigrator(db, migrations.Migrations) name := strings.ToLower(strings.ReplaceAll(removeSymbols(*migrationName), " ", "_")) _, err = migrator.CreateGoMigration(context.Background(), name) if err != nil { log.Fatalln(err) } }, } func init() { generateCmd.AddCommand(migrationCmd) } func removeSymbols(s string) string { var result string for _, char := range s { if unicode.IsLetter(char) || unicode.IsDigit(char) { result += string(char) } } return result }