@@ -161,7 +161,11 @@ public function run()
161161 $ dbPath = database_path ('database.sqlite ' );
162162 if (!file_exists ($ dbPath )) touch ($ dbPath );
163163 }
164- // 2. Write .env (uses default key from .env.example so app can boot)
164+ // 2. Switch to production drivers (DB-backed) now that DB will exist
165+ $ env = $ this ->setEnv ($ env , 'SESSION_DRIVER ' , 'database ' );
166+ $ env = $ this ->setEnv ($ env , 'CACHE_STORE ' , 'database ' );
167+ $ env = $ this ->setEnv ($ env , 'QUEUE_CONNECTION ' , 'database ' );
168+
165169 File::put (base_path ('.env ' ), $ env );
166170 $ steps [] = ['name ' => 'Create .env configuration ' , 'status ' => 'success ' ];
167171
@@ -175,23 +179,33 @@ public function run()
175179
176180 // 4. Set DB config at runtime so migrations work without reloading .env
177181 $ dbConn = $ db ['db_connection ' ] ?? 'sqlite ' ;
178- config ([" database.default " => $ dbConn ]);
182+ config ([' database.default ' => $ dbConn ]);
179183 if ($ dbConn === 'sqlite ' ) {
180- config (["database.connections.sqlite.database " => database_path ('database.sqlite ' )]);
184+ $ sqlitePath = database_path ('database.sqlite ' );
185+ if (!file_exists ($ sqlitePath )) touch ($ sqlitePath );
186+ config (['database.connections.sqlite.database ' => $ sqlitePath ]);
181187 } else {
182188 config ([
183189 "database.connections. {$ dbConn }.host " => $ db ['db_host ' ] ?? '127.0.0.1 ' ,
184190 "database.connections. {$ dbConn }.port " => $ db ['db_port ' ] ?? '3306 ' ,
185191 "database.connections. {$ dbConn }.database " => $ db ['db_database ' ] ?? 'coopbank ' ,
186- "database.connections. {$ dbConn }.username " => $ db ['db_username ' ] ?? 'root ' ,
187- "database.connections. {$ dbConn }.password " => $ db ['db_password ' ] ?? '' ,
192+ "database.connections. {$ dbConn }.username " => $ db ['db_username ' ] ?? 'root ' ,
193+ "database.connections. {$ dbConn }.password " => $ db ['db_password ' ] ?? '' ,
188194 ]);
189195 }
190- DB ::purge ();
191- DB ::reconnect ();
192196
193- // Clear config cache
194- try { Artisan::call ('config:clear ' ); } catch (\Exception $ e ) { /* may fail if no cache exists */ }
197+ // Purge ALL cached connections and reconnect with new config
198+ DB ::purge ($ dbConn );
199+ DB ::purge ('sqlite ' );
200+ DB ::purge ('mysql ' );
201+ DB ::purge ('pgsql ' );
202+ DB ::reconnect ($ dbConn );
203+
204+ // Verify the connection works
205+ DB ::connection ($ dbConn )->getPdo ();
206+
207+ // Clear any cached config
208+ try { Artisan::call ('config:clear ' ); } catch (\Exception $ e ) {}
195209 $ steps [] = ['name ' => 'Configure database connection ' , 'status ' => 'success ' ];
196210
197211 // 4. Create storage symlink
@@ -202,15 +216,15 @@ public function run()
202216 $ steps [] = ['name ' => 'Create storage symlink ' , 'status ' => 'skipped ' , 'note ' => 'Already exists ' ];
203217 }
204218
205- // 5. Run migrations
206- Artisan::call ('migrate ' , ['--force ' => true ]);
207- $ steps [] = ['name ' => 'Run database migrations ' , 'status ' => 'success ' ];
219+ // 5. Run migrations on the correct connection
220+ Artisan::call ('migrate ' , ['--database ' => $ dbConn , ' -- force ' => true ]);
221+ $ steps [] = ['name ' => 'Run database migrations ( ' . $ dbConn . ' ) ' , 'status ' => 'success ' ];
208222
209- // 6. Seed data
210- Artisan::call ('db:seed ' , ['--class ' => 'RoleSeeder ' , '--force ' => true ]);
211- Artisan::call ('db:seed ' , ['--class ' => 'CountrySeeder ' , '--force ' => true ]);
212- Artisan::call ('db:seed ' , ['--class ' => 'CompanySetupSeeder ' , '--force ' => true ]);
213- Artisan::call ('db:seed ' , ['--class ' => 'ScheduledTaskSeeder ' , '--force ' => true ]);
223+ // 6. Seed data (uses the default connection we just set)
224+ Artisan::call ('db:seed ' , ['--class ' => 'RoleSeeder ' , '--database ' => $ dbConn , ' -- force ' => true ]);
225+ Artisan::call ('db:seed ' , ['--class ' => 'CountrySeeder ' , '--database ' => $ dbConn , ' -- force ' => true ]);
226+ Artisan::call ('db:seed ' , ['--class ' => 'CompanySetupSeeder ' , '--database ' => $ dbConn , ' -- force ' => true ]);
227+ Artisan::call ('db:seed ' , ['--class ' => 'ScheduledTaskSeeder ' , '--database ' => $ dbConn , ' -- force ' => true ]);
214228 $ steps [] = ['name ' => 'Seed roles, countries, company setup, scheduled tasks ' , 'status ' => 'success ' ];
215229
216230 // 7. Create admin user
0 commit comments