aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse/sqlparse.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-03-16 15:06:30 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-03-16 15:06:30 -0400
commit65ded7c0133ae011a30546fb4fd1e00fc3179874 (patch)
tree305c9655c581ba5c959d56cf801a96bf80683dd7 /internal/sqlparse/sqlparse.go
parentUpdate README (diff)
downloadcli-65ded7c0133ae011a30546fb4fd1e00fc3179874.zip
Update migration file format
Diffstat (limited to '')
-rw-r--r--internal/sqlparse/sqlparse.go78
1 files changed, 61 insertions, 17 deletions
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go
index e986068..fa9fa96 100644
--- a/internal/sqlparse/sqlparse.go
+++ b/internal/sqlparse/sqlparse.go
@@ -8,40 +8,84 @@ import (
)
type SqlParser interface {
- Parse(reader io.Reader) (ExecutionOptions, error)
+ Parse(reader io.Reader) (MigrationScript, MigrationScript, error)
+}
+
+type MigrationScript struct {
+ Contents string
+ Options ExecutionOptions
}
type ExecutionOptions struct {
UseTransaction bool
}
+const (
+ unknownSection = iota
+ upgradeScriptSection
+ downgradeScriptSection
+)
+
+const (
+ upgradeScriptDeliminator = "-- migrate:up"
+ downgradeScriptDeliminator = "-- migrate:down"
+ transactionOptionName = "transaction"
+)
+
type sqlParser struct{}
+// NewSqlParser creates a new SqlParser which can
+// parse out a migration script.
func NewSqlParser() SqlParser {
- return sqlParser{}
+ return &sqlParser{}
}
-func (sp sqlParser) Parse(reader io.Reader) (ExecutionOptions, error) {
- options := ExecutionOptions{UseTransaction: true}
+// 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)
- if scanner.Scan() {
- firstLine := strings.ToLower(strings.TrimSpace(scanner.Text()))
- if strings.HasPrefix(firstLine, "-- bolt:") {
- parts := strings.Split(firstLine, " ")
- // Skip the first two parts ("--" and "bolt:")
- for _, part := range parts[2:] {
- if part == "no-transaction" {
- options.UseTransaction = false
- break
- }
- }
+ currentSection := unknownSection
+ for scanner.Scan() {
+ line := strings.TrimSpace(scanner.Text())
+ loweredLine := strings.ToLower(line)
+
+ if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) {
+ currentSection = upgradeScriptSection
+ upgradeScript.Options = parseExecutionOptions(line)
+ } else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) {
+ currentSection = downgradeScriptSection
+ downgradeScript.Options = parseExecutionOptions(line)
+ } else if currentSection == upgradeScriptSection {
+ upgradeScript.Contents += line + "\n"
+ } else if currentSection == downgradeScriptSection {
+ downgradeScript.Contents += line + "\n"
}
}
if err := scanner.Err(); err != nil {
- return options, fmt.Errorf("parsing sql file encountered an error: %w", err)
+ return MigrationScript{}, MigrationScript{}, fmt.Errorf("parsing sql file encountered an error: %w", err)
+ }
+
+ return upgradeScript, downgradeScript, nil
+}
+
+// parseExecutionOptions extracts the execution options for a
+// upgrade or downgrade migration script.
+func parseExecutionOptions(line string) ExecutionOptions {
+ options := ExecutionOptions{UseTransaction: true}
+
+ parts := strings.Split(line, " ")
+ for _, part := range parts {
+ part = strings.ToLower(part)
+ if strings.Contains(part, "transaction:") {
+ option := strings.Split(part, ":")[1]
+ options.UseTransaction = option != "false"
+ }
}
- return options, nil
+ return options
}