import os import sys from pathlib import Path # Add the parent directory to Python path so we can import from root sys.path.append(str(Path(__file__).parent.parent)) from flask import Flask from flask_sqlalchemy import SQLAlchemy from extensions import db from sqlalchemy import text def upgrade(): # Create notifs table with db.engine.connect() as conn: conn.execute(text(''' CREATE TABLE IF NOT EXISTS notifs ( id SERIAL PRIMARY KEY, notif_type VARCHAR(50) NOT NULL, user_id INTEGER NOT NULL REFERENCES "user" (id), sender_id INTEGER REFERENCES "user" (id), timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, read BOOLEAN NOT NULL DEFAULT FALSE, details JSONB ); -- Create indexes for faster queries CREATE INDEX IF NOT EXISTS idx_notifs_notif_type ON notifs(notif_type); CREATE INDEX IF NOT EXISTS idx_notifs_timestamp ON notifs(timestamp); CREATE INDEX IF NOT EXISTS idx_notifs_user_id ON notifs(user_id); CREATE INDEX IF NOT EXISTS idx_notifs_sender_id ON notifs(sender_id); CREATE INDEX IF NOT EXISTS idx_notifs_read ON notifs(read); ''')) conn.commit() def downgrade(): # Drop notifs table and its indexes with db.engine.connect() as conn: conn.execute(text(''' DROP INDEX IF EXISTS idx_notifs_notif_type; DROP INDEX IF EXISTS idx_notifs_timestamp; DROP INDEX IF EXISTS idx_notifs_user_id; DROP INDEX IF EXISTS idx_notifs_sender_id; DROP INDEX IF EXISTS idx_notifs_read; DROP TABLE IF EXISTS notifs; ''')) conn.commit() if __name__ == '__main__': app = Flask(__name__) # Use the same database configuration as in app.py app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', 'postgresql://postgres:1253@localhost:5432/docupulse') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False print("Connecting to database...") db.init_app(app) with app.app_context(): upgrade()