This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
125 lines (102 loc) · 4.01 KB
/
parser_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
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
package voyager_test
import (
"code.cloudfoundry.org/lager/lagertest"
"github.com/concourse/voyager"
"github.com/concourse/voyager/voyagerfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var basicSQLMigration = []byte(`
BEGIN;
CREATE TABLE some_table;
COMMIT;`)
var noTransactionMigration = []byte(`
-- NO_TRANSACTION
CREATE TYPE enum_type AS ENUM ('blue_type', 'green_type');
ALTER TYPE enum_type ADD VALUE 'some_type'; `)
var multipleStatementMigration = []byte(`
BEGIN;
CREATE TABLE some_table (ID integer, something varchar);
ALTER TABLE some_table ADD COLUMN notes varchar;
COMMIT;`)
var sqlFunctionMigration = []byte(`
BEGIN;
CREATE OR REPLACE FUNCTION on_item_delete() RETURNS TRIGGER AS $$
BEGIN
EXECUTE format('DROP TABLE IF EXISTS item%s', OLD.id);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;`)
var _ = Describe("Parser", func() {
var (
parser voyager.Parser
bindata *voyagerfakes.FakeSource
logger *lagertest.TestLogger
)
BeforeEach(func() {
logger = lagertest.NewTestLogger("test")
bindata = new(voyagerfakes.FakeSource)
bindata.AssetReturns([]byte{}, nil)
parser = voyager.NewParser(bindata)
})
It("parses the direction of the migration from the file name", func() {
downMigration, err := parser.ParseFileToMigration(logger, "2000_some_migration.down.go")
Expect(err).ToNot(HaveOccurred())
Expect(downMigration.Direction).To(Equal("down"))
upMigration, err := parser.ParseFileToMigration(logger, "1000_some_migration.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(upMigration.Direction).To(Equal("up"))
})
It("parses the strategy of the migration from the file", func() {
downMigration, err := parser.ParseFileToMigration(logger, "2000_some_migration.down.go")
Expect(err).ToNot(HaveOccurred())
Expect(downMigration.Strategy).To(Equal(voyager.GoMigration))
bindata.AssetReturns(basicSQLMigration, nil)
upMigration, err := parser.ParseFileToMigration(logger, "1000_some_migration.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(upMigration.Strategy).To(Equal(voyager.SQLTransaction))
bindata.AssetReturns(noTransactionMigration, nil)
upNoTxMigration, err := parser.ParseFileToMigration(logger, "3000_some_no_transaction_migration.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(upNoTxMigration.Strategy).To(Equal(voyager.SQLNoTransaction))
})
Context("SQL migrations", func() {
It("parses the migration into statements", func() {
bindata.AssetReturns(multipleStatementMigration, nil)
migration, err := parser.ParseFileToMigration(logger, "1234_create_and_alter_table.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(len(migration.Statements)).To(Equal(2))
})
It("combines sql functions in one statement", func() {
bindata.AssetReturns(sqlFunctionMigration, nil)
migration, err := parser.ParseFileToMigration(logger, "1800_sql_function_migration.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(len(migration.Statements)).To(Equal(1))
})
It("removes the BEGIN and COMMIT statements", func() {
bindata.AssetReturns(multipleStatementMigration, nil)
migration, err := parser.ParseFileToMigration(logger, "1234_create_and_alter_table.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(len(migration.Statements)).To(Equal(2))
Expect(migration.Statements[0]).ToNot(Equal("BEGIN"))
})
Context("No transactions", func() {
It("marks migration as no transaction", func() {
bindata.AssetReturns(noTransactionMigration, nil)
migration, err := parser.ParseFileToMigration(logger, "3000_some_no_transaction_migration.up.sql")
Expect(err).ToNot(HaveOccurred())
Expect(len(migration.Statements)).To(Equal(2))
})
})
})
Context("Go migrations", func() {
It("returns the name of the migration function to run", func() {
bindata.AssetReturns([]byte(`
func (m *Migrator) Up_2000() {}
`), nil)
migration, err := parser.ParseFileToMigration(logger, "2000_some_go_migration.up.go")
Expect(err).ToNot(HaveOccurred())
Expect(migration.Name).To(Equal("Up_2000"))
})
})
})