/ db / migrations / 039-create-settings-table.sql
039-create-settings-table.sql
 1  -- Migration 039: Create settings table for runtime-changeable configuration
 2  --
 3  -- Context:
 4  --   The circuit breaker needs to be dynamically toggleable via dashboard without
 5  --   restarting services. Static config (sender details, API keys) stays in .env,
 6  --   but operational controls like circuit breaker need database storage.
 7  --
 8  -- Changes:
 9  --   1. Create settings table for runtime-changeable configuration
10  --   2. Seed with circuit breaker setting (migrated from old config table backup)
11  --
12  -- Note:
13  --   Score cutoff (LOW_SCORE_CUTOFF) remains in .env only - it's a business rule
14  --   that doesn't need frequent changes. Circuit breaker is an operational control.
15  
16  -- Create settings table for runtime-changeable configuration
17  CREATE TABLE IF NOT EXISTS settings (
18      key TEXT PRIMARY KEY,
19      value TEXT NOT NULL,
20      description TEXT,
21      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
22  );
23  
24  -- Seed with circuit breaker setting
25  -- Try to migrate from old config backup, otherwise use default 'true'
26  -- Normalize 1/0 values to 'true'/'false' for clarity
27  INSERT INTO settings (key, value, description)
28  SELECT
29      'cron_circuit_breaker_enabled',
30      CASE
31          WHEN (SELECT value FROM config_backup_20260210 WHERE key = 'cron_circuit_breaker_enabled') IN ('0', 'false') THEN 'false'
32          ELSE 'true'
33      END,
34      'Global kill switch for all cron jobs (true=enabled, false=disabled)'
35  WHERE NOT EXISTS (SELECT 1 FROM settings WHERE key = 'cron_circuit_breaker_enabled');
36  
37  -- Migration complete
38  -- Verify with: sqlite3 db/sites.db "SELECT * FROM settings"