Skip to content

Commit

Permalink
add path
Browse files Browse the repository at this point in the history
  • Loading branch information
pxianyu committed May 26, 2023
1 parent c39d644 commit bea07be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/Command/CreateMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function configure()
->addArgument('name', InputArgument::REQUIRED, 'The migration name')
->addOption('--create', null, InputOption::VALUE_REQUIRED, 'The table to create')
->addOption('--table', null, InputOption::VALUE_REQUIRED, 'The table to migrate')
->addOption('--path', null, InputOption::VALUE_REQUIRED, 'create file path')
->setHelp('Creates a new migration' . PHP_EOL);

parent::configure();
Expand All @@ -36,14 +37,15 @@ public function execute(InputInterface $input, OutputInterface $output)
$name = Str::snake(trim($this->input->getArgument('name')));
$table = $this->input->getOption('table');
$create = $this->input->getOption('create') ?: false;
$path = $this->input->getOption('path');
if (! $table && is_string($create)) {
$table = $create;
$create = true;
}
if (! $table) {
[$table, $create] = TableGuesser::guess($name);
}
$this->writeMigration($name, $table, $create);
$this->writeMigration($name, $table, $create, $path);

return 0;
}
Expand All @@ -56,11 +58,11 @@ public function execute(InputInterface $input, OutputInterface $output)
* @param bool $create
* @throws \Exception
*/
protected function writeMigration(string $name, string $table, bool $create)
protected function writeMigration(string $name, string $table, bool $create, string $path)
{
$file = $this->creator->create(
$name,
$this->getMigrationPath(),
$this->getMigrationPath().DIRECTORY_SEPARATOR.$path,
$table,
$create
);
Expand Down
12 changes: 8 additions & 4 deletions src/Command/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Migrate extends AbstractCommand
{
protected static $defaultName = 'migrate:migrate';
protected static $defaultName = 'migrate:run';

protected BaseMigrator $migrator;
protected DatabaseMigrationRepository $repository;
Expand All @@ -25,6 +25,7 @@ protected function configure()
->addOption('dry-run', 'x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
->addOption('step', 's', InputOption::VALUE_REQUIRED, 'Force the migrations to be run so they can be rolled back individually', 1)
->addOption('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production')
->addOption('path','p',InputOption::VALUE_OPTIONAL, 'file path')
->setHelp('Runs all available migrations' . PHP_EOL);

parent::configure();
Expand All @@ -37,19 +38,22 @@ public function execute(InputInterface $input, OutputInterface $output)
if (! $this->confirmToProceed()) {
return 0;
}

$migrationPath=$this->getMigrationPath();
if ($input->getOption('path')) {
$migrationPath = $migrationPath.DIRECTORY_SEPARATOR.$input->getOption('path');
}
$this->repository = new DatabaseMigrationRepository($this->getDb(), $this->getMigrationTable());
$this->migrator = new Migrator($this->repository, $this->getDb(), new Filesystem());
$this->migrator->setOutput($output);

$this->migrator->usingConnection($this->database, function () use ($output, $input) {
$this->migrator->usingConnection($this->database, function () use ($output, $input, $migrationPath) {
$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(new OutputStyle($input, $output))
->run([$this->getMigrationPath()], [
->run([$migrationPath], [
'pretend' => $this->input->getOption('dry-run'),
'step' => (int)$this->input->getOption('step'),
]);
Expand Down

0 comments on commit bea07be

Please sign in to comment.