Skip to content

Commit

Permalink
feat: add transaction option to CreateSQLMigrations
Browse files Browse the repository at this point in the history
Allow CreateSQLMigrations to use .tx.sql and use a transactional template
  • Loading branch information
codeliger committed Oct 6, 2023
1 parent 8a43835 commit 8d5ed0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newDBCommand(migrator *migrate.Migrator) *cli.Command {
Usage: "create up and down SQL migrations",
Action: func(c *cli.Context) error {
name := strings.Join(c.Args().Slice(), "_")
files, err := migrator.CreateSQLMigrations(c.Context, name)
files, err := migrator.CreateSQLMigrations(c.Context, name, false)
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,26 @@ func (m *Migrator) CreateGoMigration(
}

// CreateSQLMigrations creates an up and down SQL migration files.
func (m *Migrator) CreateSQLMigrations(ctx context.Context, name string) ([]*MigrationFile, error) {
func (m *Migrator) CreateSQLMigrations(ctx context.Context, name string, transactional bool) ([]*MigrationFile, error) {
name, err := m.genMigrationName(name)
if err != nil {
return nil, err
}

up, err := m.createSQL(ctx, name+".up.sql")
upSuffix := ".up.sql"
downSuffix := ".down.sql"

if transactional {
upSuffix = ".up.tx.sql"
downSuffix = ".down.tx.sql"
}

up, err := m.createSQL(ctx, name+upSuffix)
if err != nil {
return nil, err
}

down, err := m.createSQL(ctx, name+".down.sql")
down, err := m.createSQL(ctx, name+downSuffix)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8d5ed0e

Please sign in to comment.