Spaces:
Sleeping

File size: 1,885 Bytes
5b0ddeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d807f7
 
 
 
5b0ddeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from sqlalchemy import create_engine, text
from dotenv import load_dotenv

load_dotenv()

def fix_sequences():
    database_url = os.getenv("DATABASE_URL")
    if not database_url:
        print("DATABASE_URL not found in .env")
        return

    # Handle Postgres dialect if needed
    if database_url.startswith("postgres://"):
        database_url = database_url.replace("postgres://", "postgresql://", 1)

    engine = create_engine(database_url)
    
    # List of tables with SERIAL primary keys
    tables = [
        "verified_news", "raw_news", "daily_digests", "track_notifications", 
        "users", "protocol_history", "topic_tracking", "otp_verifications",
        "subscriptions", "folders", "saved_articles", "read_history",
        "flagged_articles", "breaking_news", "advertisements", "newspapers"
    ]
    
    with engine.connect() as conn:
        print("Starting sequence synchronization...")
        for table in tables:
            try:
                # Get the sequence name (standard Postgres naming: table_id_seq)
                # But it's safer to just set the value based on MAX(id)
                seq_val_query = text(f"SELECT setval(pg_get_serial_sequence('{table}', 'id'), (SELECT MAX(id) FROM {table}))")
                conn.execute(seq_val_query)
                conn.commit()
                print(f"✅ Synced sequence for table: {table}")
            except Exception as e:
                # Some tables might not have the standard id or sequence name
                if "null" in str(e).lower():
                    print(f"ℹ️  Table {table} sequence is already in sync or has no data.")
                else:
                    print(f"⚠️  Could not sync sequence for {table}: {e}")
        
    print("\nDatabase health check complete. Sequences are now synchronized.")

if __name__ == "__main__":
    fix_sequences()