Update init_db.py
This commit is contained in:
72
init_db.py
72
init_db.py
@@ -4,39 +4,57 @@ from sqlalchemy import text
|
|||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
# First, try to create the section table if it doesn't exist
|
||||||
try:
|
try:
|
||||||
# Try to run migrations first
|
db.session.execute(text("""
|
||||||
upgrade()
|
CREATE TABLE IF NOT EXISTS section (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100) NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
icon VARCHAR(200)
|
||||||
|
)
|
||||||
|
"""))
|
||||||
|
db.session.commit()
|
||||||
|
print("Section table created or already exists")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Migration error (this is normal if tables exist): {e}")
|
print(f"Error creating section table: {e}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
# Check if section_id column exists in plant table
|
# Then try to add the section_id column if it doesn't exist
|
||||||
try:
|
try:
|
||||||
result = db.session.execute(text("SELECT section_id FROM plant LIMIT 1"))
|
# Check if column exists
|
||||||
print("section_id column exists")
|
result = db.session.execute(text("""
|
||||||
except Exception:
|
SELECT column_name
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name='plant' AND column_name='section_id'
|
||||||
|
"""))
|
||||||
|
if not result.fetchone():
|
||||||
print("Adding section_id column to plant table")
|
print("Adding section_id column to plant table")
|
||||||
try:
|
db.session.execute(text("""
|
||||||
# Add section_id column if it doesn't exist
|
ALTER TABLE plant
|
||||||
db.session.execute(text("""
|
ADD COLUMN section_id INTEGER
|
||||||
ALTER TABLE plant
|
REFERENCES section(id)
|
||||||
ADD COLUMN IF NOT EXISTS section_id INTEGER
|
"""))
|
||||||
REFERENCES section(id)
|
db.session.commit()
|
||||||
"""))
|
print("Added section_id column successfully")
|
||||||
db.session.commit()
|
else:
|
||||||
print("Added section_id column successfully")
|
print("section_id column already exists")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error adding section_id column: {e}")
|
print(f"Error adding section_id column: {e}")
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
|
||||||
# Create default admin user if it doesn't exist
|
# Create default admin user if it doesn't exist
|
||||||
admin = User.query.filter_by(username='admin').first()
|
try:
|
||||||
if not admin:
|
admin = User.query.filter_by(username='admin').first()
|
||||||
admin = User(username='admin')
|
if not admin:
|
||||||
admin.set_password('admin123') # You should change this password after first login
|
admin = User(username='admin')
|
||||||
db.session.add(admin)
|
admin.set_password('admin123') # You should change this password after first login
|
||||||
db.session.commit()
|
db.session.add(admin)
|
||||||
print("Default admin user created!")
|
db.session.commit()
|
||||||
|
print("Default admin user created!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating admin user: {e}")
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
init_db()
|
init_db()
|
||||||
Reference in New Issue
Block a user