refactor: group migration logic, use Migration to apply instead of path
This commit is contained in:
parent
88db84cac4
commit
90a512bd91
5 changed files with 33 additions and 24 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
20
migrate.go
20
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
|
||||
|
|
|
@ -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")
|
||||
|
|
17
migration.go
Normal file
17
migration.go
Normal 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,
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue