aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration_fs_repo_test.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-03-16 15:06:30 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-03-16 15:06:30 -0400
commit65ded7c0133ae011a30546fb4fd1e00fc3179874 (patch)
tree305c9655c581ba5c959d56cf801a96bf80683dd7 /internal/repositories/migration_fs_repo_test.go
parentUpdate README (diff)
downloadcli-65ded7c0133ae011a30546fb4fd1e00fc3179874.zip
Update migration file format
Diffstat (limited to 'internal/repositories/migration_fs_repo_test.go')
-rw-r--r--internal/repositories/migration_fs_repo_test.go41
1 files changed, 19 insertions, 22 deletions
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index 32de193..96d48a0 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -59,26 +59,23 @@ func TestCreate_SuccessfullyCreated(t *testing.T) {
err = repo.Create(migration)
assert.Nil(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"))
+ assertFileExists(t, filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())))
}
-func TestCreate_FailsToCreateDir(t *testing.T) {
+func TestCreate_FailsToCreateMigration(t *testing.T) {
tempDir := t.TempDir()
- migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
- repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
+ repo, err := repositories.NewMigrationFsRepo(
+ &configloader.MigrationsConfig{DirectoryPath: tempDir},
+ )
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "add users table")
- migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version)
- _, err = os.Create(filepath.Join(tempDir, migrationDirName))
+ _, err = os.OpenFile(filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())), os.O_CREATE, 0000)
assert.Nil(t, err)
err = repo.Create(migration)
assert.NotNil(t, err)
- assert.ErrorContains(t, err, "file exists")
+ assert.ErrorContains(t, err, "unable to create file")
}
func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) {
@@ -91,17 +88,17 @@ func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) {
err = repo.Create(migration)
assert.Nil(t, err)
- migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version)
- expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);")
+ migrationName := fmt.Sprintf("%s.sql", migration.Name())
+ expectedUpgradeScriptContents := "CREATE TABLE users(id int PRIMARY KEY);\n"
os.WriteFile(
- filepath.Join(tempDir, migrationDirName, "upgrade.sql"),
- expectedUpgradeScriptContents,
+ filepath.Join(tempDir, migrationName),
+ []byte("-- migrate:up\n"+expectedUpgradeScriptContents),
0755,
)
- upgradeScriptContents, err := repo.ReadUpgradeScript(migration)
+ upgradeScript, err := repo.ReadUpgradeScript(migration)
assert.Nil(t, err)
- assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents))
+ assert.Equal(t, upgradeScript.Contents, string(expectedUpgradeScriptContents))
}
func TestReadUpgradeScript_FileDoesNotExist(t *testing.T) {
@@ -126,17 +123,17 @@ func TestReadDowngradeScript_SuccessfullyRead(t *testing.T) {
err = repo.Create(migration)
assert.Nil(t, err)
- migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version)
- expectedDowngradeScriptContents := []byte("DROP TABLE users;")
+ migrationName := fmt.Sprintf("%s.sql", migration.Name())
+ expectedDowngradeScriptContents := "DROP TABLE users;\n"
os.WriteFile(
- filepath.Join(tempDir, migrationDirName, "downgrade.sql"),
- expectedDowngradeScriptContents,
+ filepath.Join(tempDir, migrationName),
+ []byte("-- migrate:down\n"+expectedDowngradeScriptContents),
0755,
)
- downgradeScriptContents, err := repo.ReadDowngradeScript(migration)
+ downgradeScript, err := repo.ReadDowngradeScript(migration)
assert.Nil(t, err)
- assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents))
+ assert.Equal(t, downgradeScript.Contents, string(expectedDowngradeScriptContents))
}
func TestReadDowngradeScript_FileDoesNotExist(t *testing.T) {