85 lines
3.2 KiB
Go
85 lines
3.2 KiB
Go
package verdict
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// MigrationTools are the values --tool takes. The CI presets offer the same
|
|
// set (ci_presets.DB_MIGRATION_TOOLS; a test in apps/api compares the two).
|
|
var MigrationTools = map[string]bool{"liquibase": true, "flyway": true, "atlas": true, "none": true}
|
|
|
|
// CheckTool refuses a tool the module does not run, before anything runs.
|
|
// Until 2026-09-27 any other value — a typo such as "liquibse" — fell through
|
|
// to atlas and applied the directory with a tool nobody chose.
|
|
//
|
|
// It also refuses atlas for anything but PostgreSQL and MySQL: the module runs
|
|
// the Atlas Community Edition, which migrates those two
|
|
// (https://atlasgo.io/community-edition); SQL Server and Oracle keep their own
|
|
// message, as they are the ones asked for most.
|
|
func CheckTool(tool, dialect string) error {
|
|
if !MigrationTools[tool] {
|
|
return fmt.Errorf("--tool=%q is not one of liquibase, flyway, atlas, none", tool)
|
|
}
|
|
if tool == "atlas" && (dialect == "tsql" || dialect == "oracle") {
|
|
return fmt.Errorf("atlas community edition does not support dialect %q (SQL Server and Oracle are in its proprietary build only); use liquibase", dialect)
|
|
}
|
|
if tool == "atlas" && !atlasDialects[dialect] {
|
|
return fmt.Errorf("atlas community edition does not support %q; use liquibase or flyway", dialect)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// atlasDialects are what the Atlas Community Edition migrates.
|
|
var atlasDialects = map[string]bool{"postgres": true, "mysql": true}
|
|
|
|
// DefaultDialect is what a local tool (atlas's dev database) runs when
|
|
// neither --engine nor --dialect names one. --dialect itself has no default:
|
|
// unset, it is not sent, and the platform judges the SQL in the engines of the
|
|
// service's own databases (with --service-id) — a default "postgres" sent on
|
|
// every call made that impossible.
|
|
const DefaultDialect = "postgres"
|
|
|
|
// Target is what the platform is asked to judge the SQL as: the database
|
|
// ENGINE when one is named — the platform then judges it in that engine's own
|
|
// dialect, and one it has no rules for (MongoDB, Snowflake, ...) comes back
|
|
// unjudged, needing an approver (GET /db-engines) — otherwise the SQL
|
|
// dialect. Never both: the endpoint refuses a request naming both.
|
|
type Target struct {
|
|
Engine string
|
|
Dialect string
|
|
}
|
|
|
|
// ResolveTarget reads --engine and --dialect. Naming both is refused rather
|
|
// than one of the two silently ignored.
|
|
func ResolveTarget(engine, dialect string) (Target, error) {
|
|
engine = strings.ToLower(strings.TrimSpace(engine))
|
|
dialect = strings.TrimSpace(dialect)
|
|
if engine == "" {
|
|
return Target{Dialect: dialect}, nil
|
|
}
|
|
if dialect != "" {
|
|
return Target{}, fmt.Errorf("give --engine or --dialect, not both (--engine=%q, --dialect=%q)", engine, dialect)
|
|
}
|
|
return Target{Engine: engine}, nil
|
|
}
|
|
|
|
// ToolDialect is the dialect a migration TOOL works in on this runner (atlas
|
|
// replays the files on a dev database of it). Only atlas reads it, and atlas
|
|
// knows PostgreSQL and MySQL; any other engine is returned as it is, which
|
|
// CheckTool refuses for atlas.
|
|
func (t Target) ToolDialect() string {
|
|
switch t.Engine {
|
|
case "":
|
|
if t.Dialect == "" {
|
|
return DefaultDialect
|
|
}
|
|
return t.Dialect
|
|
case "postgres", "postgresql":
|
|
return "postgres"
|
|
case "mysql", "mariadb":
|
|
return "mysql"
|
|
}
|
|
return t.Engine
|
|
}
|