-
Notifications
You must be signed in to change notification settings - Fork 524
/
provider_test.go
81 lines (72 loc) · 1.92 KB
/
provider_test.go
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
package goose_test
import (
"database/sql"
"io/fs"
"path/filepath"
"testing"
"testing/fstest"
"github.com/pressly/goose/v3"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
func TestProvider(t *testing.T) {
dir := t.TempDir()
db, err := sql.Open("sqlite", filepath.Join(dir, "sql_embed.db"))
require.NoError(t, err)
t.Run("empty", func(t *testing.T) {
_, err := goose.NewProvider(goose.DialectSQLite3, db, fstest.MapFS{})
require.Error(t, err)
require.ErrorIs(t, err, goose.ErrNoMigrations)
})
mapFS := fstest.MapFS{
"migrations/001_foo.sql": {Data: []byte(`-- +goose Up`)},
"migrations/002_bar.sql": {Data: []byte(`-- +goose Up`)},
}
fsys, err := fs.Sub(mapFS, "migrations")
require.NoError(t, err)
p, err := goose.NewProvider(goose.DialectSQLite3, db, fsys)
require.NoError(t, err)
sources := p.ListSources()
require.Len(t, sources, 2)
require.Equal(t, sources[0], newSource(goose.TypeSQL, "001_foo.sql", 1))
require.Equal(t, sources[1], newSource(goose.TypeSQL, "002_bar.sql", 2))
}
var (
migration1 = `
-- +goose Up
CREATE TABLE foo (id INTEGER PRIMARY KEY);
-- +goose Down
DROP TABLE foo;
`
migration2 = `
-- +goose Up
ALTER TABLE foo ADD COLUMN name TEXT;
-- +goose Down
ALTER TABLE foo DROP COLUMN name;
`
migration3 = `
-- +goose Up
CREATE TABLE bar (
id INTEGER PRIMARY KEY,
description TEXT
);
-- +goose Down
DROP TABLE bar;
`
migration4 = `
-- +goose Up
-- Rename the 'foo' table to 'my_foo'
ALTER TABLE foo RENAME TO my_foo;
-- Add a new column 'timestamp' to 'my_foo'
ALTER TABLE my_foo ADD COLUMN timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
-- +goose Down
-- Remove the 'timestamp' column from 'my_foo'
ALTER TABLE my_foo DROP COLUMN timestamp;
-- Rename the 'my_foo' table back to 'foo'
ALTER TABLE my_foo RENAME TO foo;
`
)
func TestPartialErrorUnwrap(t *testing.T) {
err := &goose.PartialError{Err: goose.ErrNoCurrentVersion}
require.ErrorIs(t, err, goose.ErrNoCurrentVersion)
}