aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration_db_repo_test.go
blob: 55b8eb27a8eeb50b54b3dab97e3a78d31a73fde6 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package repositories_test

import (
	"database/sql"
	"testing"
	"time"

	"github.com/eugenetriguba/bolt/internal/bolttest"
	"github.com/eugenetriguba/bolt/internal/models"
	"github.com/eugenetriguba/bolt/internal/repositories"
	"gotest.tools/v3/assert"
)

func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	row := db.QueryRow(`
		SELECT 1 
		FROM INFORMATION_SCHEMA.TABLES 
		WHERE TABLE_SCHEMA = 'public' 
		AND TABLE_NAME = 'bolt_migrations'
	`)
	var scanResult int
	err := row.Scan(&scanResult)
	assert.ErrorIs(t, err, sql.ErrNoRows)

	_, err = repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	row = db.QueryRow(`
		SELECT 1 
		FROM INFORMATION_SCHEMA.TABLES 
		WHERE TABLE_SCHEMA = 'public' 
		AND TABLE_NAME = 'bolt_migrations'
	`)
	err = row.Scan(&scanResult)
	assert.NilError(t, err)
}

func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	_, err := db.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`)
	assert.NilError(t, err)
	_, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`)
	assert.NilError(t, err)

	_, err = repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	row := db.QueryRow(`SELECT id FROM bolt_migrations`)
	var scanResult int
	err = row.Scan(&scanResult)
	assert.NilError(t, err)
	assert.Equal(t, scanResult, 1)
}

func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	migrations, err := repo.List()
	assert.NilError(t, err)
	assert.Equal(t, len(migrations), 0)
}

func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	version := "20230101000000"
	_, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version)
	assert.NilError(t, err)

	migrations, err := repo.List()
	assert.NilError(t, err)
	assert.Equal(t, len(migrations), 1)
	assert.DeepEqual(
		t,
		migrations[version],
		&models.Migration{Version: version, Message: "", Applied: true},
	)
}

func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	version := "20230101"
	_, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version)
	assert.NilError(t, err)

	migrations, err := repo.List()
	assert.NilError(t, err)
	assert.Equal(t, len(migrations), 1)
	assert.DeepEqual(
		t,
		migrations[version],
		&models.Migration{Version: version, Message: "", Applied: true},
	)
}

func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	version := "20230101010101"
	applied, err := repo.IsApplied(version)
	assert.NilError(t, err)
	assert.Equal(t, applied, false)
}

func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	version := "20230101010101"
	_, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version)
	assert.NilError(t, err)

	applied, err := repo.IsApplied(version)
	assert.NilError(t, err)
	assert.Equal(t, applied, true)
}

func TestMigrationDBRepo_Apply(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	migration := models.NewMigration(time.Now(), "test")
	err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration)
	assert.NilError(t, err)
	assert.Equal(t, migration.Applied, true)

	row := db.QueryRow(`
		SELECT 1 
		FROM INFORMATION_SCHEMA.TABLES 
		WHERE TABLE_SCHEMA = 'public' 
		AND TABLE_NAME = 'tmp'
	`)
	var scanResult int
	err = row.Scan(&scanResult)
	assert.NilError(t, err)
	applied, err := repo.IsApplied(migration.Version)
	assert.NilError(t, err)
	assert.Equal(t, applied, true)
}

func TestMigrationDBRepo_Revert(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)

	_, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
	assert.NilError(t, err)

	migration := models.NewMigration(time.Now(), "test")
	_, err = db.Exec(
		`INSERT INTO bolt_migrations(version) VALUES ($1);`,
		migration.Version,
	)
	assert.NilError(t, err)
	migration.Applied = true

	err = repo.Revert(`DROP TABLE tmp;`, migration)
	assert.NilError(t, err)
	assert.Equal(t, migration.Applied, false)

	row := db.QueryRow(`
		SELECT 1 
		FROM INFORMATION_SCHEMA.TABLES 
		WHERE TABLE_SCHEMA = 'public' 
		AND TABLE_NAME = 'tmp'
	`)
	var scanResult int
	err = row.Scan(&scanResult)
	assert.ErrorIs(t, err, sql.ErrNoRows)

	row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`)
	var count int
	err = row.Scan(&count)
	assert.NilError(t, err)
	assert.Equal(t, count, 0)
}

func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)
	_, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)")
	assert.NilError(t, err)

	migration := models.NewMigration(time.Now(), "test")
	// CREATE INDEX CONCURRENTLY cannot run inside a transaction.
	err = repo.Apply(
		`-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`,
		migration,
	)
	assert.NilError(t, err)
	assert.Equal(t, migration.Applied, true)

	err = repo.Revert(
		"-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);",
		migration,
	)
	assert.NilError(t, err)
	assert.Equal(t, migration.Applied, false)
}

func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)
	migration := models.NewMigration(time.Now(), "test")

	err = repo.Apply("this is not SQL", migration)
	assert.ErrorContains(t, err, "syntax error")
	assert.Equal(t, migration.Applied, false)

	err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration)
	assert.ErrorContains(t, err, "syntax error")
	assert.Equal(t, migration.Applied, false)
}

func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) {
	db := bolttest.NewTestDB(t, "postgres")
	repo, err := repositories.NewMigrationDBRepo(db)
	assert.NilError(t, err)
	migration := models.NewMigration(time.Now(), "test")
	migration.Applied = true

	err = repo.Revert("this is not SQL", migration)
	assert.ErrorContains(t, err, "syntax error")
	assert.Equal(t, migration.Applied, true)

	err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration)
	assert.ErrorContains(t, err, "syntax error")
	assert.Equal(t, migration.Applied, true)
}