diff --git a/cobble.go b/cobble.go index 9991768..77f1a2f 100644 --- a/cobble.go +++ b/cobble.go @@ -31,7 +31,7 @@ var up = &cobra.Command{ for _, migration := range migrationHistory { log.Printf("%s", migration.Name) - if err = migrate(db, migration.Path); err != nil { + if err = ApplyMigration(db, migration); err != nil { os.Exit(1) } } diff --git a/migrate.go b/migrate.go index 784a331..950f34e 100644 --- a/migrate.go +++ b/migrate.go @@ -17,21 +17,15 @@ import ( var commentPrefix = []byte("--") var requirementPrefix = []byte("requires:") -func NewMigration(path string, name string, requires string) Migration { - return Migration{ - Path: path, - Name: name, - Requires: requires, - Run: false, - } -} - -// Applies a migration to the given database. -func migrate(db DB, migrationPath string) error { +// Applies a migration to the given database connection. +// +// If an error is returned while trying to read the migration file +// or execute the SQL it contains, the error is returned. +func ApplyMigration(db DB, migration Migration) error { + migrationPath := migration.Path migrationBytes, err := os.ReadFile(migrationPath) if err != nil { - log.Printf("Failed to read migration file: %s", migrationPath) return err } @@ -42,11 +36,11 @@ func migrate(db DB, migrationPath string) error { err = db.Execute(migrationSql) if err != nil { - log.Printf("Failed to run %s: %s", migrationPath, err) return err } return nil + } // Removes the provided prefix from the byte slice and removes diff --git a/migrate_test.go b/migrate_test.go index 01f68d2..6560d65 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -163,7 +163,9 @@ func TestMigrateRunsSqlOnDBConnection(t *testing.T) { migrationSql := "SELECT 1 FROM table;" os.WriteFile(migrationPath, []byte(migrationSql), 0750) - err := migrate(mockDb, migrationPath) + migrations, _ := getMigrations(root) + + err := ApplyMigration(mockDb, migrations[0]) if err != nil { t.Errorf("Expected no error returned, got %#v", err) @@ -179,7 +181,9 @@ func TestMigrateReturnsErrorOnFailToReadMigrationFile(t *testing.T) { root := t.TempDir() // Does not exist. migrationPath := filepath.Join(root, "0001.sql") - err := migrate(mockDb, migrationPath) + + migration := NewMigration(migrationPath, "0001.sql", "") + err := ApplyMigration(mockDb, migration) if err == nil { t.Errorf("Expected error returned, got nil") @@ -194,7 +198,8 @@ func TestMigrateReturnsErrorOnFailToRunSQL(t *testing.T) { migrationPath := filepath.Join(root, "0001.sql") os.WriteFile(migrationPath, []byte(migrationSql), 0750) - err := migrate(mockDb, migrationPath) + migrations, _ := getMigrations(root) + err := ApplyMigration(mockDb, migrations[0]) if err == nil { t.Errorf("Expected error returned, got nil") diff --git a/migration.go b/migration.go new file mode 100644 index 0000000..7c1ade2 --- /dev/null +++ b/migration.go @@ -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, + } +} diff --git a/models.go b/models.go index dbe86a8..4f53038 100644 --- a/models.go +++ b/models.go @@ -4,13 +4,6 @@ type MigrationHeaders struct { Requirements []string } -type Migration struct { - Path string - Name string - Requires string - Run bool -} - type DB interface { Execute(sql string) error }