migrations_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package migrations
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. )
  6. const inputGoose = `
  7. -- +goose Up
  8. ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
  9. -- +goose Down
  10. ALTER TABLE archived_jobs DROP COLUMN expires_at;
  11. `
  12. const outputGoose = `
  13. -- +goose Up
  14. ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
  15. `
  16. const inputMigrate = `
  17. -- +migrate Up
  18. -- SQL in section 'Up' is executed when this migration is applied
  19. CREATE TABLE people (id int);
  20. -- +migrate Down
  21. -- SQL section 'Down' is executed when this migration is rolled back
  22. DROP TABLE people;
  23. `
  24. const outputMigrate = `
  25. -- +migrate Up
  26. -- SQL in section 'Up' is executed when this migration is applied
  27. CREATE TABLE people (id int);
  28. `
  29. const inputTern = `
  30. -- Write your migrate up statements here
  31. ALTER TABLE todo RENAME COLUMN done TO is_done;
  32. ---- create above / drop below ----
  33. ALTER TABLE todo RENAME COLUMN is_done TO done;
  34. `
  35. const outputTern = `
  36. -- Write your migrate up statements here
  37. ALTER TABLE todo RENAME COLUMN done TO is_done;`
  38. const inputDbmate = `
  39. -- migrate:up
  40. CREATE TABLE foo (bar int);
  41. -- migrate:down
  42. DROP TABLE foo;`
  43. const outputDbmate = `
  44. -- migrate:up
  45. CREATE TABLE foo (bar int);`
  46. func TestRemoveRollback(t *testing.T) {
  47. if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
  48. t.Errorf("goose migration mismatch:\n%s", diff)
  49. }
  50. if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
  51. t.Errorf("sql-migrate migration mismatch:\n%s", diff)
  52. }
  53. if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
  54. t.Errorf("tern migration mismatch:\n%s", diff)
  55. }
  56. if diff := cmp.Diff(outputDbmate, RemoveRollbackStatements(inputDbmate)); diff != "" {
  57. t.Errorf("dbmate migration mismatch:\n%s", diff)
  58. }
  59. }
  60. func TestRemoveGolangMigrateRollback(t *testing.T) {
  61. filenames := map[string]bool{
  62. // make sure we let through golang-migrate files that aren't rollbacks
  63. "migrations/1.up.sql": false,
  64. // make sure we let through other sql files
  65. "migrations/2.sql": false,
  66. "migrations/foo.sql": false,
  67. "migrations/1.down.sql": true,
  68. }
  69. for filename, want := range filenames {
  70. got := IsDown(filename)
  71. if diff := cmp.Diff(want, got); diff != "" {
  72. t.Errorf("IsDown mismatch: %s\n %s", filename, diff)
  73. }
  74. }
  75. }