Skip to content

Commit e4dce84

Browse files
author
Andrey Helldar
committed
Added execution every time
1 parent 6ce56d9 commit e4dce84

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ database, you will be prompted for confirmation before the commands are executed
109109
php artisan migrate:actions --force
110110
```
111111

112+
#### Execution every time
113+
114+
In some cases, you need to call the code every time you deploy the application. For example, to call reindexing.
115+
116+
To do this, override the `$once` variable in the action file:
117+
118+
```php
119+
use Helldar\LaravelActions\Support\Actionable;
120+
121+
class Reindex extends Actionable
122+
{
123+
protected $once = false;
124+
125+
public function up(): void
126+
{
127+
// your calling code
128+
}
129+
130+
public function down(): void
131+
{
132+
//
133+
}
134+
}
135+
```
136+
137+
If the value is `$once = false`, the `up` method will be called every time the `migrate:actions` command called.
138+
139+
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
140+
command is called.
141+
112142
### Rolling Back Actions
113143

114144
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

src/Support/Actionable.php

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

88
abstract class Actionable extends Migration implements Contract
99
{
10+
protected $once = true;
11+
12+
public function isOnce(): bool
13+
{
14+
return $this->once;
15+
}
1016
}

src/Support/Migrator.php

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

0 commit comments

Comments
 (0)