Skip to content

Commit 28dcad9

Browse files
author
Andrey Helldar
authored
Merge pull request #41 from TheDragonCode/2.x
Fixed class naming error in Laravel without anonymous migrations
2 parents 391b6f8 + a77258a commit 28dcad9

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
2323
```json
2424
{
2525
"require": {
26-
"dragon-code/laravel-migration-actions": "^2.4"
26+
"dragon-code/laravel-migration-actions": "^2.6"
2727
}
2828
}
2929
```
@@ -79,6 +79,46 @@ The new action will be placed in your `database/actions` directory. Each action
7979
>
8080
> If you execute `migrate:actions` with the first command, the `migrate:actions:install` command will be called automatically.
8181
82+
#### Automatically Generate A File Name
83+
84+
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
85+
automatically generate file names.
86+
87+
Just don't include the name attribute when creating the migration.
88+
89+
If a git repository is found in the main folder, then the name of the current active branch will be taken as a prefix:
90+
91+
```bash
92+
php artisan make:migration:action
93+
94+
# 2022_01_28_184116_main_1643384476.php
95+
# 2022_01_28_184117_main_1643384477.php
96+
# 2022_01_28_184118_crm_2345_1643384478.php
97+
# 2022_01_28_184119_crm_2345_1643384479.php
98+
```
99+
100+
If the git repository is not found, then the default prefix will be used:
101+
102+
```bash
103+
php artisan make:migration:action
104+
105+
# 2022_01_28_184116_auto_1643384476.php
106+
# 2022_01_28_184117_auto_1643384477.php
107+
# 2022_01_28_184118_auto_1643384478.php
108+
```
109+
110+
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
111+
branch name starts with a number, the `branch` prefix will be automatically added to it:
112+
113+
```bash
114+
php artisan make:migration:action
115+
```
116+
117+
```php
118+
/* 2022_01_28_184116_branch_2x_1643384476.php */
119+
class Branch2x1643384476 extends Actionable { }
120+
```
121+
82122
### Running actions
83123

84124
To run all of your outstanding actions, execute the `migrate:actions` Artisan command:

src/Concerns/Argumentable.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,34 @@
77
/** @mixin \Illuminate\Console\Command */
88
trait Argumentable
99
{
10+
protected $auto_prefix = 'auto';
11+
12+
protected $branch_prefix = 'branch';
13+
1014
protected function argumentName(): string
1115
{
1216
if ($name = (string) $this->argument('name')) {
1317
return trim($name);
1418
}
1519

16-
return $this->getNamePrefix() . '_' . time();
20+
return $this->getAutoPrefix() . '_' . time();
1721
}
1822

19-
protected function getNamePrefix(): string
23+
protected function getAutoPrefix(): string
2024
{
21-
return $this->getGitBranchName() ?: 'auto';
25+
return $this->getGitBranchName() ?: $this->auto_prefix;
2226
}
2327

2428
protected function getGitBranchName(): ?string
2529
{
26-
return Git::currentBranch(base_path('.git'));
30+
$name = Git::currentBranch(base_path('.git'));
31+
32+
preg_match('/^\d.*$/', $name, $output);
33+
34+
if (! empty($output)) {
35+
return $this->branch_prefix . '_' . $name;
36+
}
37+
38+
return $name;
2739
}
2840
}

0 commit comments

Comments
 (0)