MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / check_and_migrate_db

Method check_and_migrate_db

src/core/database.py:319–606  ·  view source on GitHub ↗

Check database integrity and perform migrations if needed This method is called during upgrade mode to: 1. Create missing tables (if they don't exist) 2. Add missing columns to existing tables 3. Ensure all config tables have default rows Args: c

(self, config_dict: dict = None)

Source from the content-addressed store, hash-verified

317 """)
318
319 async def check_and_migrate_db(self, config_dict: dict = None):
320 """Check database integrity and perform migrations if needed
321
322 This method is called during upgrade mode to:
323 1. Create missing tables (if they don't exist)
324 2. Add missing columns to existing tables
325 3. Ensure all config tables have default rows
326
327 Args:
328 config_dict: Configuration dictionary from setting.toml (optional)
329 Used only to initialize missing config rows with default values.
330 Existing config rows will NOT be overwritten.
331 """
332 async with self._connect(write=True) as db:
333 print("Checking database integrity and performing migrations...")
334 await db.execute("PRAGMA journal_mode = WAL")
335 await db.execute("PRAGMA synchronous = NORMAL")
336
337 # ========== Step 1: Create missing tables ==========
338 # Check and create cache_config table if missing
339 if not await self._table_exists(db, "cache_config"):
340 print(" ✓ Creating missing table: cache_config")
341 await db.execute("""
342 CREATE TABLE cache_config (
343 id INTEGER PRIMARY KEY DEFAULT 1,
344 cache_enabled BOOLEAN DEFAULT 0,
345 cache_timeout INTEGER DEFAULT 7200,
346 cache_base_url TEXT,
347 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
348 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
349 )
350 """)
351
352 # Check and create proxy_config table if missing
353 if not await self._table_exists(db, "proxy_config"):
354 print(" ✓ Creating missing table: proxy_config")
355 await db.execute("""
356 CREATE TABLE proxy_config (
357 id INTEGER PRIMARY KEY DEFAULT 1,
358 enabled BOOLEAN DEFAULT 0,
359 proxy_url TEXT,
360 media_proxy_enabled BOOLEAN DEFAULT 0,
361 media_proxy_url TEXT,
362 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
363 )
364 """)
365
366 # Check and create call_logic_config table if missing
367 if not await self._table_exists(db, "call_logic_config"):
368 print(" Creating missing table: call_logic_config")
369 await db.execute("""
370 CREATE TABLE call_logic_config (
371 id INTEGER PRIMARY KEY DEFAULT 1,
372 call_mode TEXT DEFAULT 'default',
373 polling_mode_enabled BOOLEAN DEFAULT 0,
374 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
375 )
376 """)

Callers 1

lifespanFunction · 0.80

Calls 4

_connectMethod · 0.95
_table_existsMethod · 0.95
_column_existsMethod · 0.95
_ensure_config_rowsMethod · 0.95

Tested by

no test coverage detected