42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from app import app, db, User
|
|
from flask_migrate import upgrade
|
|
from sqlalchemy import text
|
|
|
|
def init_db():
|
|
with app.app_context():
|
|
try:
|
|
# Try to run migrations first
|
|
upgrade()
|
|
except Exception as e:
|
|
print(f"Migration error (this is normal if tables exist): {e}")
|
|
|
|
# Check if section_id column exists in plant table
|
|
try:
|
|
result = db.session.execute(text("SELECT section_id FROM plant LIMIT 1"))
|
|
print("section_id column exists")
|
|
except Exception:
|
|
print("Adding section_id column to plant table")
|
|
try:
|
|
# Add section_id column if it doesn't exist
|
|
db.session.execute(text("""
|
|
ALTER TABLE plant
|
|
ADD COLUMN IF NOT EXISTS section_id INTEGER
|
|
REFERENCES section(id)
|
|
"""))
|
|
db.session.commit()
|
|
print("Added section_id column successfully")
|
|
except Exception as e:
|
|
print(f"Error adding section_id column: {e}")
|
|
db.session.rollback()
|
|
|
|
# Create default admin user if it doesn't exist
|
|
admin = User.query.filter_by(username='admin').first()
|
|
if not admin:
|
|
admin = User(username='admin')
|
|
admin.set_password('admin123') # You should change this password after first login
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
print("Default admin user created!")
|
|
|
|
if __name__ == '__main__':
|
|
init_db() |