Add migrate_db() to add missing columns to existing servers table
This commit is contained in:
20
app/app.py
20
app/app.py
@@ -412,10 +412,30 @@ def usage_color(percent):
|
||||
|
||||
# --- Main ---
|
||||
|
||||
def migrate_db():
|
||||
"""Add any missing columns to existing tables."""
|
||||
import sqlite3
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("PRAGMA table_info(servers)")
|
||||
existing = {row[1] for row in cursor.fetchall()}
|
||||
migrations = {
|
||||
'url': "ALTER TABLE servers ADD COLUMN url VARCHAR(1024) DEFAULT ''",
|
||||
'notes': "ALTER TABLE servers ADD COLUMN notes TEXT DEFAULT ''",
|
||||
}
|
||||
for col, sql in migrations.items():
|
||||
if col not in existing:
|
||||
cursor.execute(sql)
|
||||
logger.info("Added column '%s' to servers table", col)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
migrate_db()
|
||||
logger.info("Database ready at %s", DB_PATH)
|
||||
|
||||
# Start collector thread
|
||||
|
||||
Reference in New Issue
Block a user