38 lines
1 KiB
Go
38 lines
1 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"fmt"
|
|
|
|
"git.juancwu.dev/juancwu/pase/store"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
// Migrate applies all pending migrations to db using the SQLite dialect.
|
|
// It is safe to call repeatedly; already-applied migrations are skipped.
|
|
//
|
|
// Run from a single instance at startup. There is no advisory locking; if you
|
|
// need it, gate the call behind your own coordination primitive.
|
|
func Migrate(ctx context.Context, db *sql.DB) error {
|
|
migrations, err := store.LoadMigrations(migrationsFS, "migrations")
|
|
if err != nil {
|
|
return fmt.Errorf("pase/sqlite: load migrations: %w", err)
|
|
}
|
|
|
|
m := &store.Migrator{
|
|
DB: db,
|
|
Dialect: store.SQLiteDialect{},
|
|
Migrations: migrations,
|
|
// SQLite's TEXT timestamp convention. INTEGER PK is implicit ROWID alias.
|
|
CreateTableSQL: `CREATE TABLE IF NOT EXISTS pase_schema_migrations (
|
|
version INTEGER PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
applied_at TEXT NOT NULL
|
|
)`,
|
|
}
|
|
return m.Migrate(ctx)
|
|
}
|