It's been 2 days I'm trying to write a simple wizard installation for my due project, and I'm currently going nuts about a problem I can't solve.
The need
I'm developping a sorta wizard. This wizard asks for the usual DB settings : host, port, username, password, and database name.
I'm asked to create a database whose name comes from the form, and populate it with migrations and seeds defined in usual files.
I can't use the RachidLaasri github project, because specs cannot accept it.
What works
So far, I managed to create a database, using a low level connection (using new \mysqli). This step was necessary this way because we need to test every step of connection to return an appropriate error message.
Then, I rewrite the .env with parameters proven corrects, including database name. So far so good.
the problem
Except that when I run Artisan::call('migrate')
, it runs on previous database name (the one in the .env before rewriting it). I tried everything I found, but can't figure out a solution.
So, now a bit of my code, simplifed for readability :
// Try connection with provided settings
$dbHost = $request->get('db_host');
$dbPort = $request->get('db_port');
$dbName = $request->get('db_name');
$dbUsername = $request->get('db_username');
$dbPassword = $request->get('db_password');
try {
$dbConnection = new \mysqli($dbHost,
$dbUsername,
$dbPassword,
'',
$dbPort
);
} catch (\Exception $e) {
return redirect()->back()
->withInput($request->all())
->withErrors(['general' => trans('wizard.errors.dbInit') . $e->getMessage()]);
}
// Create database
$sql = 'CREATE DATABASE ' . $dbName .
' CHARACTER SET utf8 COLLATE utf8_bin';
$queryResult = $dbConnection->query($sql);
// [...] Handling errors
$dbConnection->close();
// Writing .env file
// [...] Checking and replacing params, writing file, catching errors
// Executing artisan commands to create tables, and populate them with mandatory data
Artisan::call("config:clear");
// Setting the config to use the newly created database
Config::set('database.connections.mysql.database', $dbName);
// DB::select('USE ' . $dbName); // Throws : General error: 1 near "USE": syntax error (SQL: USE dbname)
Artisan::call('migrate');
Artisan::call('db:seed');
dd('STAHP'); // TODO debug
return redirect()->route('wizard.step2');
Migrations and seeds are always done on the previous DB_DATABASE value coming from .env file.
I'm out of solution, I don't know what to try.
thank you for your help.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire