Files

44 lines
2.1 KiB
Go

package verdict
import (
"fmt"
"regexp"
)
// liquibaseRunsUnjudged matches a formatted-SQL directive line that makes
// Liquibase run, or skip, text the engine read as a comment:
//
// - precondition(s)/precondition-sql-check: runs the query in the comment
// (measured 2026-09-27 on 4.31.1: `--precondition-sql-check expectedResult:0
// WITH d AS (DELETE FROM users RETURNING 1) SELECT count(*) FROM d` is
// judged "additive" — the engine sees a comment — and Liquibase executes it);
// - include/includeAll: pull in files nobody sent to the engine;
// - property: defines a ${…} value substituted after judging;
// - validCheckSum: accepts an edited changeset without re-running the check;
// - ignoreLines: skips judged lines, so what runs is not what was judged.
//
// The platform refuses the same set, and more, for changesets it renders itself
// (apps/api/app/platform/services/db_apply_service.py, _LIQUIBASE_DIRECTIVE_LINE).
// A tenant's own file keeps the lines a formatted-SQL changelog needs:
// `--liquibase formatted sql`, `--changeset author:id [attributes]`,
// `--rollback`, `--comment`.
var liquibaseRunsUnjudged = regexp.MustCompile(
`(?i)^\s*--\s*(?:preconditions?|precondition-[a-z-]+|include|includeall|property|validchecksum|ignorelines)\b`)
// lineEnds are line ends as Liquibase's reader sees them (Java readLine): a
// lone \r ends a line too, so "SELECT 1;\r--precondition-sql-check …" is two
// lines to Liquibase and must be two lines here.
var lineEnds = regexp.MustCompile(`\r\n|\r|\n`)
// LiquibaseJudgeable refuses a formatted-SQL file holding a directive through
// which Liquibase would run (or skip) text the engine did not judge.
func LiquibaseJudgeable(file, sql string) error {
for i, line := range lineEnds.Split(sql, -1) {
if liquibaseRunsUnjudged.MatchString(line) {
return fmt.Errorf("%s line %d is a Liquibase directive (%q) that runs or skips SQL the engine read as a comment; "+
"the engine cannot judge it, so it is refused — write the check as its own changeset", file, i+1, line)
}
}
return nil
}