From a77258a78e5beb1f078fdc9ca5877c2cdfe46711 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 28 Jan 2022 22:45:49 +0300 Subject: [PATCH] Fixed class naming error in Laravel without anonymous migrations --- README.md | 42 ++++++++++++++++++++++++++++++++++- src/Concerns/Argumentable.php | 20 +++++++++++++---- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cfcb46d9..fc353f4c 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Or manually update `require` block of `composer.json` and run `composer update`. ```json { "require": { - "dragon-code/laravel-migration-actions": "^2.4" + "dragon-code/laravel-migration-actions": "^2.6" } } ``` @@ -79,6 +79,46 @@ The new action will be placed in your `database/actions` directory. Each action > > If you execute `migrate:actions` with the first command, the `migrate:actions:install` command will be called automatically. +#### Automatically Generate A File Name + +If you are not worried about the names of your files, then in version [2.6](https://github.com/TheDragonCode/laravel-migration-actions/releases/tag/v2.6.0) we added the ability to +automatically generate file names. + +Just don't include the name attribute when creating the migration. + +If a git repository is found in the main folder, then the name of the current active branch will be taken as a prefix: + +```bash +php artisan make:migration:action + +# 2022_01_28_184116_main_1643384476.php +# 2022_01_28_184117_main_1643384477.php +# 2022_01_28_184118_crm_2345_1643384478.php +# 2022_01_28_184119_crm_2345_1643384479.php +``` + +If the git repository is not found, then the default prefix will be used: + +```bash +php artisan make:migration:action + +# 2022_01_28_184116_auto_1643384476.php +# 2022_01_28_184117_auto_1643384477.php +# 2022_01_28_184118_auto_1643384478.php +``` + +If you are using Laravel prior to version [8.37](https://github.com/laravel/framework/releases/tag/v8.37.0), then to ensure backward compatibility, if the current git repository +branch name starts with a number, the `branch` prefix will be automatically added to it: + +```bash +php artisan make:migration:action +``` + +```php +/* 2022_01_28_184116_branch_2x_1643384476.php */ +class Branch2x1643384476 extends Actionable { } +``` + ### Running actions To run all of your outstanding actions, execute the `migrate:actions` Artisan command: diff --git a/src/Concerns/Argumentable.php b/src/Concerns/Argumentable.php index 8b5952c1..876d1f7c 100644 --- a/src/Concerns/Argumentable.php +++ b/src/Concerns/Argumentable.php @@ -7,22 +7,34 @@ /** @mixin \Illuminate\Console\Command */ trait Argumentable { + protected $auto_prefix = 'auto'; + + protected $branch_prefix = 'branch'; + protected function argumentName(): string { if ($name = (string) $this->argument('name')) { return trim($name); } - return $this->getNamePrefix() . '_' . time(); + return $this->getAutoPrefix() . '_' . time(); } - protected function getNamePrefix(): string + protected function getAutoPrefix(): string { - return $this->getGitBranchName() ?: 'auto'; + return $this->getGitBranchName() ?: $this->auto_prefix; } protected function getGitBranchName(): ?string { - return Git::currentBranch(base_path('.git')); + $name = Git::currentBranch(base_path('.git')); + + preg_match('/^\d.*$/', $name, $output); + + if (! empty($output)) { + return $this->branch_prefix . '_' . $name; + } + + return $name; } }