1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package sqltool
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"time"
"unicode"
"ariga.io/atlas/sql/migrate"
)
var (
// GolangMigrateFormatter returns migrate.Formatter compatible with golang-migrate/migrate.
GolangMigrateFormatter = templateFormatter(
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.up.sql",
`{{ range .Changes }}{{ with .Comment }}-- {{ println . }}{{ end }}{{ printf "%s;\n" .Cmd }}{{ end }}`,
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.down.sql",
`{{ range rev .Changes }}{{ if .Reverse }}{{ with .Comment }}-- reverse: {{ println . }}{{ end }}{{ printf "%s;\n" .Reverse }}{{ end }}{{ end }}`,
)
// GooseFormatter returns migrate.Formatter compatible with pressly/goose.
GooseFormatter = templateFormatter(
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.sql",
`-- +goose Up
{{ range .Changes }}{{ with .Comment }}-- {{ println . }}{{ end }}{{ printf "%s;\n" .Cmd }}{{ end }}
-- +goose Down
{{ range rev .Changes }}{{ if .Reverse }}{{ with .Comment }}-- reverse: {{ println . }}{{ end }}{{ printf "%s;\n" .Reverse }}{{ end }}{{ end }}`,
)
// FlywayFormatter returns migrate.Formatter compatible with Flyway.
FlywayFormatter = templateFormatter(
"V{{ now }}{{ with .Name }}__{{ . }}{{ end }}.sql",
`{{ range .Changes }}{{ with .Comment }}-- {{ println . }}{{ end }}{{ printf "%s;\n" .Cmd }}{{ end }}`,
"U{{ now }}{{ with .Name }}__{{ . }}{{ end }}.sql",
`{{ range rev .Changes }}{{ if .Reverse }}{{ with .Comment }}-- reverse: {{ println . }}{{ end }}{{ printf "%s;\n" .Reverse }}{{ end }}{{ end }}`,
)
// LiquibaseFormatter returns migrate.Formatter compatible with Liquibase.
LiquibaseFormatter = templateFormatter(
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.sql",
`{{- $now := now -}}
--liquibase formatted sql
{{- range $index, $change := .Changes }}
--changeset atlas:{{ $now }}-{{ inc $index }}
{{ with $change.Comment }}--comment: {{ . }}{{ end }}
{{ $change.Cmd }};
{{ with $change.Reverse }}--rollback: {{ . }};{{ end }}
{{ end }}`,
)
// DBMateFormatter returns migrate.Formatter compatible with amacneil/dbmate.
DBMateFormatter = templateFormatter(
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.sql",
`-- migrate:up
{{ range .Changes }}{{ with .Comment }}-- {{ println . }}{{ end }}{{ printf "%s;\n" .Cmd }}{{ end }}
-- migrate:down
{{ range rev .Changes }}{{ if .Reverse }}{{ with .Comment }}-- reverse: {{ println . }}{{ end }}{{ printf "%s;\n" .Reverse }}{{ end }}{{ end }}`,
)
// DbmateFormatter is the same as DBMateFormatter.
// Deprecated: Use DBMateFormatter instead.
DbmateFormatter = DBMateFormatter
)
type (
// GolangMigrateDir wraps migrate.LocalDir and provides a migrate.Scanner implementation able to understand files
// generated by the GolangMigrateFormatter for migration directory replaying.
GolangMigrateDir struct{ *migrate.LocalDir }
// GolangMigrateFile wraps migrate.LocalFile with custom description function.
GolangMigrateFile struct{ *migrate.LocalFile }
)
// NewGolangMigrateDir returns a new GolangMigrateDir.
func NewGolangMigrateDir(path string) (*GolangMigrateDir, error) {
dir, err := migrate.NewLocalDir(path)
if err != nil {
return nil, err
}
return &GolangMigrateDir{dir}, nil
}
// Files implements Scanner.Files. It looks for all files with up.sql suffix and orders them by filename.
func (d *GolangMigrateDir) Files() ([]migrate.File, error) {
names, err := fs.Glob(d, "*.up.sql")
if err != nil {
return nil, err
}
// Sort files lexicographically.
sort.Slice(names, func(i, j int) bool {
return names[i] < names[j]
})
ret := make([]migrate.File, len(names))
for i, n := range names {
b, err := fs.ReadFile(d, n)
if err != nil {
return nil, fmt.Errorf("sql/migrate: read file %q: %w", n, err)
}
ret[i] = &GolangMigrateFile{LocalFile: migrate.NewLocalFile(n, b)}
}
return ret, nil
}
// Desc implements File.Desc.
func (f *GolangMigrateFile) Desc() string {
return strings.TrimSuffix(f.LocalFile.Desc(), ".up")
}
type (
// GooseDir wraps migrate.LocalDir and provides a migrate.Scanner implementation able to understand files
// generated by the GooseFormatter for migration directory replaying.
GooseDir struct{ *migrate.LocalDir }
// GooseFile wraps migrate.LocalFile with custom statements function.
GooseFile struct{ *migrate.LocalFile }
)
// NewGooseDir returns a new GooseDir.
func NewGooseDir(path string) (*GooseDir, error) {
dir, err := migrate.NewLocalDir(path)
if err != nil {
return nil, err
}
return &GooseDir{dir}, nil
}
// Files looks for all files with .sql suffix and orders them by filename.
func (d *GooseDir) Files() ([]migrate.File, error) {
files, err := d.LocalDir.Files()
if err != nil {
return nil, err
}
for i, f := range files {
files[i] = &GooseFile{f.(*migrate.LocalFile)}
}
return files, nil
}
// StmtDecls understands the migration format used by pressly/goose sql migration files.
func (f *GooseFile) StmtDecls() ([]*migrate.Stmt, error) {
// Atlas custom delimiter is per file, goose has pragma do mark start and end of a delimiter.
// In order to use the Atlas lexer, we define a custom delimiter for the source SQL and edit it to use the
// custom delimiter.
const delim = "-- ATLAS_DELIM_END"
var (
state, lineCount int
lines = []string{"-- atlas:delimiter " + delim, ""}
sc = bufio.NewScanner(bytes.NewReader(f.Bytes()))
)
Scan:
for sc.Scan() {
lineCount++
line := sc.Text()
// Handle goose custom delimiters.
if strings.HasPrefix(line, goosePragma) {
switch strings.TrimSpace(strings.TrimPrefix(line, goosePragma)) {
case "Up":
switch state {
case none: // found the "up" part of the file
state = up
default:
return nil, unexpectedPragmaErr(f, lineCount, "Up")
}
case "Down":
switch state {
case up: // found the "down" part
break Scan
default:
return nil, unexpectedPragmaErr(f, lineCount, "Down")
}
case "StatementBegin":
switch state {
case up:
state = begin // begin of a statement
default:
return nil, unexpectedPragmaErr(f, lineCount, "StatementBegin")
}
case "StatementEnd":
switch state {
case begin:
state = end // end of a statement
default:
return nil, unexpectedPragmaErr(f, lineCount, "StatementEnd")
}
}
}
// Write the line of the statement.
if !reGoosePragma.MatchString(line) && state != end {
// end of statement if line ends with semicolon
line = strings.TrimRightFunc(line, unicode.IsSpace)
lines = append(lines, line)
if state == up && strings.HasSuffix(line, ";") && !strings.HasPrefix(line, "--") {
lines = append(lines, delim)
}
}
if state == end {
state = up
lines = append(lines, delim)
}
}
return migrate.Stmts(strings.Join(lines, "\n"))
}
// Stmts understands the migration format used by pressly/goose sql migration files.
func (f *GooseFile) Stmts() ([]string, error) {
s, err := f.StmtDecls()
if err != nil {
return nil, err
}
stmts := make([]string, len(s))
for i := range s {
stmts[i] = s[i].Text
}
return stmts, nil
}
type (
// DBMateDir wraps migrate.LocalDir and provides a migrate.Scanner implementation able to understand files
// generated by the DBMateFormatter for migration directory replaying.
DBMateDir struct{ *migrate.LocalDir }
// DBMateFile wraps migrate.LocalFile with custom statements function.
DBMateFile struct{ *migrate.LocalFile }
)
// NewDBMateDir returns a new DBMateDir.
func NewDBMateDir(path string) (*DBMateDir, error) {
dir, err := migrate.NewLocalDir(path)
if err != nil {
return nil, err
}
return &DBMateDir{dir}, nil
}
// Files looks for all files with up.sql suffix and orders them by filename.
func (d *DBMateDir) Files() ([]migrate.File, error) {
files, err := d.LocalDir.Files()
if err != nil {
return nil, err
}
for i, f := range files {
files[i] = &DBMateFile{f.(*migrate.LocalFile)}
}
return files, nil
}
// StmtDecls understands the migration format used by amacneil/dbmate sql migration files.
func (f *DBMateFile) StmtDecls() ([]*migrate.Stmt, error) {
var (
state, lineCount int
lines []string
sc = bufio.NewScanner(bytes.NewReader(f.Bytes()))
)
Scan:
for sc.Scan() {
lineCount++
line := sc.Text()
// Handle pragmas.
if strings.HasPrefix(line, dbmatePragma) {
switch strings.TrimSpace(strings.TrimPrefix(line, dbmatePragma)) {
case "up":
state = up
case "down":
break Scan
}
}
// Write the line of the statement.
if !reDBMatePragma.MatchString(line) && state == up {
lines = append(lines, line)
}
}
return migrate.Stmts(strings.Join(lines, "\n"))
}
// Stmts understands the migration format used by amacneil/dbmate sql migration files.
func (f *DBMateFile) Stmts() ([]string, error) {
s, err := f.StmtDecls()
if err != nil {
return nil, err
}
stmts := make([]string, len(s))
for i := range s {
stmts[i] = s[i].Text
}
return stmts, nil
}
type (
// FlywayDir wraps migrate.LocalDir and provides a migrate.Scanner implementation able to understand files
// generated by the FlywayFormatter for migration directory replaying.
FlywayDir struct{ *migrate.LocalDir }
// FlywayFile wraps migrate.LocalFile with custom statements function.
FlywayFile struct{ *migrate.LocalFile }
)
// NewFlywayDir returns a new FlywayDir.
func NewFlywayDir(path string) (*FlywayDir, error) {
dir, err := migrate.NewLocalDir(path)
if err != nil {
return nil, err
}
return &FlywayDir{dir}, nil
}
// Files implements Scanner.Files. It looks for all files with .sql suffix. The given directory is recursively scanned
// for non-hidden subdirectories. All found files will be ordered by migration type (Baseline, Versioned, Repeatable)
// and filename.
func (d *FlywayDir) Files() ([]migrate.File, error) {
var ff flywayFiles
if err := fs.WalkDir(d, "", func(path string, e fs.DirEntry, err error) error {
if err != nil {
return err
}
if path != "" && e.IsDir() {
h, err := hidden(filepath.Join(d.Path(), path))
if err != nil {
return err
}
if h {
return fs.SkipDir
}
return nil
}
var (
pfx = e.Name()[0]
base = filepath.Base(e.Name())
ext = filepath.Ext(e.Name())
)
if ext != ".sql" || len(base) < 4 || (pfx != 'V' && pfx != 'B' && pfx != 'R') {
return nil
}
return ff.add(path)
}); err != nil {
return nil, err
}
var (
names = ff.names()
ret = make([]migrate.File, len(names))
)
for i, n := range names {
b, err := fs.ReadFile(d, n)
if err != nil {
return nil, fmt.Errorf("sql/migrate: read file %q: %w", n, err)
}
ret[i] = &FlywayFile{migrate.NewLocalFile(n, b)}
}
return ret, nil
}
// Desc implements File.Desc.
func (f FlywayFile) Desc() string {
return flywayDesc(f.Name())
}
// Version implements File.Version.
func (f FlywayFile) Version() string {
return flywayVersion(f.Name())
}
// SetRepeatableVersion iterates over the migration files and assigns repeatable migrations a version number since
// Atlas does not have the concept of repeatable migrations. Each repeatable migration file gets assigned the version
// of the preceding migration file (or 0) followed by an 'R'.
func SetRepeatableVersion(ff []migrate.File) {
// First find the index of the first repeatable migration file (if any).
var (
v string // last versioned migration version
idx = func() int {
for i, f := range ff {
if f.Version() == "" {
return i
}
}
return -1
}()
)
switch idx {
case -1:
// No repeatable migration does exist.
return
case 0:
// There is no preceding migration. Use Version "0".
v = "0"
default:
v = ff[idx-1].Version()
}
if v != "" {
// Every migration file following the first repeatable found are repeatable as well.
for i, f := range ff[idx:] {
ff[idx+i] = &FlywayFile{migrate.NewLocalFile(
fmt.Sprintf("V%sR__%s", v, f.Desc()),
f.Bytes(),
)}
}
}
}
// LiquibaseDir wraps migrate.LocalDir and provides a migrate.Scanner implementation able to understand files
// generated by the LiquibaseFormatter for migration directory replaying.
type LiquibaseDir struct{ *migrate.LocalDir }
// NewLiquibaseDir returns a new LiquibaseDir.
func NewLiquibaseDir(path string) (*LiquibaseDir, error) {
d, err := migrate.NewLocalDir(path)
if err != nil {
return nil, err
}
return &LiquibaseDir{d}, nil
}
const (
none int = iota
up
begin
end
goosePragma = "-- +goose"
dbmatePragma = "-- migrate:"
)
var (
reGoosePragma = regexp.MustCompile(regexp.QuoteMeta(goosePragma) + " Up|Down|StatementBegin|StatementEnd")
reDBMatePragma = regexp.MustCompile(dbmatePragma + "up|down")
)
// flywayFiles retrieves flyway migration files by calls to add(). It will only keep the latest baseline and ignore
// all versioned files that are included in that baseline.
type flywayFiles struct {
baseline string
versioned []string
repeatable []string
}
// add the given path to the migration files according to its type. The input directory is assumed to be valid
// according to the Flyway documentation (no duplicate versions, etc.).
func (ff *flywayFiles) add(path string) error {
switch p := filepath.Base(path)[0]; p {
case 'B':
if ff.baseline != "" && flywayVersion(path) < flywayVersion(ff.baseline) {
return nil
}
ff.baseline = path
// In case we set a new baseline, remove all versioned files with a version smaller than the new baseline.
var (
bv = flywayVersion(ff.baseline)
vs []string
)
for _, v := range ff.versioned {
if v > bv {
vs = append(vs, v)
}
}
ff.versioned = vs
return nil
case 'V':
v := flywayVersion(path)
if ff.baseline == "" || flywayVersion(ff.baseline) < v {
ff.versioned = append(ff.versioned, path)
}
return nil
case 'R':
ff.repeatable = append(ff.repeatable, path)
return nil
default:
return fmt.Errorf("sql/sqltool: unexpected Flyway prefix %q", p)
}
}
func (ff *flywayFiles) names() []string {
var names []string
if ff.baseline != "" {
names = append(names, ff.baseline)
}
sort.Strings(ff.versioned)
sort.Strings(ff.repeatable)
names = append(names, ff.versioned...)
names = append(names, ff.repeatable...)
return names
}
func flywayDesc(path string) string {
parts := strings.SplitN(path, "__", 2)
if len(parts) == 1 {
return ""
}
return strings.TrimSuffix(parts[1], ".sql")
}
func flywayVersion(path string) string {
// Repeatable migrations don't have a version.
if filepath.Base(path)[0] == 'R' {
return ""
}
return strings.SplitN(strings.TrimSuffix(filepath.Base(path), ".sql"), "__", 2)[0][1:]
}
func unexpectedPragmaErr(f migrate.File, line int, pragma string) error {
var tool string
switch f := f.(type) {
case *GooseFile:
tool = "goose"
case *DBMateFile:
tool = "dbmate"
default:
return fmt.Errorf("sql/migrate: unexpected migration file type '%T'", f)
}
return fmt.Errorf(
"sql/migrate: %s: %s:%d unexpected goosePragma '%s'",
tool, f.Name(), line, pragma,
)
}
// funcs contains the template.FuncMap for the different formatters.
var funcs = template.FuncMap{
"inc": func(x int) int { return x + 1 },
// now formats the current time in a lexicographically ascending order while maintaining human readability.
"now": func() string { return time.Now().UTC().Format("20060102150405") },
"rev": reverse,
}
// templateFormatter parses the given templates and passes them on to the migrate.NewTemplateFormatter.
func templateFormatter(templates ...string) migrate.Formatter {
tpls := make([]*template.Template, len(templates))
for i, t := range templates {
tpls[i] = template.Must(template.New("").Funcs(funcs).Parse(t))
}
tf, err := migrate.NewTemplateFormatter(tpls...)
if err != nil {
panic(err)
}
return tf
}
// reverse changes for the down migration.
func reverse(changes []*migrate.Change) []*migrate.Change {
n := len(changes)
rev := make([]*migrate.Change, n)
if n%2 == 1 {
rev[n/2] = changes[n/2]
}
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
rev[i], rev[j] = changes[j], changes[i]
}
return rev
}
|