@@ -168,39 +168,53 @@ def inject_user_date_format():
168168 from sqlalchemy import text
169169 inspector = db .inspect (db .engine )
170170
171- # Check UserSettings table for missing columns
172- user_settings_columns = [col ['name' ] for col in inspector .get_columns ('user_settings' )]
173- migrations_applied = []
174-
175- if 'last_notification_sent' not in user_settings_columns :
176- print ("🔄 Auto-migrating: Adding last_notification_sent column to user_settings..." )
177- with db .engine .connect () as conn :
178- conn .execute (text ('ALTER TABLE user_settings ADD COLUMN last_notification_sent DATE' ))
179- conn .commit ()
180- migrations_applied .append ("last_notification_sent" )
181-
182- if 'notification_time' not in user_settings_columns :
183- print ("🔄 Auto-migrating: Adding notification_time column to user_settings..." )
184- with db .engine .connect () as conn :
185- conn .execute (text ('ALTER TABLE user_settings ADD COLUMN notification_time INTEGER DEFAULT 9' ))
186- conn .commit ()
187- migrations_applied .append ("notification_time" )
188-
189- # Check Subscription table for missing columns
190- subscription_columns = [col ['name' ] for col in inspector .get_columns ('subscription' )]
171+ # Get all table names to check if tables exist
172+ table_names = inspector .get_table_names ()
173+ print (f"📊 Found tables: { table_names } " )
191174
192- if 'custom_notification_days' not in subscription_columns :
193- print ("🔄 Auto-migrating: Adding custom_notification_days column to subscription..." )
194- with db .engine .connect () as conn :
195- conn .execute (text ('ALTER TABLE subscription ADD COLUMN custom_notification_days INTEGER' ))
196- conn .commit ()
197- migrations_applied .append ("custom_notification_days" )
198-
199- if migrations_applied :
200- print (f"✅ Database migration completed! Added: { ', ' .join (migrations_applied )} " )
175+ # Check UserSettings table for missing columns
176+ if 'user_settings' in table_names :
177+ user_settings_columns = [col ['name' ] for col in inspector .get_columns ('user_settings' )]
178+ print (f"🔍 UserSettings columns: { user_settings_columns } " )
179+ migrations_applied = []
180+
181+ if 'last_notification_sent' not in user_settings_columns :
182+ print ("🔄 Auto-migrating: Adding last_notification_sent column to user_settings..." )
183+ with db .engine .connect () as conn :
184+ conn .execute (text ('ALTER TABLE user_settings ADD COLUMN last_notification_sent DATE' ))
185+ conn .commit ()
186+ migrations_applied .append ("last_notification_sent" )
187+
188+ if 'notification_time' not in user_settings_columns :
189+ print ("🔄 Auto-migrating: Adding notification_time column to user_settings..." )
190+ with db .engine .connect () as conn :
191+ conn .execute (text ('ALTER TABLE user_settings ADD COLUMN notification_time INTEGER DEFAULT 9' ))
192+ conn .commit ()
193+ migrations_applied .append ("notification_time" )
194+
195+ # Check Subscription table for missing columns
196+ if 'subscription' in table_names :
197+ subscription_columns = [col ['name' ] for col in inspector .get_columns ('subscription' )]
198+ print (f"🔍 Subscription columns: { subscription_columns } " )
199+
200+ if 'custom_notification_days' not in subscription_columns :
201+ print ("🔄 Auto-migrating: Adding custom_notification_days column to subscription..." )
202+ with db .engine .connect () as conn :
203+ conn .execute (text ('ALTER TABLE subscription ADD COLUMN custom_notification_days INTEGER' ))
204+ conn .commit ()
205+ migrations_applied .append ("custom_notification_days" )
206+
207+ if migrations_applied :
208+ print (f"✅ Database migration completed! Added: { ', ' .join (migrations_applied )} " )
209+ else :
210+ print ("✅ Database is up to date, no migrations needed" )
211+ else :
212+ print ("⚠️ user_settings table not found, skipping migration" )
201213
202214 except Exception as e :
203- print (f"⚠️ Database migration skipped: { e } " )
215+ print (f"⚠️ Database migration failed: { e } " )
216+ import traceback
217+ traceback .print_exc ()
204218
205219 # Create default admin user if no admin users exist
206220 from app .models import User , UserSettings
0 commit comments