Skip to content

Commit

Permalink
Merge pull request #41 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Fixed class naming error in Laravel without anonymous migrations
  • Loading branch information
Andrey Helldar authored Jan 28, 2022
2 parents 391b6f8 + a77258a commit 28dcad9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 16 additions & 4 deletions src/Concerns/Argumentable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 28dcad9

Please sign in to comment.