aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse/sqlparse.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/sqlparse/sqlparse.go71
1 files changed, 55 insertions, 16 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
+}