Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make goose annotations case-insensitive #704

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 79 additions & 11 deletions internal/sqlparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
if stateMachine.get() == start && strings.TrimSpace(line) == "" {
continue
}
// TODO(mf): validate annotations to avoid common user errors:
// https://github.com/pressly/goose/issues/163#issuecomment-501736725
if strings.HasPrefix(line, "--") {
cmd := strings.TrimSpace(strings.TrimPrefix(line, "--"))

// Check for annotations.
// All annotations must be in format: "-- +goose [annotation]"
if strings.HasPrefix(strings.TrimSpace(line), "--") && strings.Contains(line, "+goose") {
var cmd annotation

cmd, err = extractAnnotation(line)
if err != nil {
return nil, false, fmt.Errorf("failed to parse annotation line %q: %w", line, err)
}

switch cmd {
case "+goose Up":
case annotationUp:
switch stateMachine.get() {
case start:
stateMachine.set(gooseUp)
Expand All @@ -136,7 +142,7 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
}
continue

case "+goose Down":
case annotationDown:
switch stateMachine.get() {
case gooseUp, gooseStatementEndUp:
// If we hit a down annotation, but the buffer is not empty, we have an unfinished SQL query from a
Expand All @@ -151,7 +157,7 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
}
continue

case "+goose StatementBegin":
case annotationStatementBegin:
switch stateMachine.get() {
case gooseUp, gooseStatementEndUp:
stateMachine.set(gooseStatementBeginUp)
Expand All @@ -162,7 +168,7 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
}
continue

case "+goose StatementEnd":
case annotationStatementEnd:
switch stateMachine.get() {
case gooseStatementBeginUp:
stateMachine.set(gooseStatementEndUp)
Expand All @@ -172,17 +178,20 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
return nil, false, errors.New("'-- +goose StatementEnd' must be defined after '-- +goose StatementBegin', see https://github.com/pressly/goose#sql-migrations")
}

case "+goose NO TRANSACTION":
case annotationNoTransaction:
useTx = false
continue

case "+goose ENVSUB ON":
case annotationEnvsubOn:
useEnvsub = true
continue

case "+goose ENVSUB OFF":
case annotationEnvsubOff:
useEnvsub = false
continue

default:
return nil, false, fmt.Errorf("unknown annotation: %q", cmd)
}
}
// Once we've started parsing a statement the buffer is no longer empty,
Expand Down Expand Up @@ -277,6 +286,65 @@ func ParseSQLMigration(r io.Reader, direction Direction, debug bool) (stmts []st
return stmts, useTx, nil
}

type annotation string

const (
annotationUp annotation = "Up"
annotationDown annotation = "Down"
annotationStatementBegin annotation = "StatementBegin"
annotationStatementEnd annotation = "StatementEnd"
annotationNoTransaction annotation = "NO TRANSACTION"
annotationEnvsubOn annotation = "ENVSUB ON"
annotationEnvsubOff annotation = "ENVSUB OFF"
)

var supportedAnnotations = map[annotation]struct{}{
annotationUp: {},
annotationDown: {},
annotationStatementBegin: {},
annotationStatementEnd: {},
annotationNoTransaction: {},
annotationEnvsubOn: {},
annotationEnvsubOff: {},
}

var (
errEmptyAnnotation = errors.New("empty annotation")
errInvalidAnnotation = errors.New("invalid annotation")
)

// extractAnnotation extracts the annotation from the line.
// All annotations must be in format: "-- +goose [annotation]"
// Allowed annotations: Up, Down, StatementBegin, StatementEnd, NO TRANSACTION, ENVSUB ON, ENVSUB OFF
func extractAnnotation(line string) (annotation, error) {
// Remove leading and trailing whitespace from line.
cmd := strings.TrimSpace(line)
Copy link
Collaborator

@mfridman mfridman Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been going back and forth on whether we should do this.

I.e., a goose annotation must begin with -- +goose, with no leading white space.

If we do, we'll need to update the blog (I want to move some of this "blog" content into formal docs)

https://pressly.github.io/goose/blog/2022/overview-sql-file/#quick-start

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove this line and add a check if the line has a leading space and throw an appropriate error.

Copy link
Collaborator

@mfridman mfridman Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably the right thing:

  1. Only allow -- +goose <annotation> without leading space
  2. Raise an error if we detect a -- +goose annotation with leading whitespace like \s+-- \+goose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add changes to cover this logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfridman I refactored PR to allow annotations only without leading whitespace

// Extract the annotation from the line, by removing the leading "--"
cmd = strings.ReplaceAll(cmd, "--", "")
// Remove leading and trailing whitespace from the annotation command.
cmd = strings.TrimSpace(cmd)

// Extract the annotation from the line, by removing the leading "+goose"
cmd = strings.TrimPrefix(cmd, "+goose")

// Remove leading and trailing whitespace from the annotation command.
cmd = strings.TrimSpace(cmd)

if cmd == "" {
return "", errEmptyAnnotation
}

a := annotation(cmd)

for s := range supportedAnnotations {
if strings.EqualFold(string(s), string(a)) {
return s, nil
}
}

return "", fmt.Errorf("%q: %w", cmd, errInvalidAnnotation)
}

func missingSemicolonError(state parserState, direction Direction, s string) error {
return fmt.Errorf("failed to parse migration: state %d, direction: %v: unexpected unfinished SQL query: %q: missing semicolon?",
state,
Expand Down
61 changes: 61 additions & 0 deletions internal/sqlparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,64 @@ CREATE TABLE post (
check.HasError(t, err)
check.Contains(t, err.Error(), "variable substitution failed: $SOME_UNSET_VAR: required env var not set:")
}

func Test_extractAnnotation(t *testing.T) {
tests := []struct {
name string
input string
want annotation
wantErr func(t *testing.T, err error)
}{
{
name: "Up",
input: "-- +goose Up",
want: annotationUp,
wantErr: check.NoError,
},
{
name: "Down",
input: "-- +goose Down",
want: annotationDown,
wantErr: check.NoError,
},
{
name: "StmtBegin",
input: "-- +goose StatementBegin",
want: annotationStatementBegin,
wantErr: check.NoError,
},
{
name: "NoTransact",
input: "-- +goose NO TRANSACTION",
want: annotationNoTransaction,
wantErr: check.NoError,
},
{
name: "Unsupported",
input: "-- +goose unsupported",
want: "",
wantErr: check.HasError,
},
{
name: "Empty",
input: "-- +goose",
want: "",
wantErr: check.HasError,
},
{
name: "statement with spaces and Uppercase",
input: "-- +goose UP ",
want: annotationUp,
wantErr: check.NoError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractAnnotation(tt.input)
tt.wantErr(t, err)

check.Equal(t, got, tt.want)
})
}
}
4 changes: 2 additions & 2 deletions internal/sqlparser/testdata/valid-up/test01/input.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- +goose Up
-- +goose UP
CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
);

-- +goose StatementBegin
-- +goose statementBegin
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
Expand Down
Loading