Manages container lifecycle tracking database.
| 39 | |
| 40 | |
| 41 | class ContainerDatabase: |
| 42 | """Manages container lifecycle tracking database.""" |
| 43 | |
| 44 | def __init__(self, db_path: Optional[Path] = None): |
| 45 | """Initialize database connection. |
| 46 | |
| 47 | Args: |
| 48 | db_path: Path to database file. Defaults to ~/.fastled/docker-compiler/compiler.db |
| 49 | """ |
| 50 | if db_path is None: |
| 51 | home = Path.home() |
| 52 | db_dir = home / ".fastled" / "docker-compiler" |
| 53 | db_dir.mkdir(parents=True, exist_ok=True) |
| 54 | db_path = db_dir / "compiler.db" |
| 55 | |
| 56 | self.db_path = db_path |
| 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 | ) |
no outgoing calls