aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse
diff options
context:
space:
mode:
Diffstat (limited to 'internal/sqlparse')
-rw-r--r--internal/sqlparse/sqlparse.go71
-rw-r--r--internal/sqlparse/sqlparse_test.go105
2 files changed, 20 insertions, 156 deletions
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go
index a43d354..90ebc2f 100644
--- a/internal/sqlparse/sqlparse.go
+++ b/internal/sqlparse/sqlparse.go
@@ -8,13 +8,7 @@ import (
)
type SqlParser interface {
- Parse(reader io.Reader) (Migration, error)
-}
-
-type Migration struct {
- UpgradeScript MigrationScript
- DowngradeScript MigrationScript
- Dependencies []string
+ Parse(reader io.Reader) (MigrationScript, MigrationScript, error)
}
type MigrationScript struct {
@@ -35,7 +29,6 @@ const (
const (
upgradeScriptDeliminator = "-- migrate:up"
downgradeScriptDeliminator = "-- migrate:down"
- dependsDirective = "-- depends:"
transactionOptionName = "transaction"
)
@@ -47,11 +40,12 @@ func NewSqlParser() SqlParser {
return &sqlParser{}
}
-// 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{}}
+// 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{}
scanner := bufio.NewScanner(reader)
currentSection := unknownSection
@@ -61,30 +55,25 @@ func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) {
currentSection = upgradeScriptSection
- migration.UpgradeScript.Options = parseExecutionOptions(line)
+ upgradeScript.Options = parseExecutionOptions(line)
} else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) {
currentSection = downgradeScriptSection
- migration.DowngradeScript.Options = parseExecutionOptions(line)
+ downgradeScript.Options = parseExecutionOptions(line)
} else if currentSection == upgradeScriptSection {
- migration.UpgradeScript.Contents += line + "\n"
+ upgradeScript.Contents += line + "\n"
} else if currentSection == downgradeScriptSection {
- migration.DowngradeScript.Contents += line + "\n"
- }
-
- if strings.HasPrefix(loweredLine, dependsDirective) {
- migration.Dependencies = append(migration.Dependencies, parseDependencies(line)...)
+ downgradeScript.Contents += line + "\n"
}
-
}
if err := scanner.Err(); err != nil {
- return Migration{}, fmt.Errorf(
+ return MigrationScript{}, MigrationScript{}, fmt.Errorf(
"parsing sql file encountered an error: %w",
err,
)
}
- return migration, nil
+ return upgradeScript, downgradeScript, nil
}
// parseExecutionOptions extracts the execution options for a
@@ -92,10 +81,10 @@ func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
func parseExecutionOptions(line string) ExecutionOptions {
options := ExecutionOptions{UseTransaction: true}
- parts := strings.SplitSeq(line, " ")
- for part := range parts {
+ parts := strings.Split(line, " ")
+ for _, part := range parts {
part = strings.ToLower(part)
- if strings.Contains(part, fmt.Sprintf("%s:", transactionOptionName)) {
+ if strings.Contains(part, "transaction:") {
option := strings.Split(part, ":")[1]
options.UseTransaction = option != "false"
}
@@ -103,31 +92,3 @@ 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 20ba72f..5cbbc07 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()
- migration, err := sqlParser.Parse(
+ upgradeScript, downgradeScript, err := sqlParser.Parse(
strings.NewReader(tc.migration),
)
assert.Nil(t, err)
- check.DeepEqual(t, migration.UpgradeScript, tc.expectedUpgradeScript)
- check.DeepEqual(t, migration.DowngradeScript, tc.expectedDowngradeScript)
+ check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript)
+ check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript)
}
}
@@ -215,104 +215,7 @@ 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)
- })
- }
-}