48 lines
814 B
Go
48 lines
814 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
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,
|
|
}
|
|
}
|
|
|
|
// 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 (m *Migration) ApplyMigration(db DB) error {
|
|
migrationPath := m.Path
|
|
migrationBytes, err := os.ReadFile(migrationPath)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
migrationSql := string(migrationBytes)
|
|
|
|
log.Printf("SQL: %s", migrationSql)
|
|
|
|
err = db.Execute(migrationSql)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|