51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to upgrade an existing database with the new user_sentiments table.
|
|
"""
|
|
|
|
import sqlite3
|
|
import os
|
|
|
|
def upgrade_database():
|
|
db_path = 'jokes.db'
|
|
|
|
if not os.path.exists(db_path):
|
|
print(f"❌ Database {db_path} does not exist!")
|
|
print("Please run database.py first to create the database.")
|
|
return False
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# Check if user_sentiments table already exists
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_sentiments';")
|
|
table_exists = cursor.fetchone()
|
|
|
|
if table_exists:
|
|
print("✅ Database is already up to date!")
|
|
conn.close()
|
|
return True
|
|
|
|
print("🔄 Upgrading database schema...")
|
|
|
|
# Create the user_sentiments table
|
|
cursor.execute('''CREATE TABLE user_sentiments (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
joke_id INTEGER NOT NULL,
|
|
user_sentiment TEXT CHECK(user_sentiment IN ('up', 'down', 'neutral')) DEFAULT 'neutral',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (joke_id) REFERENCES jokes(id) ON DELETE CASCADE
|
|
)''')
|
|
|
|
conn.commit()
|
|
print("✅ Database upgraded successfully! Added user_sentiments table.")
|
|
conn.close()
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
print("🔄 Checking database schema...")
|
|
success = upgrade_database()
|
|
if success:
|
|
print("\n🎉 Database is ready for the enhanced application!")
|
|
else:
|
|
print("\n💥 Failed to upgrade the database.") |