aboutsummaryrefslogtreecommitdiffstats
path: root/internal/sqlparse/sqlparse.go
blob: fa9fa962bd01b55cc445a764a33044c24661da73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package sqlparse

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type SqlParser interface {
	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{}
}

// 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
	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 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
}