Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

-- +goose Up

-- +goose StatementBegin
CREATE OR REPLACE FUNCTION shithub_deployment_pattern_matches(pattern text, ref_name text)
RETURNS boolean
LANGUAGE plpgsql
Expand Down Expand Up @@ -38,6 +39,7 @@ BEGIN
RETURN ref_name ~ regex;
END;
$$;
-- +goose StatementEnd

-- +goose Down

Expand Down
43 changes: 43 additions & 0 deletions internal/migrationsfs/migrationsfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,49 @@ func TestMigrationVersionsAreUnique(t *testing.T) {
}
}

func TestFunctionMigrationsUseGooseStatementBlocks(t *testing.T) {
t.Parallel()

entries, err := fs.ReadDir(FS(), ".")
if err != nil {
t.Fatalf("ReadDir migrations: %v", err)
}

var problems []string
for _, entry := range entries {
if entry.IsDir() || !isGooseMigrationName(entry.Name()) {
continue
}
body, err := fs.ReadFile(FS(), entry.Name())
if err != nil {
t.Fatalf("ReadFile %s: %v", entry.Name(), err)
}

inStatementBlock := false
for lineNo, line := range strings.Split(string(body), "\n") {
trimmed := strings.TrimSpace(line)
switch trimmed {
case "-- +goose StatementBegin":
inStatementBlock = true
case "-- +goose StatementEnd":
inStatementBlock = false
}

upper := strings.ToUpper(trimmed)
createsFunction := strings.HasPrefix(upper, "CREATE FUNCTION ") ||
strings.HasPrefix(upper, "CREATE OR REPLACE FUNCTION ") ||
strings.HasPrefix(upper, "DO $$")
if createsFunction && !inStatementBlock {
problems = append(problems, fmt.Sprintf("%s:%d creates a multi-statement SQL block without -- +goose StatementBegin", entry.Name(), lineNo+1))
}
}
}

if len(problems) > 0 {
t.Fatalf("goose SQL function blocks must be wrapped:\n%s", strings.Join(problems, "\n"))
}
}

func isGooseMigrationName(name string) bool {
if len(name) < len("0000_.sql") || !strings.HasSuffix(name, ".sql") || name[4] != '_' {
return false
Expand Down
Loading