Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
19595ec
1
Parent(s): e4ce5de
Improve database initialization and persistence
Browse files
app.py
CHANGED
|
@@ -16,54 +16,13 @@ ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 123
|
|
| 16 |
|
| 17 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 18 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 19 |
-
|
| 20 |
-
# 確保資料目錄存在並有正確權限
|
| 21 |
os.makedirs('/data', exist_ok=True)
|
| 22 |
-
|
| 23 |
-
# 等待資料目錄就緒並確保權限正確
|
| 24 |
-
max_retries = 30 # 增加重試次數
|
| 25 |
-
retry_count = 0
|
| 26 |
-
while retry_count < max_retries:
|
| 27 |
-
try:
|
| 28 |
-
# 檢查目錄權限
|
| 29 |
-
if not os.path.exists('/data'):
|
| 30 |
-
print("Creating /data directory...")
|
| 31 |
-
os.makedirs('/data', exist_ok=True)
|
| 32 |
-
|
| 33 |
-
# 設置權限
|
| 34 |
-
os.chmod('/data', 0o777)
|
| 35 |
-
|
| 36 |
-
# 檢查是否可寫
|
| 37 |
-
if os.access('/data', os.W_OK):
|
| 38 |
-
print("/data directory is writable")
|
| 39 |
-
# 檢查資料庫文件
|
| 40 |
-
if os.path.exists(db_path):
|
| 41 |
-
print(f"Database file exists at {db_path}")
|
| 42 |
-
if os.access(db_path, os.W_OK):
|
| 43 |
-
print("Database file is writable")
|
| 44 |
-
break
|
| 45 |
-
else:
|
| 46 |
-
print("Setting database file permissions...")
|
| 47 |
-
os.chmod(db_path, 0o666)
|
| 48 |
-
else:
|
| 49 |
-
print(f"Creating new database file at {db_path}")
|
| 50 |
-
open(db_path, 'a').close()
|
| 51 |
-
os.chmod(db_path, 0o666)
|
| 52 |
-
break
|
| 53 |
-
|
| 54 |
-
print(f"Waiting for /data directory to be writable... Attempt {retry_count + 1}/{max_retries}")
|
| 55 |
-
time.sleep(2)
|
| 56 |
-
retry_count += 1
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Error checking /data directory: {e}")
|
| 59 |
-
time.sleep(2)
|
| 60 |
-
retry_count += 1
|
| 61 |
-
|
| 62 |
-
if retry_count >= max_retries:
|
| 63 |
-
print("Warning: Could not verify /data directory permissions after maximum retries")
|
| 64 |
else: # 在本地開發環境中
|
| 65 |
-
|
| 66 |
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
| 67 |
|
| 68 |
print(f"Using database path: {db_path}")
|
| 69 |
|
|
@@ -71,25 +30,6 @@ print(f"Using database path: {db_path}")
|
|
| 71 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 72 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 73 |
|
| 74 |
-
# 確保資料庫文件存在並設置權限
|
| 75 |
-
try:
|
| 76 |
-
# 如果資料庫文件不存在,創建它
|
| 77 |
-
if not os.path.exists(db_path):
|
| 78 |
-
print(f"Creating new database file at {db_path}")
|
| 79 |
-
open(db_path, 'a').close()
|
| 80 |
-
|
| 81 |
-
# 設置資料庫文件權限
|
| 82 |
-
print(f"Setting permissions for database file at {db_path}")
|
| 83 |
-
os.chmod(db_path, 0o666)
|
| 84 |
-
|
| 85 |
-
# 檢查資料庫文件權限
|
| 86 |
-
if os.access(db_path, os.W_OK):
|
| 87 |
-
print("Database file is writable")
|
| 88 |
-
else:
|
| 89 |
-
print("Warning: Database file is not writable")
|
| 90 |
-
except OSError as e:
|
| 91 |
-
print(f"Error setting up database: {e}")
|
| 92 |
-
|
| 93 |
db = SQLAlchemy(app)
|
| 94 |
|
| 95 |
class Table(db.Model):
|
|
@@ -137,12 +77,30 @@ class Participant(db.Model):
|
|
| 137 |
def init_db():
|
| 138 |
with app.app_context():
|
| 139 |
try:
|
|
|
|
| 140 |
db.create_all()
|
|
|
|
| 141 |
# 只在完全沒有桌子時才創建預設桌子
|
| 142 |
if Table.query.count() == 0:
|
| 143 |
print("No tables found, creating default tables...")
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
db.session.add(table)
|
| 147 |
db.session.commit()
|
| 148 |
print("Default tables created successfully!")
|
|
|
|
| 16 |
|
| 17 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 18 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 19 |
+
# 確保資料目錄存在
|
|
|
|
| 20 |
os.makedirs('/data', exist_ok=True)
|
| 21 |
+
db_path = '/data/event.db'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else: # 在本地開發環境中
|
| 23 |
+
# 確保 instance 目錄存在
|
| 24 |
os.makedirs(app.instance_path, exist_ok=True)
|
| 25 |
+
db_path = os.path.join(app.instance_path, 'event.db')
|
| 26 |
|
| 27 |
print(f"Using database path: {db_path}")
|
| 28 |
|
|
|
|
| 30 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 31 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
db = SQLAlchemy(app)
|
| 34 |
|
| 35 |
class Table(db.Model):
|
|
|
|
| 77 |
def init_db():
|
| 78 |
with app.app_context():
|
| 79 |
try:
|
| 80 |
+
# 只創建不存在的表格
|
| 81 |
db.create_all()
|
| 82 |
+
|
| 83 |
# 只在完全沒有桌子時才創建預設桌子
|
| 84 |
if Table.query.count() == 0:
|
| 85 |
print("No tables found, creating default tables...")
|
| 86 |
+
# 從設定檔讀取桌次設定
|
| 87 |
+
settings_file = os.path.join('/data' if os.environ.get('SPACE_ID') else app.instance_path, 'settings.json')
|
| 88 |
+
if os.path.exists(settings_file):
|
| 89 |
+
with open(settings_file, 'r') as f:
|
| 90 |
+
import json
|
| 91 |
+
settings = json.load(f)
|
| 92 |
+
total_tables = settings.get('total_tables', 10)
|
| 93 |
+
seats_per_table = settings.get('seats_per_table', 8)
|
| 94 |
+
else:
|
| 95 |
+
total_tables = 10
|
| 96 |
+
seats_per_table = 8
|
| 97 |
+
|
| 98 |
+
# 創建預設桌子
|
| 99 |
+
for i in range(1, total_tables + 1):
|
| 100 |
+
table = Table(
|
| 101 |
+
name=f'第{i}桌',
|
| 102 |
+
capacity=seats_per_table
|
| 103 |
+
)
|
| 104 |
db.session.add(table)
|
| 105 |
db.session.commit()
|
| 106 |
print("Default tables created successfully!")
|