Skip to content

Commit 3d5a48e

Browse files
committed
[ADD] create new repeatable migration
1 parent 7417d66 commit 3d5a48e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

cmd/migration.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ var (
3939
},
4040
}
4141

42+
repeatable bool
43+
4244
migrationNewCmd = &cobra.Command{
4345
Use: "new <migration name>",
4446
Short: "Create an empty migration script",
4547
Args: cobra.ExactArgs(1),
4648
RunE: func(cmd *cobra.Command, args []string) error {
47-
return new.Run(args[0], os.Stdin, afero.NewOsFs())
49+
return new.Run(repeatable, args[0], os.Stdin, afero.NewOsFs())
4850
},
4951
}
5052

@@ -149,6 +151,8 @@ func init() {
149151
migrationFetchCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local")
150152
migrationCmd.AddCommand(migrationFetchCmd)
151153
// Build new command
154+
newFlags := migrationNewCmd.Flags()
155+
newFlags.BoolVarP(&repeatable, "repeatable", "r", false, "Creates a repeatable migration instead of a common migration.")
152156
migrationCmd.AddCommand(migrationNewCmd)
153157
rootCmd.AddCommand(migrationCmd)
154158
}

internal/migration/new/new.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ import (
1111
"github.com/supabase/cli/internal/utils"
1212
)
1313

14-
func Run(migrationName string, stdin afero.File, fsys afero.Fs) error {
15-
path := GetMigrationPath(utils.GetCurrentTimestamp(), migrationName)
14+
func Run(repeatable bool, migrationName string, stdin afero.File, fsys afero.Fs) error {
15+
path := ""
16+
17+
if repeatable {
18+
// if migration name already exists, repeatable migration will be overwritten
19+
path = GetRepeatableMigrationPath(migrationName)
20+
} else {
21+
path = GetMigrationPath(utils.GetCurrentTimestamp(), migrationName)
22+
}
23+
1624
if err := utils.MkdirIfNotExistFS(fsys, filepath.Dir(path)); err != nil {
1725
return err
1826
}
@@ -33,6 +41,11 @@ func GetMigrationPath(timestamp, name string) string {
3341
return filepath.Join(utils.MigrationsDir, fullName)
3442
}
3543

44+
func GetRepeatableMigrationPath(name string) string {
45+
fullName := fmt.Sprintf("r_%s.sql", name)
46+
return filepath.Join(utils.MigrationsDir, fullName)
47+
}
48+
3649
func CopyStdinIfExists(stdin afero.File, dst io.Writer) error {
3750
if fi, err := stdin.Stat(); err != nil {
3851
return errors.Errorf("failed to initialise stdin: %w", err)

0 commit comments

Comments
 (0)