Skip to content

Commit 425ab13

Browse files
author
Andrey Helldar
authored
Merge pull request #4 from andrey-helldar/features/once
Features/once
2 parents d239e10 + a4197e3 commit 425ab13

17 files changed

+258
-38
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ tests/ export-ignore
99
.gitignore export-ignore
1010
.scrutinizer.yml export-ignore
1111
.styleci.yml export-ignore
12-
phpunit.yml export-ignore
12+
13+
phpunit.xml export-ignore

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [Generating actions](#generating-actions)
2727
* [Running Actions](#running-actions)
2828
* [Forcing Actions To Run In Production](#forcing-actions-to-run-in-production)
29+
* [Execution every time](#execution-every-time)
2930
* [Rolling Back Actions](#rolling-back-actions)
3031
* [Roll Back & Action Using A Single Command](#roll-back--action-using-a-single-command)
3132
* [Actions Status](#actions-status)
@@ -109,6 +110,44 @@ database, you will be prompted for confirmation before the commands are executed
109110
php artisan migrate:actions --force
110111
```
111112

113+
#### Execution every time
114+
115+
In some cases, you need to call the code every time you deploy the application. For example, to call reindexing.
116+
117+
To do this, override the `$once` variable in the action file:
118+
119+
```php
120+
use Helldar\LaravelActions\Support\Actionable;
121+
122+
class Reindex extends Actionable
123+
{
124+
/**
125+
* Determines the type of launch of the action.
126+
*
127+
* If true, then it will be executed once.
128+
* If false, then the action will run every time the `migrate:actions` command is invoked.
129+
*
130+
* @var bool
131+
*/
132+
protected $once = false;
133+
134+
public function up(): void
135+
{
136+
// your calling code
137+
}
138+
139+
public function down(): void
140+
{
141+
//
142+
}
143+
}
144+
```
145+
146+
If the value is `$once = false`, the `up` method will be called every time the `migrate:actions` command called.
147+
148+
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
149+
command is called.
150+
112151
### Rolling Back Actions
113152

114153
To roll back the latest action operation, you may use the `rollback` command. This command rolls back the last "batch" of actions, which may include multiple

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"require-dev": {
2525
"mockery/mockery": "^1.3.1",
2626
"orchestra/testbench": "^4.0|^5.0|^6.0",
27-
"phpunit/phpunit": "^8.0|^9.0"
27+
"phpunit/phpunit": "^8.0|^9.0",
28+
"ramsey/uuid": "^3.7|^4.0"
2829
},
2930
"autoload": {
3031
"psr-4": {

phpunit.xml

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnError="false"
11-
stopOnFailure="false"
12-
verbose="true"
2+
<phpunit
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
bootstrap="vendor/autoload.php"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnError="false"
14+
stopOnFailure="false"
15+
verbose="true"
1316
>
17+
<coverage processUncoveredFiles="true">
18+
<include>
19+
<directory suffix=".php">./src</directory>
20+
</include>
21+
<report>
22+
<clover outputFile="build/logs/clover.xml"/>
23+
<html outputDirectory="build/logs/coverage"/>
24+
<text outputFile="build/logs/coverage.txt"/>
25+
</report>
26+
</coverage>
1427
<php>
1528
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
1629
</php>
@@ -19,14 +32,4 @@
1932
<directory suffix="Test.php">./tests</directory>
2033
</testsuite>
2134
</testsuites>
22-
<filter>
23-
<whitelist processUncoveredFilesFromWhitelist="true">
24-
<directory suffix=".php">./src</directory>
25-
</whitelist>
26-
</filter>
27-
<logging>
28-
<log type="coverage-clover" target="build/logs/clover.xml"/>
29-
<log type="coverage-text" target="build/logs/coverage.txt"/>
30-
<log type="coverage-html" target="build/logs/coverage"/>
31-
</logging>
3235
</phpunit>

src/Console/Reset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Helldar\LaravelActions\Console;
44

55
use Helldar\LaravelActions\Constants\Names;
6+
use Helldar\LaravelActions\Support\Migrator;
67
use Helldar\LaravelActions\Traits\Database;
78
use Helldar\LaravelActions\Traits\Infoable;
89
use Helldar\LaravelActions\Traits\Optionable;
910
use Illuminate\Console\ConfirmableTrait;
1011
use Illuminate\Database\Console\Migrations\BaseCommand;
11-
use Illuminate\Database\Migrations\Migrator;
1212
use Symfony\Component\Console\Input\InputOption;
1313

1414
final class Reset extends BaseCommand
@@ -35,14 +35,14 @@ final class Reset extends BaseCommand
3535
/**
3636
* The migrator instance.
3737
*
38-
* @var \Illuminate\Database\Migrations\Migrator
38+
* @var \Helldar\LaravelActions\Support\Migrator
3939
*/
4040
protected $migrator;
4141

4242
/**
4343
* Create a new migration rollback command instance.
4444
*
45-
* @param \Illuminate\Database\Migrations\Migrator $migrator
45+
* @param \Helldar\LaravelActions\Support\Migrator $migrator
4646
*/
4747
public function __construct(Migrator $migrator)
4848
{

src/Contracts/Actionable.php

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

55
interface Actionable
66
{
7+
/**
8+
* Run the actions.
9+
*/
710
public function up(): void;
811

12+
/**
13+
* Reverse the actions.
14+
*/
915
public function down(): void;
1016
}

src/Support/Actionable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,26 @@
77

88
abstract class Actionable extends Migration implements Contract
99
{
10+
/**
11+
* Determines the type of launch of the action.
12+
*
13+
* If true, then it will be executed once.
14+
* If false, then the action will run every time the `migrate:actions` command is invoked.
15+
*
16+
* @var bool
17+
*/
18+
protected $once = true;
19+
20+
/**
21+
* Determines the type of launch of the action.
22+
*
23+
* If true, then it will be executed once.
24+
* If false, then the action will run every time the `migrate:actions` command is invoked.
25+
*
26+
* @return bool
27+
*/
28+
public function isOnce(): bool
29+
{
30+
return $this->once;
31+
}
1032
}

src/Support/Migrator.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,54 @@ public function usingConnection($name, callable $callback)
1919
$this->setConnection($prev);
2020
});
2121
}
22+
23+
/**
24+
* Run "up" a migration instance.
25+
*
26+
* @param string $file
27+
* @param int $batch
28+
* @param bool $pretend
29+
*/
30+
protected function runUp($file, $batch, $pretend)
31+
{
32+
// First we will resolve a "real" instance of the migration class from this
33+
// migration file name. Once we have the instances we can run the actual
34+
// command such as "up" or "down", or we can just simulate the action.
35+
$migration = $this->resolve(
36+
$name = $this->getMigrationName($file)
37+
);
38+
39+
if ($pretend) {
40+
return $this->pretendToRun($migration, 'up');
41+
}
42+
43+
$this->note("<comment>Migrating:</comment> {$name}");
44+
45+
$startTime = microtime(true);
46+
47+
$this->runMigration($migration, 'up');
48+
49+
$runTime = number_format((microtime(true) - $startTime) * 1000, 2);
50+
51+
// Once we have run a migrations class, we will log that it was run in this
52+
// repository so that we don't try to run it next time we do a migration
53+
// in the application. A migration repository keeps the migrate order.
54+
if ($this->allowLogging($migration)) {
55+
$this->repository->log($name, $batch);
56+
}
57+
58+
$this->note("<info>Migrated:</info> {$name} ({$runTime}ms)");
59+
}
60+
61+
/**
62+
* Whether it is necessary to record information about the execution in the database.
63+
*
64+
* @param object $migration
65+
*
66+
* @return bool
67+
*/
68+
protected function allowLogging($migration): bool
69+
{
70+
return $migration->isOnce();
71+
}
2272
}

tests/Commands/MigrateTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,40 @@ public function testMigrationCommand()
1919
$this->artisan('migrate:actions')->run();
2020

2121
$this->assertDatabaseCount($this->table, 1);
22-
$this->assertDatabaseHasLike($this->table, 'migration', 'test_migration');
22+
$this->assertDatabaseMigrationHas($this->table, 'test_migration');
23+
}
24+
25+
public function testEveryTimeExecution()
26+
{
27+
$this->copyFiles();
28+
29+
$table = 'every_time';
30+
31+
$this->artisan('migrate:actions:install')->run();
32+
33+
$this->assertDatabaseCount($table, 0);
34+
$this->assertDatabaseCount($this->table, 0);
35+
$this->assertDatabaseMigrationDoesntLike($this->table, 'every_time');
36+
$this->artisan('migrate:actions')->run();
37+
38+
$this->assertDatabaseCount($table, 1);
39+
$this->assertDatabaseCount($this->table, 1);
40+
$this->assertDatabaseMigrationDoesntLike($this->table, 'every_time');
41+
$this->artisan('migrate:actions')->run();
42+
43+
$this->assertDatabaseCount($table, 2);
44+
$this->assertDatabaseCount($this->table, 1);
45+
$this->assertDatabaseMigrationDoesntLike($this->table, 'every_time');
46+
$this->artisan('migrate:actions')->run();
47+
48+
$this->assertDatabaseCount($table, 3);
49+
$this->assertDatabaseCount($this->table, 1);
50+
$this->assertDatabaseMigrationDoesntLike($this->table, 'every_time');
51+
$this->artisan('migrate:actions')->run();
52+
53+
$this->assertDatabaseCount($table, 4);
54+
$this->assertDatabaseCount($this->table, 1);
55+
$this->assertDatabaseMigrationDoesntLike($this->table, 'every_time');
2356
}
2457

2558
public function testMigrationNotFound()

tests/Commands/RefreshTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function testRefreshCommand()
2323
$this->artisan('migrate:actions:refresh')->run();
2424

2525
$this->assertDatabaseCount($this->table, 1);
26-
$this->assertDatabaseHasLike($this->table, 'migration', 'refresh');
26+
$this->assertDatabaseMigrationHas($this->table, 'refresh');
2727
}
2828
}

0 commit comments

Comments
 (0)