-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_database_connection.py
More file actions
338 lines (268 loc) · 11.2 KB
/
test_database_connection.py
File metadata and controls
338 lines (268 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
测试数据库连接和基本功能
"""
import sys
import os
import time
from datetime import datetime
# 添加项目根目录到Python路径
sys.path.insert(0, '/workspaces/vespera')
def test_postgresql_connection():
"""测试PostgreSQL连接"""
print("="*60)
print("测试PostgreSQL连接")
print("="*60)
try:
import psycopg2
from sqlalchemy import create_engine, text
# 从环境变量读取配置
from dotenv import load_dotenv
load_dotenv()
host = os.getenv('POSTGRES_HOST', 'localhost')
port = os.getenv('POSTGRES_PORT', '5432')
database = os.getenv('POSTGRES_DB', 'qiming_star')
user = os.getenv('POSTGRES_USER', 'qiming_user')
password = os.getenv('POSTGRES_PASSWORD', 'qiming_pass_2024')
# 构建连接字符串
connection_string = f"postgresql://{user}:{password}@{host}:{port}/{database}"
print(f"连接到: {host}:{port}/{database}")
# 等待数据库启动
max_retries = 30
for i in range(max_retries):
try:
engine = create_engine(connection_string)
with engine.connect() as conn:
result = conn.execute(text("SELECT version()"))
version = result.fetchone()[0]
print(f"✅ PostgreSQL连接成功")
print(f" 版本: {version}")
# 测试基本操作
conn.execute(text("CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name VARCHAR(50))"))
conn.execute(text("INSERT INTO test_table (name) VALUES ('test') ON CONFLICT DO NOTHING"))
result = conn.execute(text("SELECT COUNT(*) FROM test_table"))
count = result.fetchone()[0]
print(f" 测试表记录数: {count}")
conn.commit()
return True
except Exception as e:
if i < max_retries - 1:
print(f" 等待PostgreSQL启动... ({i+1}/{max_retries})")
time.sleep(2)
else:
print(f"❌ PostgreSQL连接失败: {e}")
return False
except Exception as e:
print(f"❌ PostgreSQL测试失败: {e}")
return False
def test_redis_connection():
"""测试Redis连接"""
print("\n" + "="*60)
print("测试Redis连接")
print("="*60)
try:
import redis
# 从环境变量读取配置
host = os.getenv('REDIS_HOST', 'localhost')
port = int(os.getenv('REDIS_PORT', '6379'))
password = os.getenv('REDIS_PASSWORD', 'qiming_redis_2024')
print(f"连接到: {host}:{port}")
# 等待Redis启动
max_retries = 15
for i in range(max_retries):
try:
r = redis.Redis(host=host, port=port, password=password, decode_responses=True)
# 测试连接
r.ping()
print("✅ Redis连接成功")
# 测试基本操作
r.set('test_key', 'test_value')
value = r.get('test_key')
print(f" 测试读写: {value}")
# 获取Redis信息
info = r.info()
print(f" Redis版本: {info.get('redis_version', 'unknown')}")
print(f" 内存使用: {info.get('used_memory_human', 'unknown')}")
return True
except Exception as e:
if i < max_retries - 1:
print(f" 等待Redis启动... ({i+1}/{max_retries})")
time.sleep(2)
else:
print(f"❌ Redis连接失败: {e}")
return False
except Exception as e:
print(f"❌ Redis测试失败: {e}")
return False
def test_clickhouse_connection():
"""测试ClickHouse连接"""
print("\n" + "="*60)
print("测试ClickHouse连接")
print("="*60)
try:
import requests
# 从环境变量读取配置
host = os.getenv('CLICKHOUSE_HOST', 'localhost')
port = os.getenv('CLICKHOUSE_PORT', '8123')
print(f"连接到: {host}:{port}")
# 等待ClickHouse启动
max_retries = 30
for i in range(max_retries):
try:
# 测试HTTP接口
response = requests.get(f"http://{host}:{port}/ping", timeout=5)
if response.status_code == 200:
print("✅ ClickHouse连接成功")
# 测试查询
query_response = requests.post(
f"http://{host}:{port}/",
data="SELECT version()",
timeout=5
)
if query_response.status_code == 200:
version = query_response.text.strip()
print(f" 版本: {version}")
return True
else:
raise Exception(f"HTTP状态码: {response.status_code}")
except Exception as e:
if i < max_retries - 1:
print(f" 等待ClickHouse启动... ({i+1}/{max_retries})")
time.sleep(3)
else:
print(f"❌ ClickHouse连接失败: {e}")
return False
except Exception as e:
print(f"❌ ClickHouse测试失败: {e}")
return False
def test_database_optimization():
"""测试数据库优化功能"""
print("\n" + "="*60)
print("测试数据库优化功能")
print("="*60)
try:
# 检查优化脚本是否存在
optimizer_script = "/workspaces/vespera/scripts/optimize_database_indexes.py"
sql_script = "/workspaces/vespera/sql/create_optimized_indexes.sql"
if os.path.exists(optimizer_script):
print("✅ 数据库优化脚本存在")
else:
print("❌ 数据库优化脚本不存在")
return False
if os.path.exists(sql_script):
print("✅ SQL优化脚本存在")
# 读取SQL脚本内容
with open(sql_script, 'r', encoding='utf-8') as f:
sql_content = f.read()
# 统计索引数量
index_count = sql_content.count('CREATE INDEX')
print(f" 包含 {index_count} 个索引定义")
else:
print("❌ SQL优化脚本不存在")
return False
return True
except Exception as e:
print(f"❌ 数据库优化测试失败: {e}")
return False
def create_database_test_script():
"""创建数据库测试脚本"""
print("\n" + "="*60)
print("创建数据库测试脚本")
print("="*60)
test_script = '''
import asyncio
import sys
import os
sys.path.insert(0, '/workspaces/vespera')
async def test_database_operations():
"""测试数据库操作"""
from sqlalchemy import create_engine, text
from dotenv import load_dotenv
load_dotenv()
# PostgreSQL连接
host = os.getenv('POSTGRES_HOST', 'localhost')
port = os.getenv('POSTGRES_PORT', '5432')
database = os.getenv('POSTGRES_DB', 'qiming_star')
user = os.getenv('POSTGRES_USER', 'qiming_user')
password = os.getenv('POSTGRES_PASSWORD', 'qiming_pass_2024')
connection_string = f"postgresql://{user}:{password}@{host}:{port}/{database}"
try:
engine = create_engine(connection_string)
with engine.connect() as conn:
# 创建示例表
conn.execute(text("""
CREATE TABLE IF NOT EXISTS sample_stocks (
id SERIAL PRIMARY KEY,
ts_code VARCHAR(20) NOT NULL,
name VARCHAR(100),
industry VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 插入示例数据
conn.execute(text("""
INSERT INTO sample_stocks (ts_code, name, industry)
VALUES
('000001.SZ', '平安银行', '银行'),
('000002.SZ', '万科A', '房地产'),
('600000.SH', '浦发银行', '银行')
ON CONFLICT DO NOTHING
"""))
# 查询数据
result = conn.execute(text("SELECT * FROM sample_stocks LIMIT 5"))
rows = result.fetchall()
print("示例股票数据:")
for row in rows:
print(f" {row.ts_code}: {row.name} ({row.industry})")
conn.commit()
print("✅ 数据库操作测试成功")
except Exception as e:
print(f"❌ 数据库操作测试失败: {e}")
if __name__ == "__main__":
asyncio.run(test_database_operations())
'''
script_path = "/workspaces/vespera/test_database_operations.py"
with open(script_path, 'w', encoding='utf-8') as f:
f.write(test_script)
print(f"✅ 数据库测试脚本已创建: {script_path}")
return script_path
def main():
"""主测试函数"""
print("🗄️ Vespera 数据库连接测试")
print(f"测试时间: {datetime.now()}")
# 等待Docker服务启动
print("\n⏳ 等待Docker服务启动...")
time.sleep(10)
test_results = {}
# 执行数据库连接测试
test_results["postgresql"] = test_postgresql_connection()
test_results["redis"] = test_redis_connection()
test_results["clickhouse"] = test_clickhouse_connection()
test_results["optimization"] = test_database_optimization()
# 创建测试脚本
script_path = create_database_test_script()
# 统计结果
total_tests = len(test_results)
passed_tests = sum(1 for result in test_results.values() if result)
success_rate = passed_tests / total_tests
print("\n" + "="*60)
print("数据库测试结果摘要")
print("="*60)
print(f"总测试数: {total_tests}")
print(f"通过测试: {passed_tests}")
print(f"成功率: {success_rate:.1%}")
if success_rate >= 0.75:
print("🎉 数据库连接正常!")
print("\n📋 数据库使用指南:")
print("1. 运行数据库操作测试:")
print(f" python {script_path}")
print("\n2. 执行数据库优化:")
print(" python scripts/optimize_database_indexes.py")
print("\n3. 查看数据库状态:")
print(" docker-compose ps")
print(" docker-compose logs postgres")
else:
print("⚠️ 部分数据库连接存在问题")
print("建议检查Docker服务状态和配置")
return test_results
if __name__ == "__main__":
main()