-- ============================================================================ -- Script Logging Table -- ============================================================================ -- Persistent log for import scripts and library operations. -- Provides queryable event log without cluttering CLI output. -- -- Usage: -- session = db.log_session("import_ohlc.py") -- session.info("Starting import for AAPL") -- session.warning("Missing data for 2024-01-15") -- session.error("Failed to parse file", details={"file": "bad.csv"}) -- ============================================================================ -- Create script_log table in log schema CREATE TABLE IF NOT EXISTS log.script_log ( id SERIAL PRIMARY KEY, timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(), level VARCHAR(10) NOT NULL DEFAULT 'INFO' CHECK (level IN ('DEBUG', 'INFO', 'WARNING', 'ERROR')), message TEXT NOT NULL, script_name VARCHAR(100), series_name VARCHAR(255), session_id UUID, details JSONB ); -- Indexes for common query patterns CREATE INDEX IF NOT EXISTS idx_script_log_timestamp ON log.script_log(timestamp DESC); CREATE INDEX IF NOT EXISTS idx_script_log_session ON log.script_log(session_id) WHERE session_id IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_script_log_series ON log.script_log(series_name) WHERE series_name IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_script_log_level ON log.script_log(level); -- View for recent logs (last 1000) CREATE OR REPLACE VIEW log.script_log_recent AS SELECT * FROM log.script_log ORDER BY timestamp DESC LIMIT 1000; -- Table and column comments COMMENT ON TABLE log.script_log IS 'Persistent log for import scripts and library operations'; COMMENT ON COLUMN log.script_log.level IS 'Log level: DEBUG, INFO, WARNING, ERROR'; COMMENT ON COLUMN log.script_log.session_id IS 'Groups logs from a single script run (UUID)'; COMMENT ON COLUMN log.script_log.script_name IS 'Name of the script or operation (e.g., import_ohlc.py)'; COMMENT ON COLUMN log.script_log.series_name IS 'Optional: link to specific series'; COMMENT ON COLUMN log.script_log.details IS 'Optional structured data stored as JSONB';