Initial commit
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
36
backend/internal/migrate/migrate.go
Executable file
36
backend/internal/migrate/migrate.go
Executable file
@@ -0,0 +1,36 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
)
|
||||
|
||||
// Run führt alle ausstehenden Migrationen aus.
|
||||
// migrationsFS muss die Migrations-Dateien enthalten.
|
||||
func Run(db *sql.DB, migrationsFS fs.FS) error {
|
||||
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Migrations-Treiber erstellen: %w", err)
|
||||
}
|
||||
|
||||
source, err := iofs.New(migrationsFS, ".")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Migrations-Quelle erstellen: %w", err)
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithInstance("iofs", source, "sqlite3", driver)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Migrator erstellen: %w", err)
|
||||
}
|
||||
|
||||
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("Migrationen ausführen: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user