aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration_fs_repo_test.go
blob: 8fdcd8f91c834e72b2f6b3bd84880b4b0079742e (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
package repositories_test

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"
	"time"

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

func assertFileExists(t *testing.T, path string) {
	_, err := os.Stat(path)
	assert.NilError(t, err)
}

func TestNewMigrationFsRepoCreatesDirIfNotExists(t *testing.T) {
	tempDir := t.TempDir()
	migrationsDir := filepath.Join(tempDir, "migrations")
	migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir}

	_, err := repositories.NewMigrationFsRepo(&migrationsConfig)
	assert.NilError(t, err)

	assertFileExists(t, migrationsDir)
}

func TestNewMigrationFsRepoMigrationDirIsFile(t *testing.T) {
	tempDir := t.TempDir()
	migrationsDir := filepath.Join(tempDir, "migrations")
	migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir}
	_, err := os.Create(migrationsDir)
	assert.NilError(t, err)

	_, err = repositories.NewMigrationFsRepo(&migrationsConfig)
	assert.ErrorContains(t, err, "is not a directory")
}

func TestMigrationFsRepoCreate(t *testing.T) {
	tempDir := t.TempDir()
	migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
	repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
	assert.NilError(t, err)
	migration := models.NewMigration(time.Now(), "add users table")

	err = repo.Create(migration)
	assert.NilError(t, err)

	migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version)
	assertFileExists(t, filepath.Join(tempDir, migrationDirName))
	assertFileExists(t, filepath.Join(tempDir, migrationDirName, "upgrade.sql"))
	assertFileExists(t, filepath.Join(tempDir, migrationDirName, "downgrade.sql"))
}

func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) {
	tempDir := t.TempDir()
	migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
	repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
	assert.NilError(t, err)

	migration := models.NewMigration(time.Now(), "add users table")
	err = repo.Create(migration)
	assert.NilError(t, err)

	migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version)
	expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);")
	os.WriteFile(
		filepath.Join(tempDir, migrationDirName, "upgrade.sql"),
		expectedUpgradeScriptContents,
		0755,
	)
	expectedDowngradeScriptContents := []byte("DROP TABLE users;")
	os.WriteFile(
		filepath.Join(tempDir, migrationDirName, "downgrade.sql"),
		expectedDowngradeScriptContents,
		0755,
	)

	upgradeScriptContents, err := repo.ReadUpgradeScript(migration)
	assert.NilError(t, err)
	assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents))
	downgradeScriptContents, err := repo.ReadDowngradeScript(migration)
	assert.NilError(t, err)
	assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents))
}

func TestMigrationFsRepo_List(t *testing.T) {
	tempDir := t.TempDir()
	migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
	repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
	assert.NilError(t, err)

	migration1 := models.NewMigration(
		time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
		"migration_1",
	)
	migration2 := models.NewMigration(
		time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
		"migration_2",
	)
	err = repo.Create(migration1)
	assert.NilError(t, err)
	repo.Create(migration2)
	assert.NilError(t, err)

	migrations, err := repo.List()
	assert.NilError(t, err)
	assert.Equal(t, len(migrations), 2)
	assert.DeepEqual(t, migrations[migration1.Version], migration1)
	assert.DeepEqual(t, migrations[migration2.Version], migration2)
}