49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import sqlite3
|
|
import os
|
|
import sys
|
|
|
|
# Default path inside Docker container
|
|
DEFAULT_DB_PATH = "/app/data/parking.db"
|
|
|
|
def migrate():
|
|
# Allow overriding DB path via env var or argument
|
|
db_path = os.getenv("DATABASE_PATH", DEFAULT_DB_PATH)
|
|
if len(sys.argv) > 1:
|
|
db_path = sys.argv[1]
|
|
|
|
if not os.path.exists(db_path):
|
|
print(f"Error: Database file not found at {db_path}")
|
|
print("Usage: python upgrade_db_v1.py [path_to_db]")
|
|
sys.exit(1)
|
|
|
|
print(f"Migrating database at: {db_path}")
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
columns_to_add = [
|
|
("booking_window_enabled", "BOOLEAN", "0"),
|
|
("booking_window_end_hour", "INTEGER", "18"),
|
|
("booking_window_end_minute", "INTEGER", "0")
|
|
]
|
|
|
|
for col_name, col_type, default_val in columns_to_add:
|
|
try:
|
|
# Check if column exists
|
|
cursor.execute(f"SELECT {col_name} FROM offices LIMIT 1")
|
|
except sqlite3.OperationalError:
|
|
# Column doesn't exist, add it
|
|
print(f"Adding column: {col_name} ({col_type})")
|
|
try:
|
|
cursor.execute(f"ALTER TABLE offices ADD COLUMN {col_name} {col_type} DEFAULT {default_val}")
|
|
except Exception as e:
|
|
print(f"Failed to add column {col_name}: {e}")
|
|
else:
|
|
print(f"Column {col_name} already exists. Skipping.")
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
print("Migration complete.")
|
|
|
|
if __name__ == "__main__":
|
|
migrate()
|