-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
126 lines (101 loc) · 2.76 KB
/
migrator_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
126
package kin
import (
"database/sql"
"fmt"
"os"
"testing"
_ "github.com/jmataya/renv/autoload"
_ "github.com/lib/pq"
)
func setupMigrationDir(path string) error {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
return nil
}
func cleanupMigrationDir(path string) error {
return os.RemoveAll(path)
}
func createFile(path, name, contents string) error {
file, err := os.Create(fmt.Sprintf("%s/%s", path, name))
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(fmt.Sprintf("%s\n", contents)); err != nil {
return err
}
file.Sync()
return nil
}
func TestMigrate(t *testing.T) {
migrationPath := "./sql"
cleanupMigrationDir(migrationPath)
if err := setupMigrationDir(migrationPath); err != nil {
t.Errorf("setupMigrationDir = %v", err)
return
}
createFoo := "create table foo (id serial primary key);"
createBar := `
create table bar (
id serial primary key,
foo_id int not null,
foreign key (foo_id) references foo(id) on update restrict on delete restrict
);
`
if err := createFile(migrationPath, "1__create_foo.sql", createFoo); err != nil {
t.Errorf("createFile = %v", err)
cleanupMigrationDir(migrationPath)
return
}
if err := createFile(migrationPath, "2__create_bar.sql", createBar); err != nil {
t.Errorf("createFile = %v", err)
cleanupMigrationDir(migrationPath)
return
}
connStr := os.Getenv("POSTGRES_URL")
if connStr == "" {
t.Error("POSTGRES_URL is empty")
return
}
if connStr == "" {
panic("POSTGRES_URL must not be empty")
}
db, _ := sql.Open("postgres", connStr)
migrator, _ := NewMigrator(db)
if err := migrator.Migrate(migrationPath); err != nil {
t.Errorf("migrator.Migrate(%s) = %v, want nil", migrationPath, err)
cleanupMigrationDir(migrationPath)
}
cleanupMigrationDir(migrationPath)
}
func TestMigrateRerun(t *testing.T) {
migrationPath := "./sql"
cleanupMigrationDir(migrationPath)
if err := setupMigrationDir(migrationPath); err != nil {
t.Errorf("setupMigrationDir = %v", err)
return
}
createBaz := "create table baz (id serial primary key);"
if err := createFile(migrationPath, "1__create_baz.sql", createBaz); err != nil {
t.Errorf("createFile = %v", err)
cleanupMigrationDir(migrationPath)
return
}
connStr := os.Getenv("POSTGRES_URL")
if connStr == "" {
t.Error("POSTGRES_URL is empty")
return
}
db, _ := sql.Open("postgres", connStr)
migrator, _ := NewMigrator(db)
if err := migrator.Migrate(migrationPath); err != nil {
t.Errorf("migrator.Migrate(%s) = %v, want nil", migrationPath, err)
cleanupMigrationDir(migrationPath)
return
}
if err := migrator.Migrate(migrationPath); err != nil {
t.Errorf("migrator.Migrate(%s) = %v, want nil", migrationPath, err)
}
cleanupMigrationDir(migrationPath)
}