github.com/supabase/cli@v1.168.1/cmd/migration.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "os/signal" 7 8 "github.com/spf13/afero" 9 "github.com/spf13/cobra" 10 "github.com/spf13/viper" 11 "github.com/supabase/cli/internal/migration/fetch" 12 "github.com/supabase/cli/internal/migration/list" 13 "github.com/supabase/cli/internal/migration/new" 14 "github.com/supabase/cli/internal/migration/repair" 15 "github.com/supabase/cli/internal/migration/squash" 16 "github.com/supabase/cli/internal/migration/up" 17 "github.com/supabase/cli/internal/utils" 18 "github.com/supabase/cli/internal/utils/flags" 19 ) 20 21 var ( 22 migrationCmd = &cobra.Command{ 23 GroupID: groupLocalDev, 24 Use: "migration", 25 Aliases: []string{"migrations"}, 26 Short: "Manage database migration scripts", 27 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 28 ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt) 29 cmd.SetContext(ctx) 30 return cmd.Root().PersistentPreRunE(cmd, args) 31 }, 32 } 33 34 migrationListCmd = &cobra.Command{ 35 Use: "list", 36 Short: "List local and remote migrations", 37 RunE: func(cmd *cobra.Command, args []string) error { 38 return list.Run(cmd.Context(), flags.DbConfig, afero.NewOsFs()) 39 }, 40 } 41 42 migrationNewCmd = &cobra.Command{ 43 Use: "new <migration name>", 44 Short: "Create an empty migration script", 45 Args: cobra.ExactArgs(1), 46 RunE: func(cmd *cobra.Command, args []string) error { 47 return new.Run(args[0], os.Stdin, afero.NewOsFs()) 48 }, 49 } 50 51 targetStatus = utils.EnumFlag{ 52 Allowed: []string{ 53 repair.Applied, 54 repair.Reverted, 55 }, 56 } 57 58 migrationRepairCmd = &cobra.Command{ 59 Use: "repair [version] ...", 60 Short: "Repair the migration history table", 61 RunE: func(cmd *cobra.Command, args []string) error { 62 return repair.Run(cmd.Context(), flags.DbConfig, args, targetStatus.Value, afero.NewOsFs()) 63 }, 64 PostRun: func(cmd *cobra.Command, args []string) { 65 fmt.Println("Finished " + utils.Aqua("supabase migration repair") + ".") 66 }, 67 } 68 69 migrationVersion string 70 71 migrationSquashCmd = &cobra.Command{ 72 Use: "squash", 73 Short: "Squash migrations to a single file", 74 RunE: func(cmd *cobra.Command, args []string) error { 75 return squash.Run(cmd.Context(), migrationVersion, flags.DbConfig, afero.NewOsFs()) 76 }, 77 PostRun: func(cmd *cobra.Command, args []string) { 78 fmt.Println("Finished " + utils.Aqua("supabase migration squash") + ".") 79 }, 80 } 81 82 migrationUpCmd = &cobra.Command{ 83 Use: "up", 84 Short: "Apply pending migrations to local database", 85 RunE: func(cmd *cobra.Command, args []string) error { 86 return up.Run(cmd.Context(), includeAll, flags.DbConfig, afero.NewOsFs()) 87 }, 88 PostRun: func(cmd *cobra.Command, args []string) { 89 fmt.Println("Local database is up to date.") 90 }, 91 } 92 93 migrationFetchCmd = &cobra.Command{ 94 Use: "fetch", 95 Short: "Fetch migration files from history table", 96 RunE: func(cmd *cobra.Command, args []string) error { 97 return fetch.Run(cmd.Context(), flags.DbConfig, afero.NewOsFs()) 98 }, 99 } 100 ) 101 102 func init() { 103 // Build list command 104 listFlags := migrationListCmd.Flags() 105 listFlags.String("db-url", "", "Lists migrations of the database specified by the connection string (must be percent-encoded).") 106 listFlags.Bool("linked", true, "Lists migrations applied to the linked project.") 107 listFlags.Bool("local", false, "Lists migrations applied to the local database.") 108 migrationListCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local") 109 listFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.") 110 cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", listFlags.Lookup("password"))) 111 migrationListCmd.MarkFlagsMutuallyExclusive("db-url", "password") 112 migrationCmd.AddCommand(migrationListCmd) 113 // Build repair command 114 repairFlags := migrationRepairCmd.Flags() 115 repairFlags.Var(&targetStatus, "status", "Version status to update.") 116 cobra.CheckErr(migrationRepairCmd.MarkFlagRequired("status")) 117 repairFlags.String("db-url", "", "Repairs migrations of the database specified by the connection string (must be percent-encoded).") 118 repairFlags.Bool("linked", true, "Repairs the migration history of the linked project.") 119 repairFlags.Bool("local", false, "Repairs the migration history of the local database.") 120 migrationRepairCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local") 121 repairFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.") 122 cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", repairFlags.Lookup("password"))) 123 migrationRepairCmd.MarkFlagsMutuallyExclusive("db-url", "password") 124 migrationCmd.AddCommand(migrationRepairCmd) 125 // Build squash command 126 squashFlags := migrationSquashCmd.Flags() 127 squashFlags.StringVar(&migrationVersion, "version", "", "Squash up to the specified version.") 128 squashFlags.String("db-url", "", "Squashes migrations of the database specified by the connection string (must be percent-encoded).") 129 squashFlags.Bool("linked", false, "Squashes the migration history of the linked project.") 130 squashFlags.Bool("local", true, "Squashes the migration history of the local database.") 131 migrationSquashCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local") 132 squashFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.") 133 cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", squashFlags.Lookup("password"))) 134 migrationSquashCmd.MarkFlagsMutuallyExclusive("db-url", "password") 135 migrationCmd.AddCommand(migrationSquashCmd) 136 // Build up command 137 upFlags := migrationUpCmd.Flags() 138 upFlags.BoolVar(&includeAll, "include-all", false, "Include all migrations not found on remote history table.") 139 upFlags.String("db-url", "", "Applies migrations to the database specified by the connection string (must be percent-encoded).") 140 upFlags.Bool("linked", false, "Applies pending migrations to the linked project.") 141 upFlags.Bool("local", true, "Applies pending migrations to the local database.") 142 migrationUpCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local") 143 migrationCmd.AddCommand(migrationUpCmd) 144 // Build up command 145 fetchFlags := migrationFetchCmd.Flags() 146 fetchFlags.String("db-url", "", "Fetches migrations from the database specified by the connection string (must be percent-encoded).") 147 fetchFlags.Bool("linked", true, "Fetches migration history from the linked project.") 148 fetchFlags.Bool("local", false, "Fetches migration history from the local database.") 149 migrationFetchCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local") 150 migrationCmd.AddCommand(migrationFetchCmd) 151 // Build new command 152 migrationCmd.AddCommand(migrationNewCmd) 153 rootCmd.AddCommand(migrationCmd) 154 }