I am using Laravel 8 with sail. I want to create a command that creates multiple databases (I want to use the parallel tests feature together with DatabaseTransaction trait, so a migration once beforehand is required. Migration takes also a long time, so creation during test won't work for me).
This is how I create a database and then migrate it in my command:
$database='mydb_test_1';
Config::set('database.connections.mysql.database', null);
$query = "CREATE DATABASE IF NOT EXISTS $database CHARACTER SET $charset COLLATE $collation;";
DB::statement($query);
Config::set('database.connections.mysql.database', $database);
// Necessary to update database name, as DB is a singleton
DB::reconnect();
$this->call('migrate');
This peace of code works for the very first time, when the database did not exist yet.
However, if I run it twice, it shows this error:
However, if I disable the DB:statement(..) like this:
$database='mydb_test_1';
Config::set('database.connections.mysql.database', null);
$query = "CREATE DATABASE IF NOT EXISTS $database CHARACTER SET $charset COLLATE $collation;";
//DB::statement($query);
Config::set('database.connections.mysql.database', $database);
// Necessary to update database name, as DB is a singleton
DB::reconnect();
$this->call('migrate');
Then the code from above returns
Nothing to migrate.
no matter how often I call it.
Why is that? Why does DB::statement('CREATE DATABASE IF NOT EXISTS ..'); makes the migration command fail?
