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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
package sqlparse
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
)
const (
sqlCmdPrefix = "-- +migrate "
optionNoTransaction = "notransaction"
)
type ParsedMigration struct {
UpStatements []string
DownStatements []string
DisableTransactionUp bool
DisableTransactionDown bool
}
var (
// LineSeparator can be used to split migrations by an exact line match. This line
// will be removed from the output. If left blank, it is not considered. It is defaulted
// to blank so you will have to set it manually.
// Use case: in MSSQL, it is convenient to separate commands by GO statements like in
// SQL Query Analyzer.
LineSeparator = ""
)
func errNoTerminator() error {
if len(LineSeparator) == 0 {
return errors.New(`ERROR: The last statement must be ended by a semicolon or '-- +migrate StatementEnd' marker.
See https://github.com/rubenv/sql-migrate for details.`)
}
return errors.New(fmt.Sprintf(`ERROR: The last statement must be ended by a semicolon, a line whose contents are %q, or '-- +migrate StatementEnd' marker.
See https://github.com/rubenv/sql-migrate for details.`, LineSeparator))
}
// Checks the line to see if the line has a statement-ending semicolon
// or if the line contains a double-dash comment.
func endsWithSemicolon(line string) bool {
prev := ""
scanner := bufio.NewScanner(strings.NewReader(line))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
if strings.HasPrefix(word, "--") {
break
}
prev = word
}
return strings.HasSuffix(prev, ";")
}
type migrationDirection int
const (
directionNone migrationDirection = iota
directionUp
directionDown
)
type migrateCommand struct {
Command string
Options []string
}
func (c *migrateCommand) HasOption(opt string) bool {
for _, specifiedOption := range c.Options {
if specifiedOption == opt {
return true
}
}
return false
}
func parseCommand(line string) (*migrateCommand, error) {
cmd := &migrateCommand{}
if !strings.HasPrefix(line, sqlCmdPrefix) {
return nil, errors.New("ERROR: not a sql-migrate command")
}
fields := strings.Fields(line[len(sqlCmdPrefix):])
if len(fields) == 0 {
return nil, errors.New(`ERROR: incomplete migration command`)
}
cmd.Command = fields[0]
cmd.Options = fields[1:]
return cmd, nil
}
// Split the given sql script into individual statements.
//
// The base case is to simply split on semicolons, as these
// naturally terminate a statement.
//
// However, more complex cases like pl/pgsql can have semicolons
// within a statement. For these cases, we provide the explicit annotations
// 'StatementBegin' and 'StatementEnd' to allow the script to
// tell us to ignore semicolons.
func ParseMigration(r io.ReadSeeker) (*ParsedMigration, error) {
p := &ParsedMigration{}
_, err := r.Seek(0, 0)
if err != nil {
return nil, err
}
var buf bytes.Buffer
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
statementEnded := false
ignoreSemicolons := false
currentDirection := directionNone
for scanner.Scan() {
line := scanner.Text()
// ignore comment except beginning with '-- +'
if strings.HasPrefix(line, "-- ") && !strings.HasPrefix(line, "-- +") {
continue
}
// handle any migrate-specific commands
if strings.HasPrefix(line, sqlCmdPrefix) {
cmd, err := parseCommand(line)
if err != nil {
return nil, err
}
switch cmd.Command {
case "Up":
if len(strings.TrimSpace(buf.String())) > 0 {
return nil, errNoTerminator()
}
currentDirection = directionUp
if cmd.HasOption(optionNoTransaction) {
p.DisableTransactionUp = true
}
break
case "Down":
if len(strings.TrimSpace(buf.String())) > 0 {
return nil, errNoTerminator()
}
currentDirection = directionDown
if cmd.HasOption(optionNoTransaction) {
p.DisableTransactionDown = true
}
break
case "StatementBegin":
if currentDirection != directionNone {
ignoreSemicolons = true
}
break
case "StatementEnd":
if currentDirection != directionNone {
statementEnded = (ignoreSemicolons == true)
ignoreSemicolons = false
}
break
}
}
if currentDirection == directionNone {
continue
}
isLineSeparator := !ignoreSemicolons && len(LineSeparator) > 0 && line == LineSeparator
if !isLineSeparator && !strings.HasPrefix(line, "-- +") {
if _, err := buf.WriteString(line + "\n"); err != nil {
return nil, err
}
}
// Wrap up the two supported cases: 1) basic with semicolon; 2) psql statement
// Lines that end with semicolon that are in a statement block
// do not conclude statement.
if (!ignoreSemicolons && (endsWithSemicolon(line) || isLineSeparator)) || statementEnded {
statementEnded = false
switch currentDirection {
case directionUp:
p.UpStatements = append(p.UpStatements, buf.String())
case directionDown:
p.DownStatements = append(p.DownStatements, buf.String())
default:
panic("impossible state")
}
buf.Reset()
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
// diagnose likely migration script errors
if ignoreSemicolons {
return nil, errors.New("ERROR: saw '-- +migrate StatementBegin' with no matching '-- +migrate StatementEnd'")
}
if currentDirection == directionNone {
return nil, errors.New(`ERROR: no Up/Down annotations found, so no statements were executed.
See https://github.com/rubenv/sql-migrate for details.`)
}
// allow comment without sql instruction. Example:
// -- +migrate Down
// -- nothing to downgrade!
if len(strings.TrimSpace(buf.String())) > 0 && !strings.HasPrefix(buf.String(), "-- +") {
return nil, errNoTerminator()
}
return p, nil
}
|