Files
Momentum/db.py
2026-02-09 17:26:00 -06:00

53 lines
1.4 KiB
Python

import mysql.connector
import os
def get_db():
return mysql.connector.connect(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE"),
autocommit=True
)
def init_db():
db = get_db()
cur = db.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
discord_id VARCHAR(32) PRIMARY KEY,
val_tag VARCHAR(32) NOT NULL,
rank VARCHAR(32) NOT NULL,
rr INT DEFAULT 0,
kills INT DEFAULT 0,
deaths INT DEFAULT 0,
calories_burned DECIMAL(10, 2) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
# Add calories_burned column if it doesn't exist
try:
cur.execute("""
ALTER TABLE users ADD COLUMN calories_burned DECIMAL(10, 2) DEFAULT 0
""")
except:
pass # Column already exists
cur.execute("""
CREATE TABLE IF NOT EXISTS workouts (
discord_id VARCHAR(32),
workout VARCHAR(32),
amount INT DEFAULT 0,
PRIMARY KEY (discord_id, workout),
FOREIGN KEY (discord_id)
REFERENCES users(discord_id)
ON DELETE CASCADE
)
""")
cur.close()
db.close()
print("✅ Database ready")