diff options
Diffstat (limited to 'internal/sqlparse')
| -rw-r--r-- | internal/sqlparse/sqlparse.go | 71 | ||||
| -rw-r--r-- | internal/sqlparse/sqlparse_test.go | 105 |
2 files changed, 156 insertions, 20 deletions
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go index 90ebc2f..a43d354 100644 --- a/internal/sqlparse/sqlparse.go +++ b/internal/sqlparse/sqlparse.go @@ -8,7 +8,13 @@ import ( ) type SqlParser interface { - Parse(reader io.Reader) (MigrationScript, MigrationScript, error) + Parse(reader io.Reader) (Migration, error) +} + +type Migration struct { + UpgradeScript MigrationScript + DowngradeScript MigrationScript + Dependencies []string } type MigrationScript struct { @@ -29,6 +35,7 @@ const ( const ( upgradeScriptDeliminator = "-- migrate:up" downgradeScriptDeliminator = "-- migrate:down" + dependsDirective = "-- depends:" transactionOptionName = "transaction" ) @@ -40,12 +47,11 @@ func NewSqlParser() SqlParser { return &sqlParser{} } -// Parse parses out the migration's upgrade and downgrade scripts -// and any custom execution options with those scripts. It returns -// the upgrade script, downgrade script, and any error if one occurred. -func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, error) { - upgradeScript := MigrationScript{} - downgradeScript := MigrationScript{} +// Parse parses out the migration's upgrade and downgrade scripts, +// any custom execution options with those scripts, and migration dependencies. +// It returns the upgrade script, downgrade script, dependencies, and any error if one occurred. +func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) { + migration := Migration{Dependencies: []string{}} scanner := bufio.NewScanner(reader) currentSection := unknownSection @@ -55,25 +61,30 @@ func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) { currentSection = upgradeScriptSection - upgradeScript.Options = parseExecutionOptions(line) + migration.UpgradeScript.Options = parseExecutionOptions(line) } else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) { currentSection = downgradeScriptSection - downgradeScript.Options = parseExecutionOptions(line) + migration.DowngradeScript.Options = parseExecutionOptions(line) } else if currentSection == upgradeScriptSection { - upgradeScript.Contents += line + "\n" + migration.UpgradeScript.Contents += line + "\n" } else if currentSection == downgradeScriptSection { - downgradeScript.Contents += line + "\n" + migration.DowngradeScript.Contents += line + "\n" + } + + if strings.HasPrefix(loweredLine, dependsDirective) { + migration.Dependencies = append(migration.Dependencies, parseDependencies(line)...) } + } if err := scanner.Err(); err != nil { - return MigrationScript{}, MigrationScript{}, fmt.Errorf( + return Migration{}, fmt.Errorf( "parsing sql file encountered an error: %w", err, ) } - return upgradeScript, downgradeScript, nil + return migration, nil } // parseExecutionOptions extracts the execution options for a @@ -81,10 +92,10 @@ func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, func parseExecutionOptions(line string) ExecutionOptions { options := ExecutionOptions{UseTransaction: true} - parts := strings.Split(line, " ") - for _, part := range parts { + parts := strings.SplitSeq(line, " ") + for part := range parts { part = strings.ToLower(part) - if strings.Contains(part, "transaction:") { + if strings.Contains(part, fmt.Sprintf("%s:", transactionOptionName)) { option := strings.Split(part, ":")[1] options.UseTransaction = option != "false" } @@ -92,3 +103,31 @@ func parseExecutionOptions(line string) ExecutionOptions { return options } + +// parseDependencies extracts migration dependencies from a depends directive. +// Expected format: -- depends: 001, 002, 003 +func parseDependencies(line string) []string { + dependencies := []string{} + + loweredLine := strings.ToLower(line) + prefixIdx := strings.Index(loweredLine, dependsDirective) + if prefixIdx == -1 { + return dependencies + } + + depsStr := line[prefixIdx+len(dependsDirective):] + depsStr = strings.TrimSpace(depsStr) + if depsStr == "" { + return dependencies + } + + deps := strings.SplitSeq(depsStr, ",") + for dep := range deps { + dep = strings.TrimSpace(dep) + if dep != "" { + dependencies = append(dependencies, dep) + } + } + + return dependencies +} diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 5cbbc07..20ba72f 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -182,12 +182,12 @@ func TestSqlParser_Parse(t *testing.T) { for _, tc := range testCases { sqlParser := sqlparse.NewSqlParser() - upgradeScript, downgradeScript, err := sqlParser.Parse( + migration, err := sqlParser.Parse( strings.NewReader(tc.migration), ) assert.Nil(t, err) - check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript) - check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript) + check.DeepEqual(t, migration.UpgradeScript, tc.expectedUpgradeScript) + check.DeepEqual(t, migration.DowngradeScript, tc.expectedDowngradeScript) } } @@ -215,7 +215,104 @@ func TestSqlParserParseError(t *testing.T) { ErrCond: " ", } sqlParser := sqlparse.NewSqlParser() - _, _, err := sqlParser.Parse(reader) + _, err := sqlParser.Parse(reader) assert.NotNil(t, err) assert.ErrorContains(t, err, "unwanted input encountered") } + +func TestSqlParser_ParseDependencies(t *testing.T) { + testCases := []struct { + name string + migration string + expectedDependencies []string + }{ + { + name: "no dependencies", + migration: "-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{}, + }, + { + name: "single dependency", + migration: "-- depends: 001\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001"}, + }, + { + name: "multiple dependencies", + migration: "-- depends: 001, 002, 003\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001", "002", "003"}, + }, + { + name: "multiple dependencies without spaces", + migration: "-- depends: 001,002,003\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001", "002", "003"}, + }, + { + name: "case insensitive depends", + migration: "-- DEPENDS: 001, 002\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001", "002"}, + }, + { + name: "mixed case depends", + migration: "-- Depends: 001\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001"}, + }, + { + name: "empty depends directive", + migration: "-- depends: \n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{}, + }, + { + name: "depends with trailing comma", + migration: "-- depends: 001, 002,\n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001", "002"}, + }, + { + name: "depends with extra whitespace", + migration: "-- depends: 001 , 002 , 003 \n-- migrate:up\nCREATE TABLE users(id int);", + expectedDependencies: []string{"001", "002", "003"}, + }, + { + name: "timestamp version dependencies", + migration: "-- depends: 20240316145038, 20240317120000\n-- migrate:up\nCREATE TABLE posts(id int);", + expectedDependencies: []string{"20240316145038", "20240317120000"}, + }, + { + name: "multiple depends directives", + migration: `-- depends: 001 +-- depends: 002 +-- migrate:up +CREATE TABLE users(id int);`, + expectedDependencies: []string{"001", "002"}, + }, + { + name: "depends before up and down", + migration: `-- depends: 001, 002 +-- migrate:up +CREATE TABLE users(id int); +-- migrate:down +DROP TABLE users;`, + expectedDependencies: []string{"001", "002"}, + }, + { + name: "depends anywhere in file", + migration: `-- depends: 004 +-- migrate:up +-- depends: 003 +CREATE TABLE users(id int); +-- migrate:down +-- depends: 002 +DROP TABLE users; +-- depends: 001`, + expectedDependencies: []string{"004", "003", "002", "001"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + sqlParser := sqlparse.NewSqlParser() + migration, err := sqlParser.Parse(strings.NewReader(tc.migration)) + assert.Nil(t, err) + check.DeepEqual(t, migration.Dependencies, tc.expectedDependencies) + }) + } +} |