Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Include a optional flag subpath for rollbacking module's specific migration file #1626

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Commands/MigrateRollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function rollback($module)
$module = $this->module->findOrFail($module);
}

$migrator = new Migrator($module, $this->getLaravel());
$migrator = new Migrator($module, $this->getLaravel(), $this->option('subpath'));

$database = $this->option('database');

Expand Down Expand Up @@ -111,6 +111,7 @@ protected function getOptions()
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
['subpath', null, InputOption::VALUE_OPTIONAL, 'Indicate a subpath for modules specific migration file']
];
}
}
18 changes: 16 additions & 2 deletions src/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Migrator
*/
protected $laravel;

/**
* Optional subpath for specific migration file.
*
* @var string|null
* @example subpath 2000_01_01_000000_create_example_table.php
*/
protected $subpath = '';

/**
* The database connection to be used
*
Expand All @@ -34,11 +42,13 @@ class Migrator
* Create new instance.
* @param Module $module
* @param Application $application
* @param string|null $subpath
*/
public function __construct(Module $module, Application $application)
public function __construct(Module $module, Application $application, $subpath = null)
{
$this->module = $module;
$this->laravel = $application;
$this->subpath = $subpath;
}

/**
Expand Down Expand Up @@ -88,7 +98,11 @@ public function getPath()
*/
public function getMigrations($reverse = false)
{
$files = $this->laravel['files']->glob($this->getPath() . '/*_*.php');
if (!empty($this->subpath)) {
$files = $this->laravel['files']->glob($this->getPath() . '/' . $this->subpath);
} else {
$files = $this->laravel['files']->glob($this->getPath() . '/*_*.php');
}

// Once we have the array of files in the directory we will just remove the
// extension and take the basename of the file which is all we need when
Expand Down