76 lines
5.8 KiB
Go
76 lines
5.8 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
// InitializeFresh bootstraps an empty database/schema and is idempotent after
|
|
// its first successful migration. Existing unversioned application tables are
|
|
// deliberately rejected, not adopted or converted. Production import and
|
|
// legacy schema reconciliation require a separate, verified migration plan.
|
|
func (r *Repository) InitializeFresh(ctx context.Context) error {
|
|
sqliteSteps, postgresSteps := r.migrationDefinitions()
|
|
if r.backend == PostgreSQL {
|
|
return db.ApplyPostgreSQLMigrations(ctx, r.database, "core-metadata", postgresSteps)
|
|
}
|
|
return db.ApplySQLiteMigrations(ctx, r.database, "core-metadata", sqliteSteps)
|
|
}
|
|
|
|
// Keep the exact released definitions shared by initialization and offline
|
|
// compatibility inspection. Neither inspection nor copying runs migrations.
|
|
func (r *Repository) migrationDefinitions() ([]db.SQLMigration, []db.PostgreSQLMigration) {
|
|
identity := "INTEGER PRIMARY KEY AUTOINCREMENT"
|
|
addressType := "TEXT"
|
|
if r.backend == PostgreSQL {
|
|
identity = "BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY"
|
|
// Path/asset identity must not depend on database locale or an ICU
|
|
// nondeterministic collation chosen by the database administrator.
|
|
addressType = `TEXT COLLATE "C"`
|
|
}
|
|
statements := []string{
|
|
`CREATE TABLE objects(uuid TEXT PRIMARY KEY,kind TEXT NOT NULL,created_at BIGINT NOT NULL)`,
|
|
`CREATE TABLE object_origins(object_uuid TEXT PRIMARY KEY REFERENCES objects(uuid),domain_uuid TEXT NOT NULL,created_at BIGINT NOT NULL)`,
|
|
`CREATE TABLE paths(domain_uuid TEXT NOT NULL,path ` + addressType + ` NOT NULL,object_uuid TEXT NOT NULL REFERENCES objects(uuid),created_at BIGINT NOT NULL,is_primary INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(domain_uuid,path))`,
|
|
`CREATE INDEX idx_paths_domain_object ON paths(domain_uuid,object_uuid)`,
|
|
`CREATE TABLE revisions(uuid TEXT PRIMARY KEY,object_uuid TEXT NOT NULL REFERENCES objects(uuid),document_hash TEXT NOT NULL,lang TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL DEFAULT '',message TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,created_at BIGINT NOT NULL,body TEXT NOT NULL DEFAULT '')`,
|
|
`CREATE INDEX idx_revisions_metadata_head ON revisions(object_uuid,lang,created_at_ms DESC,uuid DESC)`,
|
|
`CREATE TABLE assets(domain_uuid TEXT NOT NULL,address ` + addressType + ` NOT NULL,object_uuid TEXT NOT NULL UNIQUE REFERENCES objects(uuid),head_document_hash TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL DEFAULT '',size_bytes BIGINT NOT NULL CHECK(size_bytes>=0),created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,PRIMARY KEY(domain_uuid,address))`,
|
|
`CREATE TABLE asset_head_history(id ` + identity + `,domain_uuid TEXT NOT NULL,address TEXT NOT NULL,action TEXT NOT NULL,document_hash TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL,size_bytes BIGINT NOT NULL CHECK(size_bytes>=0),actor_uuid TEXT NOT NULL,created_at_ms BIGINT NOT NULL)`,
|
|
`CREATE INDEX idx_asset_metadata_history ON asset_head_history(domain_uuid,address,id)`,
|
|
}
|
|
// Minimal shape checks also run on subsequent initialization. Full catalog
|
|
// assertions for types, collation, constraints and indexes are a separate
|
|
// adoption gate before this schema can replace a legacy production store.
|
|
assertions := []string{
|
|
`SELECT uuid,kind,created_at FROM objects WHERE 1=0`,
|
|
`SELECT object_uuid,domain_uuid,created_at FROM object_origins WHERE 1=0`,
|
|
`SELECT domain_uuid,path,object_uuid,created_at,is_primary FROM paths WHERE 1=0`,
|
|
`SELECT ` + revisionColumns + `,created_at,body FROM revisions WHERE 1=0`,
|
|
`SELECT ` + assetColumns + ` FROM assets WHERE 1=0`,
|
|
`SELECT id,domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE 1=0`,
|
|
}
|
|
referenceStatements := []string{
|
|
`CREATE TABLE revision_document_refs(revision_uuid TEXT NOT NULL REFERENCES revisions(uuid),document_hash TEXT NOT NULL,PRIMARY KEY(revision_uuid,document_hash))`,
|
|
`CREATE INDEX idx_metadata_document_refs ON revision_document_refs(document_hash,revision_uuid)`,
|
|
`CREATE TABLE activity_outbox(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,sequence BIGINT NOT NULL,event_type TEXT NOT NULL,actor_uuid TEXT NOT NULL DEFAULT '',target_kind TEXT NOT NULL DEFAULT '',target_uuid TEXT NOT NULL DEFAULT '',payload TEXT NOT NULL DEFAULT '',idempotency_key TEXT NOT NULL,status TEXT NOT NULL DEFAULT 'pending',attempts INTEGER NOT NULL DEFAULT 0,next_attempt_ms BIGINT NOT NULL,last_error TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,claimed_at_ms BIGINT NOT NULL DEFAULT 0,UNIQUE(domain_uuid,sequence),UNIQUE(domain_uuid,idempotency_key))`,
|
|
`CREATE TABLE activity_outbox_heads(domain_uuid TEXT NOT NULL,event_type TEXT NOT NULL,last_sequence BIGINT NOT NULL,PRIMARY KEY(domain_uuid,event_type))`,
|
|
`CREATE INDEX idx_metadata_outbox_pending ON activity_outbox(status,next_attempt_ms,domain_uuid,sequence)`,
|
|
}
|
|
currentAssertions := append(append([]string(nil), assertions...),
|
|
`SELECT revision_uuid,document_hash FROM revision_document_refs WHERE 1=0`,
|
|
`SELECT uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,status,attempts,next_attempt_ms,last_error,created_at_ms,claimed_at_ms FROM activity_outbox WHERE 1=0`,
|
|
`SELECT domain_uuid,event_type,last_sequence FROM activity_outbox_heads WHERE 1=0`)
|
|
if r.backend == PostgreSQL {
|
|
return nil, []db.PostgreSQLMigration{
|
|
{Version: 1, Name: "initial portable core", Statements: statements, Assertions: assertions},
|
|
{Version: 2, Name: "revision references and outbox", Statements: referenceStatements, Assertions: currentAssertions},
|
|
}
|
|
}
|
|
return []db.SQLMigration{
|
|
{Version: 1, Name: "initial portable core", Statements: statements, Assertions: assertions},
|
|
{Version: 2, Name: "revision references and outbox", Statements: referenceStatements, Assertions: currentAssertions},
|
|
}, nil
|
|
}
|