Skip to content

Commit b010930

Browse files
author
Andrey Helldar
committed
Added the ability to specify the names of environments to run.
1 parent 4a76147 commit b010930

13 files changed

+355
-11
lines changed

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
3030
```json
3131
{
3232
"require": {
33-
"andrey-helldar/laravel-actions": "^1.5"
33+
"andrey-helldar/laravel-actions": "^1.6"
3434
}
3535
}
3636
```
@@ -117,6 +117,46 @@ If the value is `$once = false`, the `up` method will be called every time the `
117117
In this case, information about it will not be written to the `migration_actions` table and, therefore, the `down` method will not be called when the rollback
118118
command is called.
119119

120+
#### Execution in a specific environment
121+
122+
In some cases, it becomes necessary to execute an action in a specific environment. For example `production`.
123+
124+
For this you can use the `$environment` parameter:
125+
126+
```php
127+
use Helldar\LaravelActions\Support\Actionable;
128+
129+
class Reindex extends Actionable
130+
{
131+
/** @var string|array|null */
132+
protected $environment = 'production';
133+
134+
public function up(): void
135+
{
136+
// your code
137+
}
138+
}
139+
```
140+
141+
You can also specify multiple environment names:
142+
143+
```php
144+
use Helldar\LaravelActions\Support\Actionable;
145+
146+
class Reindex extends Actionable
147+
{
148+
/** @var string|array|null */
149+
protected $environment = ['testing', 'staging'];
150+
151+
public function up(): void
152+
{
153+
// your code
154+
}
155+
}
156+
```
157+
158+
By default, the action will run in all environments. The same will happen if you specify `null` or `[]` as the value.
159+
120160
#### Database Transactions
121161

122162
In some cases, it becomes necessary to undo previously performed actions in the database. For example, when code execution throws an error. To do this, the code
@@ -201,7 +241,11 @@ This package is licensed under the [MIT License](LICENSE).
201241

202242
Available as part of the Tidelift Subscription.
203243

204-
The maintainers of `andrey-helldar/laravel-actions` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. [Learn more](https://tidelift.com/subscription/pkg/packagist-andrey-helldar-laravel-actions?utm_source=packagist-andrey-helldar-laravel-actions&utm_medium=referral&utm_campaign=enterprise&utm_term=repo).
244+
The maintainers of `andrey-helldar/laravel-actions` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for
245+
the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages
246+
you
247+
use. [Learn more](https://tidelift.com/subscription/pkg/packagist-andrey-helldar-laravel-actions?utm_source=packagist-andrey-helldar-laravel-actions&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
248+
.
205249

206250

207251
[badge_build]: https://img.shields.io/github/workflow/status/andrey-helldar/laravel-actions/phpunit?style=flat-square

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function registerMigrateResetCommand(): void
124124

125125
protected function registerMigrateRefreshCommand(): void
126126
{
127-
$this->app->singleton(Command::REFRESH, function ($app) {
127+
$this->app->singleton(Command::REFRESH, function () {
128128
return new Refresh();
129129
});
130130
}

src/Support/Actionable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Helldar\LaravelActions\Contracts\Actionable as Contract;
66
use Illuminate\Database\Migrations\Migration;
7+
use Illuminate\Support\Arr;
78

89
abstract class Actionable extends Migration implements Contract
910
{
@@ -33,6 +34,13 @@ abstract class Actionable extends Migration implements Contract
3334
*/
3435
protected $transaction_attempts = 1;
3536

37+
/**
38+
* Determines which environment to run on.
39+
*
40+
* @var array|string|null
41+
*/
42+
protected $environment = null;
43+
3644
/**
3745
* Reverse the actions.
3846
*/
@@ -72,4 +80,14 @@ public function transactionAttempts(): int
7280
{
7381
return $this->transaction_attempts;
7482
}
83+
84+
/**
85+
* Determines which environment to run on.
86+
*
87+
* @return array
88+
*/
89+
public function onEnvironment(): array
90+
{
91+
return Arr::wrap($this->environment);
92+
}
7593
}

src/Support/Migrator.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ protected function runUp($file, $batch, $pretend)
3737
$name = $this->getMigrationName($file)
3838
);
3939

40+
if (! $this->allowEnvironment($migration)) {
41+
$this->note("<info>Migrate:</info> {$name} was skipped on this environment");
42+
43+
return;
44+
}
45+
4046
if ($pretend) {
41-
return $this->pretendToRun($migration, 'up');
47+
$this->pretendToRun($migration, 'up');
48+
49+
return;
4250
}
4351

4452
$this->note("<comment>Migrating:</comment> {$name}");
@@ -59,6 +67,30 @@ protected function runUp($file, $batch, $pretend)
5967
$this->note("<info>Migrated:</info> {$name} ({$runTime}ms)");
6068
}
6169

70+
/**
71+
* Run "down" a migration instance.
72+
*
73+
* @param string $file
74+
* @param object $migration
75+
* @param bool $pretend
76+
*
77+
* @return void
78+
*/
79+
protected function runDown($file, $migration, $pretend)
80+
{
81+
$instance = $this->resolvePath($file);
82+
83+
$name = $this->getMigrationName($file);
84+
85+
if (! $this->allowEnvironment($instance)) {
86+
$this->note("<info>Roll back:</info> {$name} was skipped on this environment");
87+
88+
return;
89+
}
90+
91+
parent::runDown($file, $migration, $pretend);
92+
}
93+
6294
/**
6395
* Starts the execution of code, starting database transactions, if necessary.
6496
*
@@ -90,6 +122,22 @@ protected function allowLogging($migration): bool
90122
return $migration->isOnce();
91123
}
92124

125+
/**
126+
* Whether the action needs to be executed in the current environment.
127+
*
128+
* @param object $migration
129+
*
130+
* @return bool
131+
*/
132+
protected function allowEnvironment($migration): bool
133+
{
134+
$environment = config('app.env', 'production');
135+
136+
$on = $migration->onEnvironment();
137+
138+
return empty($on) || in_array($environment, $on);
139+
}
140+
93141
/**
94142
* Whether it is necessary to call database transactions at runtime.
95143
*

tests/Commands/MigrateTest.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public function testOnce()
3737
$this->artisan('migrate:actions')->run();
3838

3939
$this->assertDatabaseCount($table, 1);
40-
$this->assertDatabaseCount($this->table, 1);
40+
$this->assertDatabaseCount($this->table, 4);
4141
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
4242
$this->artisan('migrate:actions')->run();
4343

4444
$this->assertDatabaseCount($table, 2);
45-
$this->assertDatabaseCount($this->table, 1);
45+
$this->assertDatabaseCount($this->table, 4);
4646
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
4747
$this->artisan('migrate:actions')->run();
4848

4949
$this->assertDatabaseCount($table, 3);
50-
$this->assertDatabaseCount($this->table, 1);
50+
$this->assertDatabaseCount($this->table, 4);
5151
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
5252
$this->artisan('migrate:actions')->run();
5353

5454
$this->assertDatabaseCount($table, 4);
55-
$this->assertDatabaseCount($this->table, 1);
55+
$this->assertDatabaseCount($this->table, 4);
5656
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
5757
}
5858

@@ -88,7 +88,8 @@ public function testFailedTransaction()
8888

8989
try {
9090
$this->artisan('migrate:actions')->run();
91-
} catch (Exception $e) {
91+
}
92+
catch (Exception $e) {
9293
$this->assertSame(Exception::class, get_class($e));
9394
$this->assertSame('Random message', $e->getMessage());
9495
}
@@ -98,6 +99,66 @@ public function testFailedTransaction()
9899
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
99100
}
100101

102+
public function testSingleEnvironment()
103+
{
104+
$this->copyFiles();
105+
106+
$table = 'environment';
107+
108+
$this->artisan('migrate:actions:install')->run();
109+
110+
$this->assertDatabaseCount($table, 0);
111+
$this->assertDatabaseCount($this->table, 0);
112+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
113+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
114+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
115+
$this->artisan('migrate:actions')->run();
116+
117+
$this->assertDatabaseCount($table, 3);
118+
$this->assertDatabaseCount($this->table, 4);
119+
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
120+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
121+
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
122+
$this->artisan('migrate:actions')->run();
123+
124+
$this->assertDatabaseCount($table, 3);
125+
$this->assertDatabaseCount($this->table, 4);
126+
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
127+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
128+
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
129+
$this->artisan('migrate:actions')->run();
130+
}
131+
132+
public function testManyEnvironments()
133+
{
134+
$this->copyFiles();
135+
136+
$table = 'environment';
137+
138+
$this->artisan('migrate:actions:install')->run();
139+
140+
$this->assertDatabaseCount($table, 0);
141+
$this->assertDatabaseCount($this->table, 0);
142+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
143+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
144+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
145+
$this->artisan('migrate:actions')->run();
146+
147+
$this->assertDatabaseCount($table, 3);
148+
$this->assertDatabaseCount($this->table, 4);
149+
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
150+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
151+
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
152+
$this->artisan('migrate:actions')->run();
153+
154+
$this->assertDatabaseCount($table, 3);
155+
$this->assertDatabaseCount($this->table, 4);
156+
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
157+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
158+
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
159+
$this->artisan('migrate:actions')->run();
160+
}
161+
101162
public function testMigrationNotFound()
102163
{
103164
$this->assertDatabaseDoesntTable($this->table);

tests/Commands/RollbackTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,37 @@ public function testRollbackCommand()
4242
$this->assertDatabaseMigrationHas($this->table, 'rollback_two');
4343
$this->assertDatabaseMigrationHas($this->table, 'rollback_tree');
4444
}
45+
46+
public function testEnvironment()
47+
{
48+
$this->copyFiles();
49+
50+
$table = 'environment';
51+
52+
$this->artisan('migrate:actions:install')->run();
53+
54+
$this->assertDatabaseCount($table, 0);
55+
$this->assertDatabaseCount($this->table, 0);
56+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
57+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
58+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
59+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_many_environments');
60+
$this->artisan('migrate:actions')->run();
61+
62+
$this->assertDatabaseCount($table, 3);
63+
$this->assertDatabaseCount($this->table, 4);
64+
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
65+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
66+
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
67+
$this->assertDatabaseMigrationHas($this->table, 'run_on_many_environments');
68+
$this->artisan('migrate:actions')->run();
69+
70+
$this->artisan('migrate:actions:rollback')->run();
71+
$this->assertDatabaseCount($table, 6);
72+
$this->assertDatabaseCount($this->table, 0);
73+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
74+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
75+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
76+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_many_environments');
77+
}
4578
}

tests/fixtures/actions/2020_12_07_153105_foo_bar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Helldar\LaravelActions\Support\Actionable;
4+
use Illuminate\Database\Query\Builder;
45
use Illuminate\Support\Facades\DB;
56
use Ramsey\Uuid\Uuid;
67

@@ -18,7 +19,7 @@ public function down(): void
1819
$this->table()->truncate();
1920
}
2021

21-
protected function table()
22+
protected function table(): Builder
2223
{
2324
return DB::table('test');
2425
}

tests/fixtures/actions/2021_01_02_020947_every_time.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Helldar\LaravelActions\Support\Actionable;
4+
use Illuminate\Database\Query\Builder;
45
use Illuminate\Support\Facades\DB;
56
use Ramsey\Uuid\Uuid;
67

@@ -15,7 +16,7 @@ public function up(): void
1516
]);
1617
}
1718

18-
protected function table()
19+
protected function table(): Builder
1920
{
2021
return DB::table('every_time');
2122
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Helldar\LaravelActions\Support\Actionable;
4+
use Illuminate\Database\Query\Builder;
5+
use Illuminate\Support\Facades\DB;
6+
use Ramsey\Uuid\Uuid;
7+
8+
final class RunOnAll extends Actionable
9+
{
10+
public function up(): void
11+
{
12+
$this->table()->insert([
13+
'value' => Uuid::uuid4(),
14+
]);
15+
}
16+
17+
public function down(): void
18+
{
19+
$this->table()->insert([
20+
'value' => Uuid::uuid4(),
21+
]);
22+
}
23+
24+
protected function table(): Builder
25+
{
26+
return DB::table('environment');
27+
}
28+
}

0 commit comments

Comments
 (0)