初始化 PostgreSQL 数据库
(self)
| 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 | """创建数据库表和索引""" |
nothing calls this directly
no test coverage detected