Initialize database schema.
(self)
| 57 | self._init_db() |
| 58 | |
| 59 | def _init_db(self) -> None: |
| 60 | """Initialize database schema.""" |
| 61 | conn = sqlite3.connect(self.db_path) |
| 62 | try: |
| 63 | cursor = conn.cursor() |
| 64 | |
| 65 | # Create base table structure (without new columns for backward compat) |
| 66 | cursor.execute( |
| 67 | """ |
| 68 | CREATE TABLE IF NOT EXISTS containers ( |
| 69 | container_id TEXT PRIMARY KEY, |
| 70 | container_name TEXT UNIQUE NOT NULL, |
| 71 | image_name TEXT NOT NULL, |
| 72 | owner_pid INTEGER NOT NULL, |
| 73 | created_at INTEGER NOT NULL |
| 74 | ) |
| 75 | """ |
| 76 | ) |
| 77 | |
| 78 | # Migrate existing tables: add config_hash and platform_type columns if missing |
| 79 | cursor.execute("PRAGMA table_info(containers)") |
| 80 | columns = [row[1] for row in cursor.fetchall()] |
| 81 | |
| 82 | if "config_hash" not in columns: |
| 83 | cursor.execute( |
| 84 | "ALTER TABLE containers ADD COLUMN config_hash TEXT DEFAULT ''" |
| 85 | ) |
| 86 | |
| 87 | if "platform_type" not in columns: |
| 88 | cursor.execute( |
| 89 | "ALTER TABLE containers ADD COLUMN platform_type TEXT DEFAULT ''" |
| 90 | ) |
| 91 | |
| 92 | # Create indices AFTER ensuring columns exist |
| 93 | cursor.execute( |
| 94 | "CREATE INDEX IF NOT EXISTS idx_owner_pid ON containers(owner_pid)" |
| 95 | ) |
| 96 | cursor.execute( |
| 97 | "CREATE INDEX IF NOT EXISTS idx_container_name ON containers(container_name)" |
| 98 | ) |
| 99 | cursor.execute( |
| 100 | "CREATE INDEX IF NOT EXISTS idx_config_hash ON containers(config_hash)" |
| 101 | ) |
| 102 | cursor.execute( |
| 103 | "CREATE INDEX IF NOT EXISTS idx_platform_type ON containers(platform_type)" |
| 104 | ) |
| 105 | |
| 106 | conn.commit() |
| 107 | finally: |
| 108 | conn.close() |
| 109 | |
| 110 | def _get_connection(self) -> sqlite3.Connection: |
| 111 | """Get a new database connection.""" |