-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_sequences.py
More file actions
executable file
·32 lines (27 loc) · 1.27 KB
/
Copy pathfix_sequences.py
File metadata and controls
executable file
·32 lines (27 loc) · 1.27 KB
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
import os
from sqlalchemy import create_engine, text
# === 直接配置區 ===
# 填入您的 Supabase (PostgreSQL) 連線字串 (建議用 Pooler Port 6543)
DATABASE_URI = "postgresql://postgres.xxx:[PASSWORD]@aws-0-us-east-1.pooler.supabase.com:6543/postgres"
# =================
def fix():
print(f"🚀 開始修復 PostgreSQL 序列計數器...")
engine = create_engine(DATABASE_URI)
# 這裡列出資料庫中的所有資料表名稱
tables = ['users', 'user_paths', 'files', 'upload_tasks']
try:
with engine.connect() as conn:
for table in tables:
# setval 會將 ID 計數器更新為該表目前最大的 ID + 1
sql = f"SELECT setval(pg_get_serial_sequence('{table}', 'id'), coalesce(max(id), 1), max(id) IS NOT null) FROM {table};"
conn.execute(text(sql))
conn.commit()
print(f"✅ 已修復表: {table}")
print("\n🎉 所有計數器已同步!您可以正常建立資料夾或上傳檔案了。")
except Exception as e:
print(f"❌ 修復失敗: {e}")
if __name__ == "__main__":
if "[ID]" in DATABASE_URI:
print("⚠️ 錯誤:請先編輯 fix_sequences.py 並填入您的 Supabase 連線資訊!")
else:
fix()