You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
|
3 months ago
|
"""测试数据库连接"""
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
|
||
|
|
# 添加项目根目录到路径
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
from app.repositories.database import init_db, engine
|
||
|
|
from sqlalchemy import text
|
||
|
|
|
||
|
|
print("=" * 50)
|
||
|
|
print("数据库连接测试")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
try:
|
||
|
|
# 测试连接
|
||
|
|
with engine.connect() as conn:
|
||
|
|
result = conn.execute(text("SELECT version()"))
|
||
|
|
version = result.scalar()
|
||
|
|
print(f"✅ 数据库连接成功")
|
||
|
|
print(f" PostgreSQL 版本: {version}")
|
||
|
|
|
||
|
|
# 初始化数据库(创建表)
|
||
|
|
print("\n正在初始化数据库表...")
|
||
|
|
init_db()
|
||
|
|
print("✅ 数据库表创建成功")
|
||
|
|
|
||
|
|
# 显示所有表
|
||
|
|
from sqlalchemy import inspect
|
||
|
|
inspector = inspect(engine)
|
||
|
|
tables = inspector.get_table_names()
|
||
|
|
print(f"\n已创建的表 ({len(tables)} 个):")
|
||
|
|
for table in sorted(tables):
|
||
|
|
print(f" - {table}")
|
||
|
|
|
||
|
|
print("\n" + "=" * 50)
|
||
|
|
print("数据库初始化完成!")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 错误: {e}")
|
||
|
|
print("\n请检查:")
|
||
|
|
print("1. Docker 是否已启动: docker ps")
|
||
|
|
print("2. 数据库端口是否正确: netstat -ano | findstr 5432")
|
||
|
|
print("3. 数据库密码是否正确")
|
||
|
|
sys.exit(1)
|