migrator = $migrator; $this->dispatcher = $dispatcher; } /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $this->migrator->usingConnection($this->option('database'), function () { $this->prepareDatabase(); // Next, we will check to see if a path option has been defined. If it has // we will use the path relative to the root of this installation folder // so that migrations may be run for any path within the applications. $this->migrator->setOutput($this->output) ->run($this->getMigrationPaths(), [ 'pretend' => $this->option('pretend'), 'step' => $this->option('step'), ]); // Finally, if the "seed" option has been given, we will re-run the database // seed task to re-populate the database, which is convenient when adding // a migration and a seed at the same time, as it is only this command. if ($this->option('seed') && ! $this->option('pretend')) { $this->call('db:seed', ['--force' => true]); } }); return 0; } /** * Prepare the migration database for running. * * @return void */ protected function prepareDatabase() { if (! $this->migrator->repositoryExists()) { $this->call('migrate:install', array_filter([ '--database' => $this->option('database'), ])); } if (! $this->migrator->hasRunAnyMigrations() && ! $this->option('pretend')) { $this->loadSchemaState(); } } /** * Load the schema state to seed the initial database schema structure. * * @return void */ protected function loadSchemaState() { $connection = $this->migrator->resolveConnection($this->option('database')); // First, we will make sure that the connection supports schema loading and that // the schema file exists before we proceed any further. If not, we will just // continue with the standard migration operation as normal without errors. if ($connection instanceof SqlServerConnection || ! is_file($path = $this->schemaPath($connection))) { return; } $this->line('Loading stored database schema: '.$path); $startTime = microtime(true); // Since the schema file will create the "migrations" table and reload it to its // proper state, we need to delete it here so we don't get an error that this // table already exists when the stored database schema file gets executed. $this->migrator->deleteRepository(); $connection->getSchemaState()->handleOutputUsing(function ($type, $buffer) { $this->output->write($buffer); })->load($path); $runTime = number_format((microtime(true) - $startTime) * 1000, 2); // Finally, we will fire an event that this schema has been loaded so developers // can perform any post schema load tasks that are necessary in listeners for // this event, which may seed the database tables with some necessary data. $this->dispatcher->dispatch( new SchemaLoaded($connection, $path) ); $this->line('Loaded stored database schema. ('.$runTime.'ms)'); } /** * Get the path to the stored schema for the given connection. * * @param \Illuminate\Database\Connection $connection * @return string */ protected function schemaPath($connection) { if ($this->option('schema-path')) { return $this->option('schema-path'); } if (file_exists($path = database_path('schema/'.$connection->getName().'-schema.dump'))) { return $path; } return database_path('schema/'.$connection->getName().'-schema.sql'); } }