hako/internal/database/migrations.go
Vibe Kanban e6fe0bc9af
Some checks failed
ci/woodpecker/tag/release Pipeline failed
Unify all migrations in a single migration to do the first release. (vibe-kanban 0d3b391f)
2026-01-29 12:57:26 +01:00

51 lines
1.4 KiB
Go

package database
import (
"context"
"database/sql"
"fmt"
"git.nakama.town/fmartingr/gotoolkit/database"
"github.com/blang/semver/v4"
"github.com/huandu/go-sqlbuilder"
)
// initSchemaVersionTable creates the schema_version table if it doesn't exist
func initSchemaVersionTable(db *sql.DB) error {
ctb := sqlbuilder.NewCreateTableBuilder()
ctb.CreateTable("schema_version").IfNotExists()
ctb.Define("id", "INTEGER", "PRIMARY KEY", "AUTOINCREMENT")
ctb.Define("version", "TEXT", "NOT NULL")
ctb.Define("applied_at", "DATETIME", "DEFAULT", "CURRENT_TIMESTAMP")
query, args := ctb.Build()
_, err := db.Exec(query, args...)
if err != nil {
return fmt.Errorf("failed to create schema_version table: %w", err)
}
return nil
}
// GetMigrations returns all database migrations
func GetMigrations() []database.Migration {
return []database.Migration{
{
FromVersion: semver.MustParse("0.0.0"),
ToVersion: semver.MustParse("1.0.0"),
MigrationFunc: migration_0_0_0_to_1_0_0,
},
}
}
// RunMigrations runs all pending migrations
func RunMigrations(ctx context.Context, conns *Connections) error {
// Initialize schema version table first
if err := initSchemaVersionTable(conns.Write); err != nil {
return fmt.Errorf("failed to initialize schema version table: %w", err)
}
// Run migrations using gotoolkit
migrations := GetMigrations()
return database.RunMigrations(ctx, conns, migrations)
}