refactor: group migration logic, use Migration to apply instead of path

This commit is contained in:
Marc 2024-07-11 20:16:17 -04:00
parent 88db84cac4
commit 90a512bd91
Signed by: marc
GPG key ID: 048E042F22B5DC79
5 changed files with 33 additions and 24 deletions

View file

@ -31,7 +31,7 @@ var up = &cobra.Command{
for _, migration := range migrationHistory { for _, migration := range migrationHistory {
log.Printf("%s", migration.Name) log.Printf("%s", migration.Name)
if err = migrate(db, migration.Path); err != nil { if err = ApplyMigration(db, migration); err != nil {
os.Exit(1) os.Exit(1)
} }
} }

View file

@ -17,21 +17,15 @@ import (
var commentPrefix = []byte("--") var commentPrefix = []byte("--")
var requirementPrefix = []byte("requires:") var requirementPrefix = []byte("requires:")
func NewMigration(path string, name string, requires string) Migration { // Applies a migration to the given database connection.
return Migration{ //
Path: path, // If an error is returned while trying to read the migration file
Name: name, // or execute the SQL it contains, the error is returned.
Requires: requires, func ApplyMigration(db DB, migration Migration) error {
Run: false, migrationPath := migration.Path
}
}
// Applies a migration to the given database.
func migrate(db DB, migrationPath string) error {
migrationBytes, err := os.ReadFile(migrationPath) migrationBytes, err := os.ReadFile(migrationPath)
if err != nil { if err != nil {
log.Printf("Failed to read migration file: %s", migrationPath)
return err return err
} }
@ -42,11 +36,11 @@ func migrate(db DB, migrationPath string) error {
err = db.Execute(migrationSql) err = db.Execute(migrationSql)
if err != nil { if err != nil {
log.Printf("Failed to run %s: %s", migrationPath, err)
return err return err
} }
return nil return nil
} }
// Removes the provided prefix from the byte slice and removes // Removes the provided prefix from the byte slice and removes

View file

@ -163,7 +163,9 @@ func TestMigrateRunsSqlOnDBConnection(t *testing.T) {
migrationSql := "SELECT 1 FROM table;" migrationSql := "SELECT 1 FROM table;"
os.WriteFile(migrationPath, []byte(migrationSql), 0750) os.WriteFile(migrationPath, []byte(migrationSql), 0750)
err := migrate(mockDb, migrationPath) migrations, _ := getMigrations(root)
err := ApplyMigration(mockDb, migrations[0])
if err != nil { if err != nil {
t.Errorf("Expected no error returned, got %#v", err) t.Errorf("Expected no error returned, got %#v", err)
@ -179,7 +181,9 @@ func TestMigrateReturnsErrorOnFailToReadMigrationFile(t *testing.T) {
root := t.TempDir() root := t.TempDir()
// Does not exist. // Does not exist.
migrationPath := filepath.Join(root, "0001.sql") migrationPath := filepath.Join(root, "0001.sql")
err := migrate(mockDb, migrationPath)
migration := NewMigration(migrationPath, "0001.sql", "")
err := ApplyMigration(mockDb, migration)
if err == nil { if err == nil {
t.Errorf("Expected error returned, got nil") t.Errorf("Expected error returned, got nil")
@ -194,7 +198,8 @@ func TestMigrateReturnsErrorOnFailToRunSQL(t *testing.T) {
migrationPath := filepath.Join(root, "0001.sql") migrationPath := filepath.Join(root, "0001.sql")
os.WriteFile(migrationPath, []byte(migrationSql), 0750) os.WriteFile(migrationPath, []byte(migrationSql), 0750)
err := migrate(mockDb, migrationPath) migrations, _ := getMigrations(root)
err := ApplyMigration(mockDb, migrations[0])
if err == nil { if err == nil {
t.Errorf("Expected error returned, got nil") t.Errorf("Expected error returned, got nil")

17
migration.go Normal file
View file

@ -0,0 +1,17 @@
package main
type Migration struct {
Path string
Name string
Requires string
Run bool
}
func NewMigration(path string, name string, requires string) Migration {
return Migration{
Path: path,
Name: name,
Requires: requires,
Run: false,
}
}

View file

@ -4,13 +4,6 @@ type MigrationHeaders struct {
Requirements []string Requirements []string
} }
type Migration struct {
Path string
Name string
Requires string
Run bool
}
type DB interface { type DB interface {
Execute(sql string) error Execute(sql string) error
} }