Overview
Concrete tool for additively reconciling a live SQLite database against an embedded reference schema provided by the internal/database package.
Description
reconcileSchema opens the embedded application.sql (via //go:embed) in an in-memory reference database, introspects its tables/columns/indexes, and compares against the live database. It creates missing tables first (so foreign-key references resolve), then adds missing columns via ALTER TABLE ADD COLUMN, then creates missing indexes. The entire operation runs in one transaction; on any failure it rolls back and the startup fails. It never drops, renames, or weakens constraints.
Usage
- Deployment: called by
database.NewDB → initTables on every startup to bring an old DB forward.
- Self-update restart: after an update, the same call reconciles any new tables/columns the new binary expects.
- First boot: against a non-existent DB file, all tables are created.
Code Reference
Source Location
- Repository: CyberStrikeAI
- File: internal/database/schema_reconcile.go
- Lines: reconcileSchema L66-130; helpers L27-60
Signature
//go:embed schemas/application.sql
var applicationSchema string
func (db *DB) reconcileSchema(schema string) error
// Called via initTables on NewDB:
func (db *DB) initTables() error // calls db.reconcileSchema(applicationSchema)
func (db *DB) EnsureKnowledgeSchema() error
Import
import "cyberstrike-ai/internal/database"
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| schema |
string |
Yes |
Embedded DDL (applicationSchema or knowledgeSchema)
|
| db |
*DB |
Yes |
Live SQLite connection
|
Outputs
| Name |
Type |
Description
|
| returns |
error |
Nil on success; non-nil rolls back the transaction
|
| Side effects |
Schema changes |
Missing tables/columns/indexes added
|
Usage Examples
Startup Reconciliation
db, err := database.NewDB("data/conversations.db", zapLogger)
if err != nil {
log.Fatal(err) // reconcileSchema failure surfaces here
}
// DB schema is now reconciled to the embedded application.sql
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.