2026-05-10 09:08:12 +02:00
|
|
|
import dataset
|
|
|
|
|
import logging
|
2026-05-11 00:41:41 +02:00
|
|
|
from datetime import datetime
|
2026-05-10 09:08:12 +02:00
|
|
|
from devplacepy.config import DATABASE_URL
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
db = dataset.connect(DATABASE_URL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_db():
|
|
|
|
|
tables = db.tables
|
|
|
|
|
if "users" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_users_username ON users (username)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_users_email ON users (email)")
|
|
|
|
|
if "posts" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_posts_user_uid ON posts (user_uid)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_posts_created_at ON posts (created_at)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_posts_topic ON posts (topic)")
|
|
|
|
|
if "comments" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_comments_post_uid ON comments (post_uid)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_comments_user_uid ON comments (user_uid)")
|
|
|
|
|
if "votes" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_votes_target ON votes (target_uid, target_type)")
|
|
|
|
|
if "messages" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_messages_sender ON messages (sender_uid)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_messages_receiver ON messages (receiver_uid)")
|
|
|
|
|
if "notifications" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_notifications_user ON notifications (user_uid)")
|
|
|
|
|
if "sessions" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions (session_token)")
|
|
|
|
|
if "projects" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_projects_user ON projects (user_uid)")
|
|
|
|
|
if "badges" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_badges_user ON badges (user_uid)")
|
|
|
|
|
if "follows" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_follows_follower ON follows (follower_uid)")
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_follows_following ON follows (following_uid)")
|
2026-05-11 00:41:41 +02:00
|
|
|
if "daily_topics" in tables:
|
|
|
|
|
existing = db["daily_topics"].find_one()
|
|
|
|
|
if not existing:
|
|
|
|
|
db["daily_topics"].insert({
|
|
|
|
|
"uid": "default",
|
|
|
|
|
"title": "Anthropic details how it improved Claude's safety...",
|
|
|
|
|
"summary": "New techniques in AI safety research show promising results for alignment of large language models with human values.",
|
|
|
|
|
"updated_at": datetime.utcnow().isoformat(),
|
|
|
|
|
})
|
|
|
|
|
if "password_resets" in tables:
|
|
|
|
|
db.query("CREATE INDEX IF NOT EXISTS idx_password_resets_token ON password_resets (token)")
|
2026-05-10 09:08:12 +02:00
|
|
|
logger.info("Database initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_table(name):
|
|
|
|
|
return db[name]
|
2026-05-11 00:41:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_daily_topic():
|
|
|
|
|
topics = db["daily_topics"]
|
|
|
|
|
topic = topics.find_one()
|
|
|
|
|
if topic:
|
|
|
|
|
return topic
|
|
|
|
|
return {"title": "Anthropic details how it improved Claude's safety...", "summary": "New techniques in AI safety research show promising results."}
|