"""Initialize database and create tables.""" import os from app import create_app, db from app.services.admin_service import AdminService def init_database(): """Initialize database with tables and default admin.""" config_name = os.environ.get('FLASK_CONFIG', 'production') app = create_app(config_name) with app.app_context(): # Create all tables db.create_all() print("Database tables created successfully.") # Create default admin if not exists created, message = AdminService.create_default_admin() print(message) # Create indexes for name columns (if not exists) try: db.session.execute(db.text('CREATE INDEX IF NOT EXISTS ix_persons_name ON persons(name)')) db.session.execute(db.text('CREATE INDEX IF NOT EXISTS ix_items_name ON items(name)')) db.session.commit() print("Indexes created successfully.") except Exception as e: print(f"Index creation skipped: {e}") if __name__ == '__main__': init_database()