Skip to content

Commit 7036eca

Browse files
author
Andrey Helldar
committed
Added the ability to exclude environments
1 parent bd8a1f2 commit 7036eca

8 files changed

+196
-19
lines changed

README.md

Lines changed: 41 additions & 1 deletion
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.6"
33+
"andrey-helldar/laravel-actions": "^1.7"
3434
}
3535
}
3636
```
@@ -157,6 +157,46 @@ class Reindex extends Actionable
157157

158158
By default, the action will run in all environments. The same will happen if you specify `null` or `[]` as the value.
159159

160+
#### Execution Excluding Certain Environments
161+
162+
In some cases, it becomes necessary to execute an action excluding certain environments. For example `production`.
163+
164+
For this you can use the `$except_environment` parameter:
165+
166+
```php
167+
use Helldar\LaravelActions\Support\Actionable;
168+
169+
class Reindex extends Actionable
170+
{
171+
/** @var string|array|null */
172+
protected $except_environment = 'production';
173+
174+
public function up(): void
175+
{
176+
// your code
177+
}
178+
}
179+
```
180+
181+
You can also specify multiple environment names:
182+
183+
```php
184+
use Helldar\LaravelActions\Support\Actionable;
185+
186+
class Reindex extends Actionable
187+
{
188+
/** @var string|array|null */
189+
protected $except_environment = ['testing', 'staging'];
190+
191+
public function up(): void
192+
{
193+
// your code
194+
}
195+
}
196+
```
197+
198+
By default, no actions will be excluded. The same happens if you specify `null` or `[]` value.
199+
160200
#### Database Transactions
161201

162202
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

src/Support/Actionable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ abstract class Actionable extends Migration implements Contract
4141
*/
4242
protected $environment = null;
4343

44+
/**
45+
* Determines in which environment it should not run.
46+
*
47+
* @var array|string|null
48+
*/
49+
protected $except_environment = null;
50+
4451
/**
4552
* Reverse the actions.
4653
*/
@@ -90,4 +97,14 @@ public function onEnvironment(): array
9097
{
9198
return Arr::wrap($this->environment);
9299
}
100+
101+
/**
102+
* Determines in which environment it should not run.
103+
*
104+
* @return array
105+
*/
106+
public function exceptEnvironment(): array
107+
{
108+
return Arr::wrap($this->except_environment);
109+
}
93110
}

src/Support/Migrator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,18 @@ protected function allowEnvironment($migration): bool
131131
{
132132
$environment = config('app.env', 'production');
133133

134-
$on = $migration->onEnvironment();
134+
$on = $migration->onEnvironment();
135+
$except = $migration->exceptEnvironment();
135136

136-
return empty($on) || in_array($environment, $on);
137+
if (! empty($on) && ! in_array($environment, $on)) {
138+
return false;
139+
}
140+
141+
if (! empty($except) && in_array($environment, $except)) {
142+
return false;
143+
}
144+
145+
return true;
137146
}
138147

139148
/**

tests/Commands/MigrateTest.php

Lines changed: 32 additions & 13 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, 4);
40+
$this->assertDatabaseCount($this->table, 5);
4141
$this->assertDatabaseMigrationDoesntLike($this->table, $table);
4242
$this->artisan('migrate:actions')->run();
4343

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

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

5454
$this->assertDatabaseCount($table, 4);
55-
$this->assertDatabaseCount($this->table, 4);
55+
$this->assertDatabaseCount($this->table, 5);
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
}
@@ -111,20 +112,26 @@ public function testSingleEnvironment()
111112
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
112113
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
113114
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
115+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_production');
116+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
114117
$this->artisan('migrate:actions')->run();
115118

116-
$this->assertDatabaseCount($table, 3);
117-
$this->assertDatabaseCount($this->table, 4);
119+
$this->assertDatabaseCount($table, 4);
120+
$this->assertDatabaseCount($this->table, 5);
118121
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
119122
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
120123
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
124+
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
125+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
121126
$this->artisan('migrate:actions')->run();
122127

123-
$this->assertDatabaseCount($table, 3);
124-
$this->assertDatabaseCount($this->table, 4);
128+
$this->assertDatabaseCount($table, 4);
129+
$this->assertDatabaseCount($this->table, 5);
125130
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
126131
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
127132
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
133+
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
134+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
128135
$this->artisan('migrate:actions')->run();
129136
}
130137

@@ -141,20 +148,32 @@ public function testManyEnvironments()
141148
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
142149
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
143150
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_testing');
151+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_many_environments');
152+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_production');
153+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
154+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_many_environments');
144155
$this->artisan('migrate:actions')->run();
145156

146-
$this->assertDatabaseCount($table, 3);
147-
$this->assertDatabaseCount($this->table, 4);
157+
$this->assertDatabaseCount($table, 4);
158+
$this->assertDatabaseCount($this->table, 5);
148159
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
149160
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
150161
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
162+
$this->assertDatabaseMigrationHas($this->table, 'run_on_many_environments');
163+
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
164+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
165+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_many_environments');
151166
$this->artisan('migrate:actions')->run();
152167

153-
$this->assertDatabaseCount($table, 3);
154-
$this->assertDatabaseCount($this->table, 4);
168+
$this->assertDatabaseCount($table, 4);
169+
$this->assertDatabaseCount($this->table, 5);
155170
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
156171
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
157172
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
173+
$this->assertDatabaseMigrationHas($this->table, 'run_on_many_environments');
174+
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
175+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
176+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_many_environments');
158177
$this->artisan('migrate:actions')->run();
159178
}
160179

tests/Commands/RollbackTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ public function testEnvironment()
5959
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_many_environments');
6060
$this->artisan('migrate:actions')->run();
6161

62-
$this->assertDatabaseCount($table, 3);
63-
$this->assertDatabaseCount($this->table, 4);
62+
$this->assertDatabaseCount($table, 4);
63+
$this->assertDatabaseCount($this->table, 5);
6464
$this->assertDatabaseMigrationHas($this->table, 'run_on_all');
6565
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
6666
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
6767
$this->assertDatabaseMigrationHas($this->table, 'run_on_many_environments');
6868
$this->artisan('migrate:actions')->run();
6969

7070
$this->artisan('migrate:actions:rollback')->run();
71-
$this->assertDatabaseCount($table, 6);
71+
$this->assertDatabaseCount($table, 8);
7272
$this->assertDatabaseCount($this->table, 0);
7373
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_all');
7474
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_on_production');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 RunExceptProduction extends Actionable
9+
{
10+
protected $except_environment = 'production';
11+
12+
public function up(): void
13+
{
14+
$this->table()->insert([
15+
'value' => Uuid::uuid4(),
16+
]);
17+
}
18+
19+
public function down(): void
20+
{
21+
$this->table()->insert([
22+
'value' => Uuid::uuid4(),
23+
]);
24+
}
25+
26+
protected function table(): Builder
27+
{
28+
return DB::table('environment');
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 RunExceptTesting extends Actionable
9+
{
10+
protected $except_environment = 'testing';
11+
12+
public function up(): void
13+
{
14+
$this->table()->insert([
15+
'value' => Uuid::uuid4(),
16+
]);
17+
}
18+
19+
public function down(): void
20+
{
21+
$this->table()->insert([
22+
'value' => Uuid::uuid4(),
23+
]);
24+
}
25+
26+
protected function table(): Builder
27+
{
28+
return DB::table('environment');
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 RunExceptManyEnvironments extends Actionable
9+
{
10+
protected $environment = ['testing'];
11+
12+
protected $except_environment = ['testing', 'production'];
13+
14+
public function up(): void
15+
{
16+
$this->table()->insert([
17+
'value' => Uuid::uuid4(),
18+
]);
19+
}
20+
21+
public function down(): void
22+
{
23+
$this->table()->insert([
24+
'value' => Uuid::uuid4(),
25+
]);
26+
}
27+
28+
protected function table(): Builder
29+
{
30+
return DB::table('environment');
31+
}
32+
}

0 commit comments

Comments
 (0)