forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/rubenv/sql-migrate/sqlparse/sqlparse.go

352 lines
5.7 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package sqlparse
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)
const (
sqlCmdPrefix = "-- +migrate "
2024-02-18 10:42:21 +00:00
optionNoTransaction = "notransaction"
)
type ParsedMigration struct {
UpStatements []string
2024-02-18 10:42:21 +00:00
DownStatements []string
DisableTransactionUp bool
2024-02-18 10:42:21 +00:00
DisableTransactionDown bool
}
// LineSeparator can be used to split migrations by an exact line match. This line
2024-02-18 10:42:21 +00:00
// will be removed from the output. If left blank, it is not considered. It is defaulted
2024-02-18 10:42:21 +00:00
// to blank so you will have to set it manually.
2024-02-18 10:42:21 +00:00
// Use case: in MSSQL, it is convenient to separate commands by GO statements like in
2024-02-18 10:42:21 +00:00
// SQL Query Analyzer.
2024-02-18 10:42:21 +00:00
var LineSeparator = ""
func errNoTerminator() error {
2024-02-18 10:42:21 +00:00
if len(LineSeparator) == 0 {
2024-02-18 10:42:21 +00:00
return fmt.Errorf(`ERROR: The last statement must be ended by a semicolon or '-- +migrate StatementEnd' marker.
2024-02-18 10:42:21 +00:00
See https://github.com/rubenv/sql-migrate for details.`)
2024-02-18 10:42:21 +00:00
}
return fmt.Errorf(`ERROR: The last statement must be ended by a semicolon, a line whose contents are %q, or '-- +migrate StatementEnd' marker.
2024-02-18 10:42:21 +00:00
See https://github.com/rubenv/sql-migrate for details.`, LineSeparator)
2024-02-18 10:42:21 +00:00
}
// Checks the line to see if the line has a statement-ending semicolon
2024-02-18 10:42:21 +00:00
// or if the line contains a double-dash comment.
2024-02-18 10:42:21 +00:00
func endsWithSemicolon(line string) bool {
2024-02-18 10:42:21 +00:00
prev := ""
2024-02-18 10:42:21 +00:00
scanner := bufio.NewScanner(strings.NewReader(line))
2024-02-18 10:42:21 +00:00
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
2024-02-18 10:42:21 +00:00
word := scanner.Text()
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(word, "--") {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
prev = word
2024-02-18 10:42:21 +00:00
}
return strings.HasSuffix(prev, ";")
2024-02-18 10:42:21 +00:00
}
type migrationDirection int
const (
directionNone migrationDirection = iota
2024-02-18 10:42:21 +00:00
directionUp
2024-02-18 10:42:21 +00:00
directionDown
)
type migrateCommand struct {
Command string
2024-02-18 10:42:21 +00:00
Options []string
}
func (c *migrateCommand) HasOption(opt string) bool {
2024-02-18 10:42:21 +00:00
for _, specifiedOption := range c.Options {
2024-02-18 10:42:21 +00:00
if specifiedOption == opt {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return false
2024-02-18 10:42:21 +00:00
}
func parseCommand(line string) (*migrateCommand, error) {
2024-02-18 10:42:21 +00:00
cmd := &migrateCommand{}
if !strings.HasPrefix(line, sqlCmdPrefix) {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("ERROR: not a sql-migrate command")
2024-02-18 10:42:21 +00:00
}
fields := strings.Fields(line[len(sqlCmdPrefix):])
2024-02-18 10:42:21 +00:00
if len(fields) == 0 {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf(`ERROR: incomplete migration command`)
2024-02-18 10:42:21 +00:00
}
cmd.Command = fields[0]
cmd.Options = fields[1:]
return cmd, nil
2024-02-18 10:42:21 +00:00
}
// Split the given sql script into individual statements.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The base case is to simply split on semicolons, as these
2024-02-18 10:42:21 +00:00
// naturally terminate a statement.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// However, more complex cases like pl/pgsql can have semicolons
2024-02-18 10:42:21 +00:00
// within a statement. For these cases, we provide the explicit annotations
2024-02-18 10:42:21 +00:00
// 'StatementBegin' and 'StatementEnd' to allow the script to
2024-02-18 10:42:21 +00:00
// tell us to ignore semicolons.
2024-02-18 10:42:21 +00:00
func ParseMigration(r io.ReadSeeker) (*ParsedMigration, error) {
2024-02-18 10:42:21 +00:00
p := &ParsedMigration{}
_, err := r.Seek(0, 0)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
var buf bytes.Buffer
2024-02-18 10:42:21 +00:00
scanner := bufio.NewScanner(r)
2024-02-18 10:42:21 +00:00
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
statementEnded := false
2024-02-18 10:42:21 +00:00
ignoreSemicolons := false
2024-02-18 10:42:21 +00:00
currentDirection := directionNone
for scanner.Scan() {
2024-02-18 10:42:21 +00:00
line := scanner.Text()
2024-02-18 10:42:21 +00:00
// ignore comment except beginning with '-- +'
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(line, "-- ") && !strings.HasPrefix(line, "-- +") {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
// handle any migrate-specific commands
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(line, sqlCmdPrefix) {
2024-02-18 10:42:21 +00:00
cmd, err := parseCommand(line)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
switch cmd.Command {
2024-02-18 10:42:21 +00:00
case "Up":
2024-02-18 10:42:21 +00:00
if len(strings.TrimSpace(buf.String())) > 0 {
2024-02-18 10:42:21 +00:00
return nil, errNoTerminator()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
currentDirection = directionUp
2024-02-18 10:42:21 +00:00
if cmd.HasOption(optionNoTransaction) {
2024-02-18 10:42:21 +00:00
p.DisableTransactionUp = true
2024-02-18 10:42:21 +00:00
}
case "Down":
2024-02-18 10:42:21 +00:00
if len(strings.TrimSpace(buf.String())) > 0 {
2024-02-18 10:42:21 +00:00
return nil, errNoTerminator()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
currentDirection = directionDown
2024-02-18 10:42:21 +00:00
if cmd.HasOption(optionNoTransaction) {
2024-02-18 10:42:21 +00:00
p.DisableTransactionDown = true
2024-02-18 10:42:21 +00:00
}
case "StatementBegin":
2024-02-18 10:42:21 +00:00
if currentDirection != directionNone {
2024-02-18 10:42:21 +00:00
ignoreSemicolons = true
2024-02-18 10:42:21 +00:00
}
case "StatementEnd":
2024-02-18 10:42:21 +00:00
if currentDirection != directionNone {
2024-02-18 10:42:21 +00:00
statementEnded = ignoreSemicolons
2024-02-18 10:42:21 +00:00
ignoreSemicolons = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if currentDirection == directionNone {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
isLineSeparator := !ignoreSemicolons && len(LineSeparator) > 0 && line == LineSeparator
if !isLineSeparator && !strings.HasPrefix(line, "-- +") {
2024-02-18 10:42:21 +00:00
if _, err := buf.WriteString(line + "\n"); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Wrap up the two supported cases: 1) basic with semicolon; 2) psql statement
2024-02-18 10:42:21 +00:00
// Lines that end with semicolon that are in a statement block
2024-02-18 10:42:21 +00:00
// do not conclude statement.
2024-02-18 10:42:21 +00:00
if (!ignoreSemicolons && (endsWithSemicolon(line) || isLineSeparator)) || statementEnded {
2024-02-18 10:42:21 +00:00
statementEnded = false
2024-02-18 10:42:21 +00:00
switch currentDirection {
2024-02-18 10:42:21 +00:00
case directionUp:
2024-02-18 10:42:21 +00:00
p.UpStatements = append(p.UpStatements, buf.String())
case directionDown:
2024-02-18 10:42:21 +00:00
p.DownStatements = append(p.DownStatements, buf.String())
default:
2024-02-18 10:42:21 +00:00
panic("impossible state")
2024-02-18 10:42:21 +00:00
}
buf.Reset()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if err := scanner.Err(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// diagnose likely migration script errors
2024-02-18 10:42:21 +00:00
if ignoreSemicolons {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("ERROR: saw '-- +migrate StatementBegin' with no matching '-- +migrate StatementEnd'")
2024-02-18 10:42:21 +00:00
}
if currentDirection == directionNone {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf(`ERROR: no Up/Down annotations found, so no statements were executed.
2024-02-18 10:42:21 +00:00
See https://github.com/rubenv/sql-migrate for details.`)
2024-02-18 10:42:21 +00:00
}
// allow comment without sql instruction. Example:
2024-02-18 10:42:21 +00:00
// -- +migrate Down
2024-02-18 10:42:21 +00:00
// -- nothing to downgrade!
2024-02-18 10:42:21 +00:00
if len(strings.TrimSpace(buf.String())) > 0 && !strings.HasPrefix(buf.String(), "-- +") {
2024-02-18 10:42:21 +00:00
return nil, errNoTerminator()
2024-02-18 10:42:21 +00:00
}
return p, nil
2024-02-18 10:42:21 +00:00
}