aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse
diff options
context:
space:
mode:
Diffstat (limited to 'internal/sqlparse')
-rw-r--r--internal/sqlparse/sqlparse.go78
-rw-r--r--internal/sqlparse/sqlparse_test.go175
2 files changed, 215 insertions, 38 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
}
diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go
index 12a435d..b5d2e93 100644
--- a/internal/sqlparse/sqlparse_test.go
+++ b/internal/sqlparse/sqlparse_test.go
@@ -9,50 +9,183 @@ import (
"github.com/eugenetriguba/bolt/internal/sqlparse"
"github.com/eugenetriguba/checkmate/assert"
+ "github.com/eugenetriguba/checkmate/check"
)
func TestSqlParser_Parse(t *testing.T) {
testCases := []struct {
- buffer string
- expectedExecutionOptions sqlparse.ExecutionOptions
+ migration string
+ expectedUpgradeScript sqlparse.MigrationScript
+ expectedDowngradeScript sqlparse.MigrationScript
}{
{
- buffer: "-- bolt: no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
+ migration: "",
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
},
{
- buffer: "-- BOLT: no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
+ migration: `
+ -- migrate:up
+ CREATE TABLE users(id int PRIMARY KEY);`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
},
{
- buffer: "\n-- bolt: no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
+ migration: `
+ -- migrate:up transaction:false
+ CREATE TABLE users(id int PRIMARY KEY);`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
},
{
- buffer: "",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
+ migration: `
+ -- migrate:up transaction:true
+ CREATE TABLE users(id int PRIMARY KEY);`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
},
{
- buffer: "--bolt: no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
+ migration: `
+ -- transaction:true migrate:up
+ CREATE TABLE users(id int PRIMARY KEY);`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
},
{
- buffer: "-- bolt : no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true},
+ migration: `
+ -- migrate:down
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
},
{
- buffer: "-- bolt: no-transaction no-transaction",
- expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false},
+ migration: `
+ -- migrate:down transaction:false
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ },
+ {
+ migration: `
+ -- migrate:down transaction:true
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ },
+ {
+ migration: `
+ -- transaction:true migrate:down
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "",
+ Options: sqlparse.ExecutionOptions{},
+ },
+ },
+ {
+ migration: `
+ -- migrate:up
+ CREATE TABLE users(id int PRIMARY KEY);
+ -- migrate:down
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ },
+ {
+ migration: `
+ -- migrate:up transaction:false
+ CREATE TABLE users(id int PRIMARY KEY);
+ -- migrate:down transaction:false
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ },
+ {
+ migration: `
+ -- MIGRATE:UP TRANSACTION:FALSE
+ CREATE TABLE users(id int PRIMARY KEY);
+ -- MIGRATE:DOWN TRANSACTION:FALSE
+ DROP TABLE users;`,
+ expectedUpgradeScript: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE users(id int PRIMARY KEY);\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ expectedDowngradeScript: sqlparse.MigrationScript{
+ Contents: "DROP TABLE users;\n",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
},
}
for _, tc := range testCases {
sqlParser := sqlparse.NewSqlParser()
-
- execOptions, err := sqlParser.Parse(strings.NewReader(tc.buffer))
+ upgradeScript, downgradeScript, err := sqlParser.Parse(strings.NewReader(tc.migration))
assert.Nil(t, err)
-
- assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions)
+ check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript)
+ check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript)
}
}
@@ -80,7 +213,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")
}