aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse/sqlparse.go
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-11-13 21:43:11 -0800
committerEugene Triguba <hello@eugenetriguba.com>2026-11-13 21:43:11 -0800
commit28dd0bfc1373746b8c056a211f068da71fdc80b2 (patch)
tree9c04a08501bc5abe7413263f60eaa55400dd162f /internal/sqlparse/sqlparse.go
parentRelease v0.13.1 (diff)
downloadcli-28dd0bfc1373746b8c056a211f068da71fdc80b2.zip
Remove migration dependencies
Diffstat (limited to 'internal/sqlparse/sqlparse.go')
-rw-r--r--internal/sqlparse/sqlparse.go71
1 files changed, 16 insertions, 55 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
-}