init
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class BaseCommand extends Command
|
||||
{
|
||||
/**
|
||||
* Get all of the migration paths.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getMigrationPaths()
|
||||
{
|
||||
// Here, 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 the installation folder so our database
|
||||
// migrations may be run for any customized path from within the application.
|
||||
if ($this->input->hasOption('path') && $this->option('path')) {
|
||||
return (new Collection($this->option('path')))->map(function ($path) {
|
||||
return ! $this->usingRealPath()
|
||||
? $this->laravel->basePath().'/'.$path
|
||||
: $path;
|
||||
})->all();
|
||||
}
|
||||
|
||||
return array_merge(
|
||||
$this->migrator->paths(), [$this->getMigrationPath()]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given path(s) are pre-resolved "real" paths.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function usingRealPath()
|
||||
{
|
||||
return $this->input->hasOption('realpath') && $this->option('realpath');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the migration directory.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getMigrationPath()
|
||||
{
|
||||
return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'migrations';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Console\ConfirmableTrait;
|
||||
use Illuminate\Console\Prohibitable;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Events\DatabaseRefreshed;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(name: 'migrate:fresh')]
|
||||
class FreshCommand extends Command
|
||||
{
|
||||
use ConfirmableTrait, Prohibitable;
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:fresh';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Drop all tables and re-run all migrations';
|
||||
|
||||
/**
|
||||
* The migrator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\Migrator
|
||||
*/
|
||||
protected $migrator;
|
||||
|
||||
/**
|
||||
* Create a new fresh command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\Migrator $migrator
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Migrator $migrator)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->migrator = $migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($this->isProhibited() ||
|
||||
! $this->confirmToProceed()) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$database = $this->input->getOption('database');
|
||||
|
||||
$this->migrator->usingConnection($database, function () use ($database) {
|
||||
if ($this->migrator->repositoryExists()) {
|
||||
$this->newLine();
|
||||
|
||||
$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
|
||||
'--database' => $database,
|
||||
'--drop-views' => $this->option('drop-views'),
|
||||
'--drop-types' => $this->option('drop-types'),
|
||||
'--force' => true,
|
||||
])) == 0);
|
||||
}
|
||||
});
|
||||
|
||||
$this->newLine();
|
||||
|
||||
$this->call('migrate', array_filter([
|
||||
'--database' => $database,
|
||||
'--path' => $this->input->getOption('path'),
|
||||
'--realpath' => $this->input->getOption('realpath'),
|
||||
'--schema-path' => $this->input->getOption('schema-path'),
|
||||
'--force' => true,
|
||||
'--step' => $this->option('step'),
|
||||
]));
|
||||
|
||||
if ($this->laravel->bound(Dispatcher::class)) {
|
||||
$this->laravel[Dispatcher::class]->dispatch(
|
||||
new DatabaseRefreshed($database, $this->needsSeeding())
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->needsSeeding()) {
|
||||
$this->runSeeder($database);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the developer has requested database seeding.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function needsSeeding()
|
||||
{
|
||||
return $this->option('seed') || $this->option('seeder');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the database seeder command.
|
||||
*
|
||||
* @param string $database
|
||||
* @return void
|
||||
*/
|
||||
protected function runSeeder($database)
|
||||
{
|
||||
$this->call('db:seed', array_filter([
|
||||
'--database' => $database,
|
||||
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
|
||||
'--force' => true,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
|
||||
['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
|
||||
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
|
||||
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
|
||||
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
||||
['schema-path', null, InputOption::VALUE_OPTIONAL, 'The path to a schema dump file'],
|
||||
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
|
||||
['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
|
||||
['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(name: 'migrate:install')]
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:install';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create the migration repository';
|
||||
|
||||
/**
|
||||
* The repository instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\MigrationRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Create a new migration install command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MigrationRepositoryInterface $repository)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->repository->setSource($this->input->getOption('database'));
|
||||
|
||||
$this->repository->createRepository();
|
||||
|
||||
$this->components->info('Migration table created successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+315
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\ConfirmableTrait;
|
||||
use Illuminate\Contracts\Console\Isolatable;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Events\SchemaLoaded;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
|
||||
use Illuminate\Database\SqlServerConnection;
|
||||
use PDOException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Throwable;
|
||||
|
||||
use function Laravel\Prompts\confirm;
|
||||
|
||||
#[AsCommand(name: 'migrate')]
|
||||
class MigrateCommand extends BaseCommand implements Isolatable
|
||||
{
|
||||
use ConfirmableTrait;
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'migrate {--database= : The database connection to use}
|
||||
{--force : Force the operation to run when in production}
|
||||
{--path=* : The path(s) to the migrations files to be executed}
|
||||
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
|
||||
{--schema-path= : The path to a schema dump file}
|
||||
{--pretend : Dump the SQL queries that would be run}
|
||||
{--seed : Indicates if the seed task should be re-run}
|
||||
{--seeder= : The class name of the root seeder}
|
||||
{--step : Force the migrations to be run so they can be rolled back individually}
|
||||
{--graceful : Return a successful exit code even if an error occurs}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run the database migrations';
|
||||
|
||||
/**
|
||||
* The migrator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\Migrator
|
||||
*/
|
||||
protected $migrator;
|
||||
|
||||
/**
|
||||
* The event dispatcher instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* Create a new migration command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\Migrator $migrator
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Migrator $migrator, Dispatcher $dispatcher)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->migrator = $migrator;
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (! $this->confirmToProceed()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->runMigrations();
|
||||
} catch (Throwable $e) {
|
||||
if ($this->option('graceful')) {
|
||||
$this->components->warn($e->getMessage());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the pending migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function runMigrations()
|
||||
{
|
||||
$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', [
|
||||
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
|
||||
'--force' => true,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the migration database for running.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function prepareDatabase()
|
||||
{
|
||||
if (! $this->repositoryExists()) {
|
||||
$this->components->info('Preparing database.');
|
||||
|
||||
$this->components->task('Creating migration table', function () {
|
||||
return $this->callSilent('migrate:install', array_filter([
|
||||
'--database' => $this->option('database'),
|
||||
])) == 0;
|
||||
});
|
||||
|
||||
$this->newLine();
|
||||
}
|
||||
|
||||
if (! $this->migrator->hasRunAnyMigrations() && ! $this->option('pretend')) {
|
||||
$this->loadSchemaState();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the migrator repository exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function repositoryExists()
|
||||
{
|
||||
return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
|
||||
try {
|
||||
if ($e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
|
||||
return $this->createMissingSqliteDatabase($e->getPrevious()->path);
|
||||
}
|
||||
|
||||
$connection = $this->migrator->resolveConnection($this->option('database'));
|
||||
|
||||
if (
|
||||
$e->getPrevious() instanceof PDOException &&
|
||||
$e->getPrevious()->getCode() === 1049 &&
|
||||
in_array($connection->getDriverName(), ['mysql', 'mariadb'])) {
|
||||
return $this->createMissingMysqlDatabase($connection);
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a missing SQLite database.
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function createMissingSqliteDatabase($path)
|
||||
{
|
||||
if ($this->option('force')) {
|
||||
return touch($path);
|
||||
}
|
||||
|
||||
if ($this->option('no-interaction')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->components->warn('The SQLite database configured for this application does not exist: '.$path);
|
||||
|
||||
if (! confirm('Would you like to create it?', default: true)) {
|
||||
$this->components->info('Operation cancelled. No database was created.');
|
||||
|
||||
throw new RuntimeException('Database was not created. Aborting migration.');
|
||||
}
|
||||
|
||||
return touch($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a missing MySQL database.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function createMissingMysqlDatabase($connection)
|
||||
{
|
||||
if ($this->laravel['config']->get("database.connections.{$connection->getName()}.database") !== $connection->getDatabaseName()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->option('force') && $this->option('no-interaction')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->option('force') && ! $this->option('no-interaction')) {
|
||||
$this->components->warn("The database '{$connection->getDatabaseName()}' does not exist on the '{$connection->getName()}' connection.");
|
||||
|
||||
if (! confirm('Would you like to create it?', default: true)) {
|
||||
$this->components->info('Operation cancelled. No database was created.');
|
||||
|
||||
throw new RuntimeException('Database was not created. Aborting migration.');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", null);
|
||||
|
||||
$this->laravel['db']->purge();
|
||||
|
||||
$freshConnection = $this->migrator->resolveConnection($this->option('database'));
|
||||
|
||||
return tap($freshConnection->unprepared("CREATE DATABASE IF NOT EXISTS `{$connection->getDatabaseName()}`"), function () {
|
||||
$this->laravel['db']->purge();
|
||||
});
|
||||
} finally {
|
||||
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", $connection->getDatabaseName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->components->info('Loading stored database schemas.');
|
||||
|
||||
$this->components->task($path, function () use ($connection, $path) {
|
||||
// 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);
|
||||
});
|
||||
|
||||
$this->newLine();
|
||||
|
||||
// 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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
||||
use Illuminate\Database\Migrations\MigrationCreator;
|
||||
use Illuminate\Support\Composer;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
|
||||
#[AsCommand(name: 'make:migration')]
|
||||
class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
|
||||
{
|
||||
/**
|
||||
* The console command signature.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'make:migration {name : The name of the migration}
|
||||
{--create= : The table to be created}
|
||||
{--table= : The table to migrate}
|
||||
{--path= : The location where the migration file should be created}
|
||||
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
|
||||
{--fullpath : Output the full path of the migration (Deprecated)}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create a new migration file';
|
||||
|
||||
/**
|
||||
* The migration creator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\MigrationCreator
|
||||
*/
|
||||
protected $creator;
|
||||
|
||||
/**
|
||||
* The Composer instance.
|
||||
*
|
||||
* @var \Illuminate\Support\Composer
|
||||
*
|
||||
* @deprecated Will be removed in a future Laravel version.
|
||||
*/
|
||||
protected $composer;
|
||||
|
||||
/**
|
||||
* Create a new migration install command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\MigrationCreator $creator
|
||||
* @param \Illuminate\Support\Composer $composer
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MigrationCreator $creator, Composer $composer)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->creator = $creator;
|
||||
$this->composer = $composer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
// It's possible for the developer to specify the tables to modify in this
|
||||
// schema operation. The developer may also specify if this table needs
|
||||
// to be freshly created so we can create the appropriate migrations.
|
||||
$name = Str::snake(trim($this->input->getArgument('name')));
|
||||
|
||||
$table = $this->input->getOption('table');
|
||||
|
||||
$create = $this->input->getOption('create') ?: false;
|
||||
|
||||
// If no table was given as an option but a create option is given then we
|
||||
// will use the "create" option as the table name. This allows the devs
|
||||
// to pass a table name into this option as a short-cut for creating.
|
||||
if (! $table && is_string($create)) {
|
||||
$table = $create;
|
||||
|
||||
$create = true;
|
||||
}
|
||||
|
||||
// Next, we will attempt to guess the table name if this the migration has
|
||||
// "create" in the name. This will allow us to provide a convenient way
|
||||
// of creating migrations that create new tables for the application.
|
||||
if (! $table) {
|
||||
[$table, $create] = TableGuesser::guess($name);
|
||||
}
|
||||
|
||||
// Now we are ready to write the migration out to disk. Once we've written
|
||||
// the migration out, we will dump-autoload for the entire framework to
|
||||
// make sure that the migrations are registered by the class loaders.
|
||||
$this->writeMigration($name, $table, $create);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the migration file to disk.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $table
|
||||
* @param bool $create
|
||||
* @return void
|
||||
*/
|
||||
protected function writeMigration($name, $table, $create)
|
||||
{
|
||||
$file = $this->creator->create(
|
||||
$name, $this->getMigrationPath(), $table, $create
|
||||
);
|
||||
|
||||
$this->components->info(sprintf('Migration [%s] created successfully.', $file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get migration path (either specified by '--path' option or default location).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getMigrationPath()
|
||||
{
|
||||
if (! is_null($targetPath = $this->input->getOption('path'))) {
|
||||
return ! $this->usingRealPath()
|
||||
? $this->laravel->basePath().'/'.$targetPath
|
||||
: $targetPath;
|
||||
}
|
||||
|
||||
return parent::getMigrationPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt for missing input arguments using the returned questions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function promptForMissingArgumentsUsing()
|
||||
{
|
||||
return [
|
||||
'name' => ['What should the migration be named?', 'E.g. create_flights_table'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Console\ConfirmableTrait;
|
||||
use Illuminate\Console\Prohibitable;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Events\DatabaseRefreshed;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(name: 'migrate:refresh')]
|
||||
class RefreshCommand extends Command
|
||||
{
|
||||
use ConfirmableTrait, Prohibitable;
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:refresh';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Reset and re-run all migrations';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($this->isProhibited() ||
|
||||
! $this->confirmToProceed()) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Next we'll gather some of the options so that we can have the right options
|
||||
// to pass to the commands. This includes options such as which database to
|
||||
// use and the path to use for the migration. Then we'll run the command.
|
||||
$database = $this->input->getOption('database');
|
||||
|
||||
$path = $this->input->getOption('path');
|
||||
|
||||
// If the "step" option is specified it means we only want to rollback a small
|
||||
// number of migrations before migrating again. For example, the user might
|
||||
// only rollback and remigrate the latest four migrations instead of all.
|
||||
$step = $this->input->getOption('step') ?: 0;
|
||||
|
||||
if ($step > 0) {
|
||||
$this->runRollback($database, $path, $step);
|
||||
} else {
|
||||
$this->runReset($database, $path);
|
||||
}
|
||||
|
||||
// The refresh command is essentially just a brief aggregate of a few other of
|
||||
// the migration commands and just provides a convenient wrapper to execute
|
||||
// them in succession. We'll also see if we need to re-seed the database.
|
||||
$this->call('migrate', array_filter([
|
||||
'--database' => $database,
|
||||
'--path' => $path,
|
||||
'--realpath' => $this->input->getOption('realpath'),
|
||||
'--force' => true,
|
||||
]));
|
||||
|
||||
if ($this->laravel->bound(Dispatcher::class)) {
|
||||
$this->laravel[Dispatcher::class]->dispatch(
|
||||
new DatabaseRefreshed($database, $this->needsSeeding())
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->needsSeeding()) {
|
||||
$this->runSeeder($database);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the rollback command.
|
||||
*
|
||||
* @param string $database
|
||||
* @param string $path
|
||||
* @param int $step
|
||||
* @return void
|
||||
*/
|
||||
protected function runRollback($database, $path, $step)
|
||||
{
|
||||
$this->call('migrate:rollback', array_filter([
|
||||
'--database' => $database,
|
||||
'--path' => $path,
|
||||
'--realpath' => $this->input->getOption('realpath'),
|
||||
'--step' => $step,
|
||||
'--force' => true,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the reset command.
|
||||
*
|
||||
* @param string $database
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
protected function runReset($database, $path)
|
||||
{
|
||||
$this->call('migrate:reset', array_filter([
|
||||
'--database' => $database,
|
||||
'--path' => $path,
|
||||
'--realpath' => $this->input->getOption('realpath'),
|
||||
'--force' => true,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the developer has requested database seeding.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function needsSeeding()
|
||||
{
|
||||
return $this->option('seed') || $this->option('seeder');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the database seeder command.
|
||||
*
|
||||
* @param string $database
|
||||
* @return void
|
||||
*/
|
||||
protected function runSeeder($database)
|
||||
{
|
||||
$this->call('db:seed', array_filter([
|
||||
'--database' => $database,
|
||||
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
|
||||
'--force' => true,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
|
||||
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
|
||||
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
||||
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
|
||||
['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
|
||||
['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted & re-run'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Console\ConfirmableTrait;
|
||||
use Illuminate\Console\Prohibitable;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(name: 'migrate:reset')]
|
||||
class ResetCommand extends BaseCommand
|
||||
{
|
||||
use ConfirmableTrait, Prohibitable;
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:reset';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Rollback all database migrations';
|
||||
|
||||
/**
|
||||
* The migrator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\Migrator
|
||||
*/
|
||||
protected $migrator;
|
||||
|
||||
/**
|
||||
* Create a new migration rollback command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\Migrator $migrator
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Migrator $migrator)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->migrator = $migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($this->isProhibited() ||
|
||||
! $this->confirmToProceed()) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
return $this->migrator->usingConnection($this->option('database'), function () {
|
||||
// First, we'll make sure that the migration table actually exists before we
|
||||
// start trying to rollback and re-run all of the migrations. If it's not
|
||||
// present we'll just bail out with an info message for the developers.
|
||||
if (! $this->migrator->repositoryExists()) {
|
||||
return $this->components->warn('Migration table not found.');
|
||||
}
|
||||
|
||||
$this->migrator->setOutput($this->output)->reset(
|
||||
$this->getMigrationPaths(), $this->option('pretend')
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
|
||||
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
|
||||
|
||||
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
|
||||
|
||||
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
||||
|
||||
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Console\ConfirmableTrait;
|
||||
use Illuminate\Console\Prohibitable;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand('migrate:rollback')]
|
||||
class RollbackCommand extends BaseCommand
|
||||
{
|
||||
use ConfirmableTrait, Prohibitable;
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:rollback';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Rollback the last database migration';
|
||||
|
||||
/**
|
||||
* The migrator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\Migrator
|
||||
*/
|
||||
protected $migrator;
|
||||
|
||||
/**
|
||||
* Create a new migration rollback command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\Migrator $migrator
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Migrator $migrator)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->migrator = $migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($this->isProhibited() ||
|
||||
! $this->confirmToProceed()) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->migrator->usingConnection($this->option('database'), function () {
|
||||
$this->migrator->setOutput($this->output)->rollback(
|
||||
$this->getMigrationPaths(), [
|
||||
'pretend' => $this->option('pretend'),
|
||||
'step' => (int) $this->option('step'),
|
||||
'batch' => (int) $this->option('batch'),
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
|
||||
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
|
||||
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
||||
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
|
||||
['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'],
|
||||
['batch', null, InputOption::VALUE_REQUIRED, 'The batch of migrations (identified by their batch number) to be reverted'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Stringable;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(name: 'migrate:status')]
|
||||
class StatusCommand extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:status';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Show the status of each migration';
|
||||
|
||||
/**
|
||||
* The migrator instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Migrations\Migrator
|
||||
*/
|
||||
protected $migrator;
|
||||
|
||||
/**
|
||||
* Create a new migration rollback command instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Migrations\Migrator $migrator
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Migrator $migrator)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->migrator = $migrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
return $this->migrator->usingConnection($this->option('database'), function () {
|
||||
if (! $this->migrator->repositoryExists()) {
|
||||
$this->components->error('Migration table not found.');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$ran = $this->migrator->getRepository()->getRan();
|
||||
|
||||
$batches = $this->migrator->getRepository()->getMigrationBatches();
|
||||
|
||||
$migrations = $this->getStatusFor($ran, $batches)
|
||||
->when($this->option('pending') !== false, fn ($collection) => $collection->filter(function ($migration) {
|
||||
return (new Stringable($migration[1]))->contains('Pending');
|
||||
}));
|
||||
|
||||
if (count($migrations) > 0) {
|
||||
$this->newLine();
|
||||
|
||||
$this->components->twoColumnDetail('<fg=gray>Migration name</>', '<fg=gray>Batch / Status</>');
|
||||
|
||||
$migrations
|
||||
->each(
|
||||
fn ($migration) => $this->components->twoColumnDetail($migration[0], $migration[1])
|
||||
);
|
||||
|
||||
$this->newLine();
|
||||
} elseif ($this->option('pending') !== false) {
|
||||
$this->components->info('No pending migrations');
|
||||
} else {
|
||||
$this->components->info('No migrations found');
|
||||
}
|
||||
|
||||
if ($this->option('pending') && $migrations->some(fn ($m) => (new Stringable($m[1]))->contains('Pending'))) {
|
||||
return $this->option('pending');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status for the given run migrations.
|
||||
*
|
||||
* @param array $ran
|
||||
* @param array $batches
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
protected function getStatusFor(array $ran, array $batches)
|
||||
{
|
||||
return (new Collection($this->getAllMigrationFiles()))
|
||||
->map(function ($migration) use ($ran, $batches) {
|
||||
$migrationName = $this->migrator->getMigrationName($migration);
|
||||
|
||||
$status = in_array($migrationName, $ran)
|
||||
? '<fg=green;options=bold>Ran</>'
|
||||
: '<fg=yellow;options=bold>Pending</>';
|
||||
|
||||
if (in_array($migrationName, $ran)) {
|
||||
$status = '['.$batches[$migrationName].'] '.$status;
|
||||
}
|
||||
|
||||
return [$migrationName, $status];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of all of the migration files.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllMigrationFiles()
|
||||
{
|
||||
return $this->migrator->getMigrationFiles($this->getMigrationPaths());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
||||
['pending', null, InputOption::VALUE_OPTIONAL, 'Only list pending migrations', false],
|
||||
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to use'],
|
||||
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Console\Migrations;
|
||||
|
||||
class TableGuesser
|
||||
{
|
||||
const CREATE_PATTERNS = [
|
||||
'/^create_(\w+)_table$/',
|
||||
'/^create_(\w+)$/',
|
||||
];
|
||||
|
||||
const CHANGE_PATTERNS = [
|
||||
'/.+_(to|from|in)_(\w+)_table$/',
|
||||
'/.+_(to|from|in)_(\w+)$/',
|
||||
];
|
||||
|
||||
/**
|
||||
* Attempt to guess the table name and "creation" status of the given migration.
|
||||
*
|
||||
* @param string $migration
|
||||
* @return array
|
||||
*/
|
||||
public static function guess($migration)
|
||||
{
|
||||
foreach (self::CREATE_PATTERNS as $pattern) {
|
||||
if (preg_match($pattern, $migration, $matches)) {
|
||||
return [$matches[1], $create = true];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (self::CHANGE_PATTERNS as $pattern) {
|
||||
if (preg_match($pattern, $migration, $matches)) {
|
||||
return [$matches[2], $create = false];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user