|
9 | 9 | use Illuminate\Database\Migrations\Migrator;
|
10 | 10 | use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
|
11 | 11 | use Illuminate\Database\SqlServerConnection;
|
| 12 | +use PDOException; |
| 13 | +use Throwable; |
12 | 14 |
|
13 | 15 | class MigrateCommand extends BaseCommand
|
14 | 16 | {
|
@@ -134,26 +136,88 @@ protected function prepareDatabase()
|
134 | 136 | protected function repositoryExists()
|
135 | 137 | {
|
136 | 138 | return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
|
137 |
| - if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { |
138 |
| - return false; |
139 |
| - } |
| 139 | + try { |
| 140 | + if ($e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { |
| 141 | + return $this->createMissingSqliteDatbase($e->getPrevious()->path); |
| 142 | + } |
140 | 143 |
|
141 |
| - if ($this->option('force')) { |
142 |
| - return touch($e->getPrevious()->path); |
143 |
| - } |
| 144 | + $connection = $this->migrator->resolveConnection($this->option('database')); |
144 | 145 |
|
145 |
| - if ($this->option('no-interaction')) { |
| 146 | + if ( |
| 147 | + $e->getPrevious() instanceof PDOException && |
| 148 | + $e->getPrevious()->getCode() === 1049 && |
| 149 | + $connection->getDriverName() === 'mysql') { |
| 150 | + return $this->createMissingMysqlDatabase($connection); |
| 151 | + } |
| 152 | + |
| 153 | + return false; |
| 154 | + } catch (Throwable) { |
146 | 155 | return false;
|
147 | 156 | }
|
| 157 | + }); |
| 158 | + } |
148 | 159 |
|
149 |
| - $this->components->warn('The SQLite database does not exist: '.$e->getPrevious()->path); |
| 160 | + /** |
| 161 | + * Create a missing SQLite database. |
| 162 | + * |
| 163 | + * @param string $path |
| 164 | + * @return bool |
| 165 | + */ |
| 166 | + protected function createMissingSqliteDatbase($path) |
| 167 | + { |
| 168 | + if ($this->option('force')) { |
| 169 | + return touch($path); |
| 170 | + } |
| 171 | + |
| 172 | + if ($this->option('no-interaction')) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + $this->components->warn('The SQLite database does not exist: '.$path); |
| 177 | + |
| 178 | + if (! $this->components->confirm('Would you like to create it?')) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + return touch($path); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Create a missing MySQL database. |
| 187 | + * |
| 188 | + * @return bool |
| 189 | + */ |
| 190 | + protected function createMissingMysqlDatabase($connection) |
| 191 | + { |
| 192 | + if ($this->laravel['config']->get("database.connections.{$connection->getName()}.database") !== $connection->getDatabaseName()) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + if (! $this->option('force') && $this->option('no-interaction')) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + if (! $this->option('force') && ! $this->option('no-interaction')) { |
| 201 | + $this->components->warn("The database '{$connection->getDatabaseName()}' does not exist on the '{$connection->getName()}' connection."); |
150 | 202 |
|
151 | 203 | if (! $this->components->confirm('Would you like to create it?')) {
|
152 | 204 | return false;
|
153 | 205 | }
|
| 206 | + } |
154 | 207 |
|
155 |
| - return touch($e->getPrevious()->path); |
156 |
| - }); |
| 208 | + try { |
| 209 | + $this->laravel['config']->set("database.connections.{$connection->getName()}.database", null); |
| 210 | + |
| 211 | + $this->laravel['db']->purge(); |
| 212 | + |
| 213 | + $freshConnection = $this->migrator->resolveConnection($this->option('database')); |
| 214 | + |
| 215 | + return tap($freshConnection->unprepared("CREATE DATABASE IF NOT EXISTS {$connection->getDatabaseName()}"), function () { |
| 216 | + $this->laravel['db']->purge(); |
| 217 | + }); |
| 218 | + } finally { |
| 219 | + $this->laravel['config']->set("database.connections.{$connection->getName()}.database", $connection->getDatabaseName()); |
| 220 | + } |
157 | 221 | }
|
158 | 222 |
|
159 | 223 | /**
|
|
0 commit comments