mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
43 lines
967 B
Go
43 lines
967 B
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
sqlmigrations "github.com/SecurityBrewery/catalyst/app/database/migrations"
|
|
"github.com/SecurityBrewery/catalyst/app/database/sqlc"
|
|
"github.com/SecurityBrewery/catalyst/app/upload"
|
|
)
|
|
|
|
type sqlMigration struct {
|
|
sqlName string
|
|
upSQL string
|
|
}
|
|
|
|
func newSQLMigration(name string) func() (migration, error) {
|
|
return func() (migration, error) {
|
|
up, err := sqlmigrations.Migrations.ReadFile(name + ".up.sql")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read up migration file for %s: %w", name, err)
|
|
}
|
|
|
|
return &sqlMigration{
|
|
sqlName: name,
|
|
upSQL: string(up),
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func (m sqlMigration) name() string {
|
|
return m.sqlName
|
|
}
|
|
|
|
func (m sqlMigration) up(ctx context.Context, queries *sqlc.Queries, _ string, _ *upload.Uploader) error {
|
|
_, err := queries.WriteDB.ExecContext(ctx, m.upSQL)
|
|
if err != nil {
|
|
return fmt.Errorf("migration %s up failed: %w", m.sqlName, err)
|
|
}
|
|
|
|
return nil
|
|
}
|