-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_reader_test.go
77 lines (64 loc) · 2.51 KB
/
migration_reader_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
package pgutil
import (
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testUpQuery = `-- Create the comments table
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
`
var testDownQuery = `-- Drop the comments table
DROP TABLE IF EXISTS comments;
`
var testConcurrentIndexUpQuery = `-- Create a concurrent index
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);`
var testConcurrentIndexDownQuery = `-- Drop the concurrent index
DROP INDEX CONCURRENTLY IF EXISTS idx_users_email;`
func TestReadMigrations(t *testing.T) {
t.Run("valid", func(t *testing.T) {
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "valid")))
require.NoError(t, err)
require.Len(t, definitions, 3)
assert.Equal(t, Definition{
ID: 3,
Name: "third",
UpQuery: RawQuery(testUpQuery),
DownQuery: RawQuery(testDownQuery),
}, definitions[2])
})
t.Run("CIC pattern", func(t *testing.T) {
t.Skip()
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_pattern")))
require.NoError(t, err)
require.Len(t, definitions, 4)
assert.Equal(t, Definition{
ID: 3,
Name: "third",
UpQuery: RawQuery(testConcurrentIndexUpQuery),
DownQuery: RawQuery(testConcurrentIndexDownQuery),
IndexMetadata: &IndexMetadata{
TableName: "users",
IndexName: "idx_users_email",
},
}, definitions[3])
})
t.Run("duplicate identifiers", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "duplicate_identifiers")))
assert.ErrorContains(t, err, "duplicate migration identifier 2")
})
t.Run("CIC with additional queries", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_with_additional_queries")))
assert.ErrorContains(t, err, `"create index concurrently" is not the only statement in the up migration`)
})
t.Run("CIC in down migration", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_in_down_migration")))
assert.ErrorContains(t, err, `"create index concurrently" is not the only statement in the up migration`)
})
}