You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.0 KiB
67 lines
2.0 KiB
# fix_database.py (updated version)
|
|
import os
|
|
import duckdb
|
|
from config import config
|
|
|
|
def fix_database():
|
|
"""Completely reset the database with correct schema"""
|
|
|
|
# Remove the existing database file completely
|
|
if os.path.exists(config.DATABASE_PATH):
|
|
os.remove(config.DATABASE_PATH)
|
|
print("Removed existing database file")
|
|
|
|
# Create directory if it doesn't exist
|
|
os.makedirs(os.path.dirname(config.DATABASE_PATH), exist_ok=True)
|
|
|
|
# Initialize with correct schema
|
|
conn = duckdb.connect(config.DATABASE_PATH)
|
|
|
|
# Create sequence for auto-incrementing IDs
|
|
conn.execute("CREATE SEQUENCE motions_id_seq START 1")
|
|
|
|
# Create motions table with sequence-based auto-increment
|
|
conn.execute("""
|
|
CREATE TABLE motions (
|
|
id INTEGER DEFAULT nextval('motions_id_seq'),
|
|
title TEXT NOT NULL,
|
|
description TEXT,
|
|
date DATE,
|
|
policy_area TEXT,
|
|
voting_results JSON,
|
|
winning_margin FLOAT,
|
|
controversy_score FLOAT,
|
|
layman_explanation TEXT,
|
|
url TEXT UNIQUE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (id)
|
|
)
|
|
""")
|
|
|
|
conn.execute("""
|
|
CREATE TABLE user_sessions (
|
|
session_id TEXT PRIMARY KEY,
|
|
user_votes JSON,
|
|
completed_motions INTEGER DEFAULT 0,
|
|
total_motions INTEGER DEFAULT 10,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
""")
|
|
|
|
conn.execute("""
|
|
CREATE TABLE party_results (
|
|
session_id TEXT,
|
|
party_name TEXT,
|
|
agreement_percentage FLOAT,
|
|
agreed_motions JSON,
|
|
disagreed_motions JSON,
|
|
PRIMARY KEY (session_id, party_name)
|
|
)
|
|
""")
|
|
|
|
conn.close()
|
|
print("Database recreated with correct schema using sequences")
|
|
|
|
if __name__ == "__main__":
|
|
fix_database()
|
|
|