|
| 1 | +package migrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "testing/fstest" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +var testCliFS = fstest.MapFS{ |
| 16 | + "1_create_users.up.sql": {Data: []byte("create table users (id int not null primary key)")}, |
| 17 | + "1_create_users.down.sql": {Data: []byte("drop table users")}, |
| 18 | +} |
| 19 | + |
| 20 | +func TestCli_Run(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + t.Run("up, version, down and drop", func(t *testing.T) { |
| 23 | + db := testDB(t, "exql_migrate_cli") |
| 24 | + var out bytes.Buffer |
| 25 | + cli := &Cli{DB: db, FS: testCliFS, Out: &out} |
| 26 | + |
| 27 | + assert.NoError(t, cli.Run(ctx, "up")) |
| 28 | + assert.Equal(t, "applying migration 1_create_users\n", out.String()) |
| 29 | + assert.Equal(t, []string{"schema_migrations", "users"}, tableNames(t, db)) |
| 30 | + |
| 31 | + out.Reset() |
| 32 | + assert.NoError(t, cli.Run(ctx, "version")) |
| 33 | + assert.Equal(t, "1\n", out.String()) |
| 34 | + |
| 35 | + assert.NoError(t, cli.Run(ctx, "down")) |
| 36 | + assert.Equal(t, []string{"schema_migrations"}, tableNames(t, db)) |
| 37 | + |
| 38 | + assert.NoError(t, cli.Run(ctx, "drop")) |
| 39 | + assert.Empty(t, tableNames(t, db)) |
| 40 | + }) |
| 41 | + t.Run("version reports dirty state", func(t *testing.T) { |
| 42 | + db := testDB(t, "exql_migrate_cli_dirty") |
| 43 | + broken := fstest.MapFS{ |
| 44 | + "1_broken.up.sql": {Data: []byte("alter table nonexistent add x int")}, |
| 45 | + } |
| 46 | + var out bytes.Buffer |
| 47 | + cli := &Cli{DB: db, FS: broken, Out: &out} |
| 48 | + assert.ErrorContains(t, cli.Run(ctx, "up"), "migration 1_broken failed") |
| 49 | + |
| 50 | + out.Reset() |
| 51 | + assert.NoError(t, cli.Run(ctx, "version")) |
| 52 | + assert.Equal(t, "1 (dirty)\n", out.String()) |
| 53 | + }) |
| 54 | + t.Run("create", func(t *testing.T) { |
| 55 | + stubTimeNow(t, time.Date(2026, 7, 6, 12, 34, 56, 0, time.UTC)) |
| 56 | + dir := t.TempDir() |
| 57 | + var out bytes.Buffer |
| 58 | + // create does not require DB nor FS |
| 59 | + cli := &Cli{Dir: dir, Out: &out} |
| 60 | + assert.NoError(t, cli.Run(ctx, "create", "add_age")) |
| 61 | + assert.Equal(t, |
| 62 | + filepath.Join(dir, "20260706123456_add_age.up.sql")+"\n"+ |
| 63 | + filepath.Join(dir, "20260706123456_add_age.down.sql")+"\n", |
| 64 | + out.String()) |
| 65 | + |
| 66 | + assert.EqualError(t, cli.Run(ctx, "create"), "usage: create <name>") |
| 67 | + assert.EqualError(t, cli.Run(ctx, "create", "a", "b"), "usage: create <name>") |
| 68 | + }) |
| 69 | + t.Run("create defaults to the migrations directory", func(t *testing.T) { |
| 70 | + stubTimeNow(t, time.Date(2026, 7, 6, 12, 34, 56, 0, time.UTC)) |
| 71 | + dir := t.TempDir() |
| 72 | + t.Chdir(dir) |
| 73 | + require.NoError(t, (&Cli{Out: &bytes.Buffer{}}).Run(ctx, "create", "add_age")) |
| 74 | + assert.FileExists(t, filepath.Join(dir, "migrations", "20260706123456_add_age.up.sql")) |
| 75 | + }) |
| 76 | + t.Run("errors", func(t *testing.T) { |
| 77 | + cli := &Cli{FS: testCliFS, Out: &bytes.Buffer{}} |
| 78 | + assert.ErrorContains(t, cli.Run(ctx), "expects a subcommand") |
| 79 | + assert.ErrorContains(t, cli.Run(ctx, "unknown"), "unknown command: unknown") |
| 80 | + |
| 81 | + broken := &Cli{FS: fstest.MapFS{"1_a.down.sql": {Data: []byte("x")}}, Out: &bytes.Buffer{}} |
| 82 | + assert.EqualError(t, broken.Run(ctx, "up"), "missing up migration for 1_a") |
| 83 | + }) |
| 84 | +} |
0 commit comments