MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / initialize

Method initialize

src/storage/psql_manager.py:42–72  ·  view source on GitHub ↗

初始化 PostgreSQL 数据库

(self)

Source from the content-addressed store, hash-verified

40 self._config_loaded = False
41
42 async def initialize(self) -> None:
43 """初始化 PostgreSQL 数据库"""
44 if self._initialized:
45 return
46
47 async with self._lock:
48 if self._initialized:
49 return
50
51 try:
52 self._dsn = os.getenv("POSTGRESQL_URI", "")
53 if not self._dsn:
54 raise RuntimeError("POSTGRESQL_URI environment variable is not set")
55
56 self._pool = await asyncpg.create_pool(self._dsn, min_size=2, max_size=10)
57
58 async with self._pool.acquire() as conn:
59 await self._create_tables(conn)
60 await self._ensure_schema_compatibility(conn)
61
62 await self._load_config_cache()
63
64 self._initialized = True
65 log.info("PostgreSQL storage initialized")
66
67 except Exception as e:
68 log.error(f"Error initializing PostgreSQL: {e}")
69 if self._pool:
70 await self._pool.close()
71 self._pool = None
72 raise
73
74 async def _create_tables(self, conn: asyncpg.Connection) -> None:
75 """创建数据库表和索引"""

Callers

nothing calls this directly

Calls 6

_create_tablesMethod · 0.95
_load_config_cacheMethod · 0.95
infoMethod · 0.80
errorMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected