BaseCommand.php
2 years ago
FreshCommand.php
2 years ago
InstallCommand.php
2 years ago
MigrateCommand.php
2 years ago
MigrateMakeCommand.php
2 years ago
RefreshCommand.php
2 years ago
ResetCommand.php
2 years ago
RollbackCommand.php
2 years ago
StatusCommand.php
2 years ago
TableGuesser.php
2 years ago
MigrateCommand.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Illuminate\Database\Console\Migrations; |
| 4 | |
| 5 | use IAWP_SCOPED\Illuminate\Console\ConfirmableTrait; |
| 6 | use IAWP_SCOPED\Illuminate\Contracts\Events\Dispatcher; |
| 7 | use IAWP_SCOPED\Illuminate\Database\Events\SchemaLoaded; |
| 8 | use IAWP_SCOPED\Illuminate\Database\Migrations\Migrator; |
| 9 | use IAWP_SCOPED\Illuminate\Database\SqlServerConnection; |
| 10 | /** @internal */ |
| 11 | class MigrateCommand extends BaseCommand |
| 12 | { |
| 13 | use ConfirmableTrait; |
| 14 | /** |
| 15 | * The name and signature of the console command. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $signature = 'migrate {--database= : The database connection to use} |
| 20 | {--force : Force the operation to run when in production} |
| 21 | {--path=* : The path(s) to the migrations files to be executed} |
| 22 | {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} |
| 23 | {--schema-path= : The path to a schema dump file} |
| 24 | {--pretend : Dump the SQL queries that would be run} |
| 25 | {--seed : Indicates if the seed task should be re-run} |
| 26 | {--seeder= : The class name of the root seeder} |
| 27 | {--step : Force the migrations to be run so they can be rolled back individually}'; |
| 28 | /** |
| 29 | * The console command description. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $description = 'Run the database migrations'; |
| 34 | /** |
| 35 | * The migrator instance. |
| 36 | * |
| 37 | * @var \Illuminate\Database\Migrations\Migrator |
| 38 | */ |
| 39 | protected $migrator; |
| 40 | /** |
| 41 | * The event dispatcher instance. |
| 42 | * |
| 43 | * @var \Illuminate\Contracts\Events\Dispatcher |
| 44 | */ |
| 45 | protected $dispatcher; |
| 46 | /** |
| 47 | * Create a new migration command instance. |
| 48 | * |
| 49 | * @param \Illuminate\Database\Migrations\Migrator $migrator |
| 50 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
| 51 | * @return void |
| 52 | */ |
| 53 | public function __construct(Migrator $migrator, Dispatcher $dispatcher) |
| 54 | { |
| 55 | parent::__construct(); |
| 56 | $this->migrator = $migrator; |
| 57 | $this->dispatcher = $dispatcher; |
| 58 | } |
| 59 | /** |
| 60 | * Execute the console command. |
| 61 | * |
| 62 | * @return int |
| 63 | */ |
| 64 | public function handle() |
| 65 | { |
| 66 | if (!$this->confirmToProceed()) { |
| 67 | return 1; |
| 68 | } |
| 69 | $this->migrator->usingConnection($this->option('database'), function () { |
| 70 | $this->prepareDatabase(); |
| 71 | // Next, we will check to see if a path option has been defined. If it has |
| 72 | // we will use the path relative to the root of this installation folder |
| 73 | // so that migrations may be run for any path within the applications. |
| 74 | $this->migrator->setOutput($this->output)->run($this->getMigrationPaths(), ['pretend' => $this->option('pretend'), 'step' => $this->option('step')]); |
| 75 | // Finally, if the "seed" option has been given, we will re-run the database |
| 76 | // seed task to re-populate the database, which is convenient when adding |
| 77 | // a migration and a seed at the same time, as it is only this command. |
| 78 | if ($this->option('seed') && !$this->option('pretend')) { |
| 79 | $this->call('db:seed', ['--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder', '--force' => \true]); |
| 80 | } |
| 81 | }); |
| 82 | return 0; |
| 83 | } |
| 84 | /** |
| 85 | * Prepare the migration database for running. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | protected function prepareDatabase() |
| 90 | { |
| 91 | if (!$this->migrator->repositoryExists()) { |
| 92 | $this->call('migrate:install', \array_filter(['--database' => $this->option('database')])); |
| 93 | } |
| 94 | if (!$this->migrator->hasRunAnyMigrations() && !$this->option('pretend')) { |
| 95 | $this->loadSchemaState(); |
| 96 | } |
| 97 | } |
| 98 | /** |
| 99 | * Load the schema state to seed the initial database schema structure. |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | protected function loadSchemaState() |
| 104 | { |
| 105 | $connection = $this->migrator->resolveConnection($this->option('database')); |
| 106 | // First, we will make sure that the connection supports schema loading and that |
| 107 | // the schema file exists before we proceed any further. If not, we will just |
| 108 | // continue with the standard migration operation as normal without errors. |
| 109 | if ($connection instanceof SqlServerConnection || !\is_file($path = $this->schemaPath($connection))) { |
| 110 | return; |
| 111 | } |
| 112 | $this->line('<info>Loading stored database schema:</info> ' . $path); |
| 113 | $startTime = \microtime(\true); |
| 114 | // Since the schema file will create the "migrations" table and reload it to its |
| 115 | // proper state, we need to delete it here so we don't get an error that this |
| 116 | // table already exists when the stored database schema file gets executed. |
| 117 | $this->migrator->deleteRepository(); |
| 118 | $connection->getSchemaState()->handleOutputUsing(function ($type, $buffer) { |
| 119 | $this->output->write($buffer); |
| 120 | })->load($path); |
| 121 | $runTime = \number_format((\microtime(\true) - $startTime) * 1000, 2); |
| 122 | // Finally, we will fire an event that this schema has been loaded so developers |
| 123 | // can perform any post schema load tasks that are necessary in listeners for |
| 124 | // this event, which may seed the database tables with some necessary data. |
| 125 | $this->dispatcher->dispatch(new SchemaLoaded($connection, $path)); |
| 126 | $this->line('<info>Loaded stored database schema.</info> (' . $runTime . 'ms)'); |
| 127 | } |
| 128 | /** |
| 129 | * Get the path to the stored schema for the given connection. |
| 130 | * |
| 131 | * @param \Illuminate\Database\Connection $connection |
| 132 | * @return string |
| 133 | */ |
| 134 | protected function schemaPath($connection) |
| 135 | { |
| 136 | if ($this->option('schema-path')) { |
| 137 | return $this->option('schema-path'); |
| 138 | } |
| 139 | if (\file_exists($path = database_path('schema/' . $connection->getName() . '-schema.dump'))) { |
| 140 | return $path; |
| 141 | } |
| 142 | return database_path('schema/' . $connection->getName() . '-schema.sql'); |
| 143 | } |
| 144 | } |
| 145 |