initial arhitectural overhaul
Signed-off-by: Sienna Meridian Satterwhite <sienna@r3t.io>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
-- Migration 001: Initial schema
|
||||
-- Creates the base tables for entity persistence and CRDT sync
|
||||
|
||||
-- Entities table - stores entity metadata
|
||||
CREATE TABLE IF NOT EXISTS entities (
|
||||
id BLOB PRIMARY KEY,
|
||||
entity_type TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Components table - stores serialized component data
|
||||
CREATE TABLE IF NOT EXISTS components (
|
||||
entity_id BLOB NOT NULL,
|
||||
component_type TEXT NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (entity_id, component_type),
|
||||
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Index for querying components by entity
|
||||
CREATE INDEX IF NOT EXISTS idx_components_entity
|
||||
ON components(entity_id);
|
||||
|
||||
-- Operation log - for CRDT sync protocol
|
||||
CREATE TABLE IF NOT EXISTS operation_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
node_id TEXT NOT NULL,
|
||||
sequence_number INTEGER NOT NULL,
|
||||
operation BLOB NOT NULL,
|
||||
timestamp INTEGER NOT NULL,
|
||||
UNIQUE(node_id, sequence_number)
|
||||
);
|
||||
|
||||
-- Index for efficient operation log queries
|
||||
CREATE INDEX IF NOT EXISTS idx_oplog_node_seq
|
||||
ON operation_log(node_id, sequence_number);
|
||||
|
||||
-- Vector clock table - for causality tracking
|
||||
CREATE TABLE IF NOT EXISTS vector_clock (
|
||||
node_id TEXT PRIMARY KEY,
|
||||
counter INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Session state table - for crash detection
|
||||
CREATE TABLE IF NOT EXISTS session_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- WAL checkpoint tracking
|
||||
CREATE TABLE IF NOT EXISTS checkpoint_state (
|
||||
last_checkpoint INTEGER NOT NULL,
|
||||
wal_size_bytes INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Initialize checkpoint state if not exists
|
||||
INSERT OR IGNORE INTO checkpoint_state (rowid, last_checkpoint, wal_size_bytes)
|
||||
VALUES (1, strftime('%s', 'now'), 0);
|
||||
@@ -0,0 +1,51 @@
|
||||
-- Migration 004: Add session support
|
||||
-- Adds session tables and session-scopes existing tables
|
||||
|
||||
-- Sessions table
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id BLOB PRIMARY KEY,
|
||||
code TEXT NOT NULL,
|
||||
name TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
last_active INTEGER NOT NULL,
|
||||
entity_count INTEGER NOT NULL DEFAULT 0,
|
||||
state TEXT NOT NULL,
|
||||
secret BLOB,
|
||||
UNIQUE(id),
|
||||
UNIQUE(code)
|
||||
);
|
||||
|
||||
-- Index for finding recent sessions
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_last_active
|
||||
ON sessions(last_active DESC);
|
||||
|
||||
-- Session membership (which node was in which session)
|
||||
CREATE TABLE IF NOT EXISTS session_membership (
|
||||
session_id BLOB NOT NULL,
|
||||
node_id TEXT NOT NULL,
|
||||
joined_at INTEGER NOT NULL,
|
||||
left_at INTEGER,
|
||||
PRIMARY KEY (session_id, node_id),
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Add session_id to entities table
|
||||
ALTER TABLE entities ADD COLUMN session_id BLOB;
|
||||
|
||||
-- Index for session-scoped entity queries
|
||||
CREATE INDEX IF NOT EXISTS idx_entities_session
|
||||
ON entities(session_id);
|
||||
|
||||
-- Add session_id to vector_clock
|
||||
ALTER TABLE vector_clock ADD COLUMN session_id BLOB;
|
||||
|
||||
-- Composite index for session + node lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_vector_clock_session_node
|
||||
ON vector_clock(session_id, node_id);
|
||||
|
||||
-- Add session_id to operation_log
|
||||
ALTER TABLE operation_log ADD COLUMN session_id BLOB;
|
||||
|
||||
-- Index for session-scoped operation queries
|
||||
CREATE INDEX IF NOT EXISTS idx_operation_log_session
|
||||
ON operation_log(session_id, node_id, sequence_number);
|
||||
Reference in New Issue
Block a user