-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_database.py
More file actions
137 lines (111 loc) Β· 4.55 KB
/
Copy pathinit_database.py
File metadata and controls
137 lines (111 loc) Β· 4.55 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
#!/usr/bin/env python3
"""
Database Initialization Script for AntiGravity Fraud Detection System
Creates the basic database schema for the minimal backend
"""
import os
import psycopg2
from psycopg2.extras import execute_values
def init_database():
"""Initialize the database with basic schema"""
# Database connection parameters
db_params = {
'host': 'simple378-db-1',
'database': 'fraud_detection',
'user': 'fraud_admin',
'password': 'secure_fraud_db_password_2024_strong',
'port': '5432'
}
try:
print("π Connecting to database...")
conn = psycopg2.connect(**db_params)
conn.autocommit = True
cursor = conn.cursor()
print("β
Connected successfully")
# Create basic tables
print("π Creating database schema...")
# Cases table
cursor.execute("""
CREATE TABLE IF NOT EXISTS cases (
id VARCHAR(50) PRIMARY KEY,
subject_name VARCHAR(255) NOT NULL,
status VARCHAR(50) DEFAULT 'pending',
risk_score INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
assigned_to VARCHAR(255)
);
""")
# Case details table
cursor.execute("""
CREATE TABLE IF NOT EXISTS case_details (
id SERIAL PRIMARY KEY,
case_id VARCHAR(50) REFERENCES cases(id),
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
# Users table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(50) PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
role VARCHAR(50) DEFAULT 'analyst',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
print("β
Tables created successfully")
# Insert sample data
print("π Inserting sample data...")
# Sample cases
cases_data = [
('case-001', 'John Doe', 'active', 75, '2024-01-01T00:00:00Z', '2024-01-01T00:00:00Z', 'analyst1'),
('case-002', 'Jane Smith', 'pending', 45, '2024-01-02T00:00:00Z', '2024-01-02T00:00:00Z', 'analyst2'),
('case-003', 'Bob Johnson', 'closed', 25, '2024-01-03T00:00:00Z', '2024-01-03T00:00:00Z', 'analyst1'),
]
execute_values(cursor,
"INSERT INTO cases (id, subject_name, status, risk_score, created_at, updated_at, assigned_to) VALUES %s ON CONFLICT (id) DO NOTHING",
cases_data
)
# Sample users
users_data = [
('user-001', 'test@example.com', 'Test User', 'analyst'),
('user-002', 'admin@example.com', 'Admin User', 'admin'),
]
execute_values(cursor,
"INSERT INTO users (id, email, name, role) VALUES %s ON CONFLICT (id) DO NOTHING",
users_data
)
# Sample case details
cursor.execute("""
INSERT INTO case_details (case_id, description) VALUES
('case-001', 'High-risk transaction pattern detected'),
('case-002', 'Suspicious account activity under review'),
('case-003', 'Case resolved - no fraud detected')
ON CONFLICT DO NOTHING
""")
print("β
Sample data inserted")
# Create indexes for performance
print("π Creating indexes...")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_cases_status ON cases(status);")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_cases_risk_score ON cases(risk_score);")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_cases_created_at ON cases(created_at);")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);")
print("β
Indexes created")
# Verify data
cursor.execute("SELECT COUNT(*) FROM cases;")
case_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM users;")
user_count = cursor.fetchone()[0]
print(f"π Database initialized: {case_count} cases, {user_count} users")
cursor.close()
conn.close()
print("π Database initialization complete!")
return True
except Exception as e:
print(f"β Database initialization failed: {e}")
return False
if __name__ == "__main__":
success = init_database()
exit(0 if success else 1)