@@ -271,30 +271,34 @@ def handle_exception(error):
271271
272272    # ===== Database Initialization ===== 
273273
274-     with  app .app_context ():
275-         # Create all tables 
276-         db .create_all ()
274+     # Ensure DB initialization only once (important with multi-worker if --preload not used) 
275+     if  not  app .config .get ('_DB_INITIALIZED' , False ):
276+         with  app .app_context ():
277+             print (f"Initializing database at URI: { app .config ['SQLALCHEMY_DATABASE_URI' ]}  " )
278+             db .create_all ()
279+ 
280+             # Create default admin user only if no administrators exist 
281+             admin_count  =  User .query .filter_by (is_admin = True ).count ()
282+             if  admin_count  ==  0 :
283+                 # No administrators exist, create default admin user 
284+                 admin_user  =  User (username = 'admin' , is_admin = True )
285+                 admin_user .set_password ('changeme' )  # Default password 
286+                 db .session .add (admin_user )
287+                 try :
288+                     db .session .commit ()
289+                     print ("="  *  50 )
290+                     print ("Default admin user created!" )
291+                     print ("Username: admin" )
292+                     print ("Password: changeme" )
293+                     print ("PLEASE CHANGE THIS PASSWORD IMMEDIATELY!" )
294+                     print ("="  *  50 )
295+                 except  Exception  as  e :
296+                     print (f"Error creating admin user: { e }  " )
297+                     db .session .rollback ()
298+             else :
299+                 print (f"Found { admin_count }   administrator(s) - skipping default admin creation" )
277300
278-         # Create default admin user only if no administrators exist 
279-         admin_count  =  User .query .filter_by (is_admin = True ).count ()
280-         if  admin_count  ==  0 :
281-             # No administrators exist, create default admin user 
282-             admin_user  =  User (username = 'admin' , is_admin = True )
283-             admin_user .set_password ('changeme' )  # Default password 
284-             db .session .add (admin_user )
285-             try :
286-                 db .session .commit ()
287-                 print ("="  *  50 )
288-                 print ("Default admin user created!" )
289-                 print ("Username: admin" )
290-                 print ("Password: changeme" )
291-                 print ("PLEASE CHANGE THIS PASSWORD IMMEDIATELY!" )
292-                 print ("="  *  50 )
293-             except  Exception  as  e :
294-                 print (f"Error creating admin user: { e }  " )
295-                 db .session .rollback ()
296-         else :
297-             print (f"Found { admin_count }   administrator(s) - skipping default admin creation" )
301+             app .config ['_DB_INITIALIZED' ] =  True 
298302
299303    # ===== Additional App Configuration ===== 
300304
0 commit comments