-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migration box now supports autocommit DDL changes (#818)
To better support CockroachDB, a new migration type ("autocommit") has been added. Autocommit migrations will not be executed in a transaction, which resolves issues in when running large DDL changes in crdb transactions. To use this feature, just add `.autocommit` to the filename: ``` 1234_name.autocommit.up.sql < does not run in transaction 1234_name.up.sql < runs in transaction ``` Works with all combinations: ``` 1234_name.postgres.autocommit.up.sql ```
- Loading branch information
Showing
8 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package popx | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/gobuffalo/pop/v6" | ||
) | ||
|
||
var mrx = regexp.MustCompile( | ||
`^(\d+)_([^.]+)(\.[a-z0-9]+)?(\.autocommit)?\.(up|down)\.(sql)$`, | ||
) | ||
|
||
// Match holds the information parsed from a migration filename. | ||
type Match struct { | ||
Version string | ||
Name string | ||
DBType string | ||
Direction string | ||
Type string | ||
Autocommit bool | ||
} | ||
|
||
// ParseMigrationFilename parses a migration filename. | ||
func ParseMigrationFilename(filename string) (*Match, error) { | ||
matches := mrx.FindAllStringSubmatch(filename, -1) | ||
if len(matches) == 0 { | ||
return nil, nil | ||
} | ||
m := matches[0] | ||
|
||
var autocommit bool | ||
var dbType string | ||
if m[3] == ".autocommit" { | ||
// A special case where autocommit group moves forward to the 3rd index. | ||
autocommit = true | ||
dbType = "all" | ||
} else if m[3] == "" { | ||
dbType = "all" | ||
} else { | ||
dbType = pop.CanonicalDialect(m[3][1:]) | ||
if !pop.DialectSupported(dbType) { | ||
return nil, fmt.Errorf("unsupported dialect %s", dbType) | ||
} | ||
} | ||
|
||
if m[6] == "fizz" && dbType != "all" { | ||
return nil, fmt.Errorf("invalid database type %q, expected \"all\" because fizz is database type independent", dbType) | ||
} | ||
|
||
if m[4] == ".autocommit" { | ||
autocommit = true | ||
} else if m[4] != "" { | ||
return nil, fmt.Errorf("invalid autocommit flag %q", m[4]) | ||
} | ||
|
||
match := &Match{ | ||
Version: m[1], | ||
Name: m[2], | ||
DBType: dbType, | ||
Autocommit: autocommit, | ||
Direction: m[5], | ||
Type: m[6], | ||
} | ||
|
||
return match, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package popx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ParseMigrationFilenameSQLUp(t *testing.T) { | ||
r := require.New(t) | ||
|
||
m, err := ParseMigrationFilename("20190611004000_create_providers.up.sql") | ||
r.NoError(err) | ||
r.NotNil(m) | ||
r.Equal(m.Version, "20190611004000") | ||
r.Equal(m.Name, "create_providers") | ||
r.Equal(m.DBType, "all") | ||
r.Equal(m.Direction, "up") | ||
r.Equal(m.Type, "sql") | ||
r.Equal(m.Autocommit, false) | ||
} | ||
|
||
func Test_ParseMigrationFilenameSQLUpPostgres(t *testing.T) { | ||
r := require.New(t) | ||
|
||
m, err := ParseMigrationFilename("20190611004000_create_providers.pg.up.sql") | ||
r.NoError(err) | ||
r.NotNil(m) | ||
r.Equal(m.Version, "20190611004000") | ||
r.Equal(m.Name, "create_providers") | ||
r.Equal(m.DBType, "postgres") | ||
r.Equal(m.Direction, "up") | ||
r.Equal(m.Type, "sql") | ||
r.Equal(m.Autocommit, false) | ||
} | ||
|
||
func Test_ParseMigrationFilenameSQLUpAutocommit(t *testing.T) { | ||
r := require.New(t) | ||
|
||
m, err := ParseMigrationFilename("20190611004000_create_providers.autocommit.up.sql") | ||
r.NoError(err) | ||
r.NotNil(m) | ||
r.Equal(m.Version, "20190611004000") | ||
r.Equal(m.Name, "create_providers") | ||
r.Equal(m.DBType, "all") | ||
r.Equal(m.Direction, "up") | ||
r.Equal(m.Type, "sql") | ||
r.Equal(m.Autocommit, true) | ||
} | ||
|
||
func Test_ParseMigrationFilenameSQLDownAutocommit(t *testing.T) { | ||
r := require.New(t) | ||
|
||
m, err := ParseMigrationFilename("20190611004000_create_providers.mysql.autocommit.down.sql") | ||
r.NoError(err) | ||
r.NotNil(m) | ||
r.Equal(m.Version, "20190611004000") | ||
r.Equal(m.Name, "create_providers") | ||
r.Equal(m.DBType, "mysql") | ||
r.Equal(m.Direction, "down") | ||
r.Equal(m.Type, "sql") | ||
r.Equal(m.Autocommit, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BEGIN;ROLLBACK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BEGIN;ROLLBACK; |