-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
53 lines (41 loc) · 1.7 KB
/
init_db.py
File metadata and controls
53 lines (41 loc) · 1.7 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
"""
Database initialization script for the 0xC Chat application.
This script creates the database tables and optionally adds some sample data.
"""
import os
from app import create_app
from database import db
from models import User, Message
def init_db(with_sample_data=False):
"""Initialize the database and optionally add sample data."""
app = create_app()
with app.app_context():
# Create all tables
db.create_all()
print("Database tables created successfully.")
# Add sample data if requested
if with_sample_data:
add_sample_data()
print("Sample data added successfully.")
def add_sample_data():
"""Add sample users and messages to the database."""
# Check if we already have users
if db.session.query(User).count() > 0:
print("Sample data already exists. Skipping.")
return
# Create sample users
admin = User.register("admin", "admin123", "admin@example.com")
user1 = User.register("user1", "password123", "user1@example.com")
user2 = User.register("user2", "password123", "user2@example.com")
# Create sample messages
Message.add(admin.id, "Welcome to 0xC Chat!")
Message.add(user1.id, "Hello, world!")
Message.add(user2.id, "This is a test message.")
Message.add(admin.id, "Feel free to explore the API.")
print(f"Created {db.session.query(User).count()} sample users.")
print(f"Created {db.session.query(Message).count()} sample messages.")
if __name__ == "__main__":
# Check if we should add sample data
sample_data = os.environ.get("SAMPLE_DATA", "0") == "1"
# Initialize the database
init_db(with_sample_data=sample_data)