forked from XuJiachengZust/codeAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_database.py
More file actions
105 lines (86 loc) · 3.3 KB
/
Copy pathclear_database.py
File metadata and controls
105 lines (86 loc) · 3.3 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
#!/usr/bin/env python3
"""
清空Neo4j数据库脚本
用于测试前清空数据库,确保测试环境干净
"""
import sys
from pathlib import Path
# 添加项目根目录到Python路径
sys.path.insert(0, str(Path(__file__).parent))
from database.neo4j_client import Neo4jClient
from utils.logger import LoggerSetup
from config.settings import Settings
def clear_database():
"""清空Neo4j数据库"""
print("=" * 60)
print("🗑️ 清空Neo4j数据库")
print("=" * 60)
# 初始化日志
logger = LoggerSetup.setup_logger("database_clear")
try:
# 创建Neo4j客户端
settings = Settings()
client = Neo4jClient(settings)
# 检查数据库连接
print("🔗 检查数据库连接...")
if not client.connect():
print("❌ 无法连接到Neo4j数据库")
return False
# 获取当前数据库统计信息
print("📊 获取当前数据库统计信息...")
stats = client.get_database_stats()
print(f" 项目数量: {stats.get('projects', 0)}")
print(f" 包数量: {stats.get('packages', 0)}")
print(f" 类数量: {stats.get('classes', 0)}")
print(f" 方法数量: {stats.get('methods', 0)}")
print(f" 字段数量: {stats.get('fields', 0)}")
print(f" 调用关系: {stats.get('calls', 0)}")
print(f" 包含关系: {stats.get('contains', 0)}")
# 确认清空操作
total_nodes = sum([
stats.get('projects', 0),
stats.get('packages', 0),
stats.get('classes', 0),
stats.get('methods', 0),
stats.get('fields', 0)
])
if total_nodes == 0:
print("✅ 数据库已经是空的,无需清空")
return True
print(f"\n⚠️ 警告: 即将删除数据库中的所有数据 ({total_nodes} 个节点)")
confirmation = input("❓ 确认要清空数据库吗?(输入 'yes' 继续): ")
if confirmation.lower() != 'yes':
print("❌ 操作已取消")
return False
# 执行清空操作
print("\n🧹 正在清空数据库...")
client.clear_database()
# 验证清空结果
print("✅ 数据库已清空")
# 再次获取统计信息确认
stats_after = client.get_database_stats()
remaining_nodes = sum([
stats_after.get('projects', 0),
stats_after.get('packages', 0),
stats_after.get('classes', 0),
stats_after.get('methods', 0),
stats_after.get('fields', 0)
])
if remaining_nodes == 0:
print("✅ 数据库清空成功,所有数据已删除")
else:
print(f"⚠️ 警告: 仍有 {remaining_nodes} 个节点未删除")
# 关闭连接
client.close()
return True
except Exception as e:
print(f"❌ 清空数据库失败: {e}")
logger.error(f"清空数据库失败: {e}")
return False
if __name__ == "__main__":
success = clear_database()
if success:
print("\n🎉 数据库清空操作完成")
else:
print("\n❌ 数据库清空操作失败")
sys.exit(1)