Spaces:
Running
Running
| import asyncio | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from packages.core.config import settings | |
| from packages.core.models import Base | |
| from packages.core.olap import init_clickhouse_schema | |
| from packages.core.search import init_es_schema | |
| async def init_models(): | |
| print("Initializing PostgreSQL schema...") | |
| # 使用 PostgreSQL DSN | |
| engine = create_async_engine(settings.DATABASE_URL, echo=True) | |
| # 因为有 PostGIS 扩展依赖,最好确保扩展已被创建 | |
| # 正常 postgis image 默认在 public 下已有 postgis | |
| async with engine.begin() as conn: | |
| # 删除所有旧表(如果是全新项目,这里清理掉之前海关项目的旧表) | |
| await conn.run_sync(Base.metadata.drop_all) | |
| # 创建所有新表 | |
| await conn.run_sync(Base.metadata.create_all) | |
| print("数据库表结构初始化成功!") | |
| print("Initializing ClickHouse schema...") | |
| try: | |
| init_clickhouse_schema() | |
| except Exception as e: | |
| print(f"ClickHouse init failed (is it running?): {e}") | |
| print("Initializing Elasticsearch schema...") | |
| try: | |
| await init_es_schema() | |
| except Exception as e: | |
| print(f"Elasticsearch init failed (is it running?): {e}") | |
| if __name__ == "__main__": | |
| asyncio.run(init_models()) | |