-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.py
More file actions
92 lines (79 loc) · 2.54 KB
/
Copy pathsetup_database.py
File metadata and controls
92 lines (79 loc) · 2.54 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
#!/usr/bin/env python3
"""Setup database table automatically"""
import os
import sys
from dotenv import load_dotenv
import pymysql
load_dotenv()
DB_HOST = os.getenv('DB_HOST', 'localhost')
DB_PORT = int(os.getenv('DB_PORT', 3306))
DB_USER = os.getenv('DB_USER', 'root')
DB_PASSWORD = os.getenv('DB_PASSWORD', '')
DB_NAME = os.getenv('DB_NAME', 'securechat')
print("=" * 70)
print("Database Setup")
print("=" * 70)
print()
print(f"Database: {DB_NAME}")
print()
try:
connection = pymysql.connect(
host=DB_HOST,
port=DB_PORT,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
print("✓ Connected to database")
with connection.cursor() as cursor:
# Check if table exists
cursor.execute("SHOW TABLES LIKE 'users'")
table_exists = cursor.fetchone()
if table_exists:
print("✓ 'users' table already exists")
response = input("Do you want to recreate it? (y/n): ")
if response.lower() == 'y':
cursor.execute("DROP TABLE users")
print(" Dropped existing table")
else:
print(" Keeping existing table")
connection.close()
sys.exit(0)
# Create table
print("\nCreating 'users' table...")
create_table_sql = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
username VARCHAR(100) NOT NULL UNIQUE,
salt VARBINARY(16) NOT NULL,
pwd_hash CHAR(64) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_email (email),
INDEX idx_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""
cursor.execute(create_table_sql)
connection.commit()
print("✓ Table created successfully")
# Verify table structure
cursor.execute("DESCRIBE users")
columns = cursor.fetchall()
print("\nTable structure:")
for col in columns:
print(f" - {col['Field']}: {col['Type']}")
connection.close()
print()
print("=" * 70)
print("Database setup complete!")
print("=" * 70)
except pymysql.Error as e:
print(f"✗ Database error: {e}")
sys.exit(1)
except Exception as e:
print(f"✗ Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)