43 lines
2.0 KiB
Python
43 lines
2.0 KiB
Python
|
|
import dataset
|
||
|
|
import logging
|
||
|
|
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)")
|
||
|
|
logger.info("Database initialized")
|
||
|
|
|
||
|
|
|
||
|
|
def get_table(name):
|
||
|
|
return db[name]
|