diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index ab3298cb..59e9d178 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -28,6 +28,11 @@ public function connection(): ?string return $this->config->get('deploy-operations.connection'); } + public function transactionAttempts(): int + { + return $this->config->get('deploy-operations.transactions.attempts', 1); + } + public function table(): string { return $this->config->get('deploy-operations.table'); diff --git a/src/Operation.php b/src/Operation.php index 9f53d141..b6a94fa5 100644 --- a/src/Operation.php +++ b/src/Operation.php @@ -3,64 +3,11 @@ namespace DragonCode\LaravelDeployOperations; use DragonCode\LaravelDeployOperations\Concerns\Artisan; -use Illuminate\Support\Arr; abstract class Operation { use Artisan; - /** - * Determines the type of launch of the deploy operation. - * - * If true, then it will be executed once. - * If false, then the operation will run every time the `operations` command is invoked. - * - * @deprecated Will be removed in 7.x version. Use `shouldOnce` method instead. - */ - protected bool $once = true; - - /** - * Determines which environment to run on. - * - * @deprecated Will be removed in 7.x version. Use `shouldRun` method instead. - */ - protected array|string|null $environment = null; - - /** - * Determines in which environment it should not run. - * - * @deprecated Will be removed in 7.x version. Use `shouldRun` method instead. - */ - protected array|string|null $exceptEnvironment = null; - - /** - * Defines a possible "pre-launch" of the operation. - * - * @deprecated Will be removed in 7.x version. Use `hasBefore` method instead. - */ - protected bool $before = true; - - /** - * @deprecated - */ - public function getConnection(): ?string - { - return config('deploy-operations.connection'); - } - - /** - * Determines the type of launch of the deploy operation. - * - * If true, then it will be executed once. - * If false, then the operation will run every time the `operations` command is invoked. - * - * @deprecated Will be removed in 7.x version. Use `shouldOnce` method instead. - */ - public function isOnce(): bool - { - return $this->once; - } - /** * Determines the type of launch of the deploy operation. * @@ -69,17 +16,7 @@ public function isOnce(): bool */ public function shouldOnce(): bool { - return $this->isOnce(); - } - - /** - * Determines a call to database transactions. - * - * @deprecated Will be removed in 7.x version. Use `withinTransactions` method instead. - */ - public function enabledTransactions(): bool - { - return (bool) config('deploy-operations.transactions.enabled'); + return true; } /** @@ -87,47 +24,7 @@ public function enabledTransactions(): bool */ public function withinTransactions(): bool { - return $this->enabledTransactions(); - } - - /** - * The number of attempts to execute a request within a transaction before throwing an error. - * - * @deprecated Will be removed in 7.x version. Set the value in the `config/deploy-operations.php` settings file. - */ - public function transactionAttempts(): int - { - return config('deploy-operations.transactions.attempts', 1); - } - - /** - * Determines which environment to run on. - * - * @deprecated Will be removed in 7.x version. Use `shouldRun` method instead. - */ - public function onEnvironment(): array - { - return Arr::wrap($this->environment); - } - - /** - * Determines in which environment it should not run. - * - * @deprecated Will be removed in 7.x version. Use `shouldRun` method instead. - */ - public function exceptEnvironment(): array - { - return Arr::wrap($this->exceptEnvironment); - } - - /** - * Determines whether the given operation can be called conditionally. - * - * @deprecated Will be removed in 7.x version. Use `shouldRun` method instead. - */ - public function allow(): bool - { - return true; + return (bool) config('deploy-operations.transactions.enabled'); } /** @@ -135,24 +32,7 @@ public function allow(): bool */ public function shouldRun(): bool { - $env = app()->environment(); - - $on = $this->onEnvironment(); - $except = $this->exceptEnvironment(); - - return $this->allow() - && (empty($on) || in_array($env, $on, true)) - && (empty($except) || ! in_array($env, $except, true)); - } - - /** - * Defines a possible "pre-launch" of the operation. - * - * @deprecated Will be removed in 7.x version. Use `needBefore` method instead. - */ - public function hasBefore(): bool - { - return $this->before; + return true; } /** @@ -160,17 +40,7 @@ public function hasBefore(): bool */ public function needBefore(): bool { - return $this->hasBefore(); - } - - /** - * Defines whether the operation will run synchronously or asynchronously. - * - * @deprecated Will be removed in 7.x version. Use `shouldBeAsync` method instead. - */ - public function isAsync(): bool - { - return (bool) config('deploy-operations.async'); + return true; } /** @@ -178,7 +48,7 @@ public function isAsync(): bool */ public function shouldBeAsync(): bool { - return $this->isAsync(); + return (bool) config('deploy-operations.async'); } /** diff --git a/src/Services/Migrator.php b/src/Services/Migrator.php index 7914452b..4121d02f 100644 --- a/src/Services/Migrator.php +++ b/src/Services/Migrator.php @@ -92,12 +92,7 @@ protected function runOperation(Operation $operation, string $method): void { if ($this->hasOperation($operation, $method)) { try { - $this->runMethod( - $operation, - $method, - $operation->withinTransactions(), - $operation->transactionAttempts() - ); + $this->runMethod($operation, $method, $operation->withinTransactions()); $operation->success(); } @@ -119,11 +114,11 @@ protected function hasAsync(Operation $operation, Options $options): bool return ! $options->sync && $operation->shouldBeAsync(); } - protected function runMethod(Operation $operation, string $method, bool $transactions, int $attempts): void + protected function runMethod(Operation $operation, string $method, bool $transactions): void { $callback = fn () => $this->container->call([$operation, $method]); - $transactions ? DB::transaction($callback, $attempts) : $callback(); + $transactions ? DB::transaction($callback, $this->config->transactionAttempts()) : $callback(); } protected function log(string $name, int $batch): void diff --git a/tests/fixtures/app/async/2021_04_06_212742_every_time.php b/tests/fixtures/app/async/2021_04_06_212742_every_time.php index 11444624..cf13b4c9 100644 --- a/tests/fixtures/app/async/2021_04_06_212742_every_time.php +++ b/tests/fixtures/app/async/2021_04_06_212742_every_time.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected bool $once = false; - public function __invoke(): void { $this->table()->insert([ @@ -20,7 +18,12 @@ protected function table(): Builder return DB::table('every_time'); } - public function isAsync(): bool + public function shouldOnce(): bool + { + return false; + } + + public function shouldBeAsync(): bool { return true; } diff --git a/tests/fixtures/app/async/2023_04_06_212637_foo_bar.php b/tests/fixtures/app/async/2023_04_06_212637_foo_bar.php index 3d0cb95d..db8bdae6 100644 --- a/tests/fixtures/app/async/2023_04_06_212637_foo_bar.php +++ b/tests/fixtures/app/async/2023_04_06_212637_foo_bar.php @@ -18,7 +18,7 @@ protected function table(): Builder return DB::table('test'); } - public function isAsync(): bool + public function shouldBeAsync(): bool { return true; } diff --git a/tests/fixtures/app/operations/2021_01_02_020947_every_time.php b/tests/fixtures/app/operations/2021_01_02_020947_every_time.php index 15406867..4d04b6d1 100644 --- a/tests/fixtures/app/operations/2021_01_02_020947_every_time.php +++ b/tests/fixtures/app/operations/2021_01_02_020947_every_time.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected bool $once = false; - public function up(): void { $this->table()->insert([ @@ -19,4 +17,9 @@ protected function table(): Builder { return DB::table('every_time'); } + + public function shouldOnce(): bool + { + return false; + } }; diff --git a/tests/fixtures/app/operations/2021_05_24_120003_run_on_many_environments.php b/tests/fixtures/app/operations/2021_05_24_120003_run_on_many_environments.php index d2105703..9745e9f3 100644 --- a/tests/fixtures/app/operations/2021_05_24_120003_run_on_many_environments.php +++ b/tests/fixtures/app/operations/2021_05_24_120003_run_on_many_environments.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $environment = ['testing', 'production']; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return in_array(app()->environment(), ['testing', 'production'], true); + } }; diff --git a/tests/fixtures/app/operations/2021_05_24_120003_run_on_production.php b/tests/fixtures/app/operations/2021_05_24_120003_run_on_production.php index 9ab83dd2..33834c74 100644 --- a/tests/fixtures/app/operations/2021_05_24_120003_run_on_production.php +++ b/tests/fixtures/app/operations/2021_05_24_120003_run_on_production.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $environment = 'production'; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return app()->environment() === 'production'; + } }; diff --git a/tests/fixtures/app/operations/2021_05_24_120003_run_on_testing.php b/tests/fixtures/app/operations/2021_05_24_120003_run_on_testing.php index 4cc72384..36773d2a 100644 --- a/tests/fixtures/app/operations/2021_05_24_120003_run_on_testing.php +++ b/tests/fixtures/app/operations/2021_05_24_120003_run_on_testing.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $environment = 'testing'; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return app()->environment() === 'testing'; + } }; diff --git a/tests/fixtures/app/operations/2021_06_07_132849_run_except_production.php b/tests/fixtures/app/operations/2021_06_07_132849_run_except_production.php index f7d74868..49e51fb3 100644 --- a/tests/fixtures/app/operations/2021_06_07_132849_run_except_production.php +++ b/tests/fixtures/app/operations/2021_06_07_132849_run_except_production.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $exceptEnvironment = 'production'; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return app()->environment() !== 'production'; + } }; diff --git a/tests/fixtures/app/operations/2021_06_07_132917_run_except_testing.php b/tests/fixtures/app/operations/2021_06_07_132917_run_except_testing.php index c7a97b68..523c97df 100644 --- a/tests/fixtures/app/operations/2021_06_07_132917_run_except_testing.php +++ b/tests/fixtures/app/operations/2021_06_07_132917_run_except_testing.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $exceptEnvironment = 'testing'; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return app()->environment() !== 'testing'; + } }; diff --git a/tests/fixtures/app/operations/2021_06_07_134045_run_except_many_environments.php b/tests/fixtures/app/operations/2021_06_07_134045_run_except_many_environments.php index 485abcb2..7dafbb95 100644 --- a/tests/fixtures/app/operations/2021_06_07_134045_run_except_many_environments.php +++ b/tests/fixtures/app/operations/2021_06_07_134045_run_except_many_environments.php @@ -6,10 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected array|string|null $environment = ['testing']; - - protected array|string|null $exceptEnvironment = ['testing', 'production']; - public function up(): void { $this->table()->insert([ @@ -28,4 +24,10 @@ protected function table(): Builder { return DB::table('environment'); } + + public function shouldRun(): bool + { + return app()->environment() === 'testing' + && ! in_array(app()->environment(), ['testing', 'production'], true); + } }; diff --git a/tests/fixtures/app/operations/2021_10_26_143304_run_disallow.php b/tests/fixtures/app/operations/2021_10_26_143304_run_disallow.php index 2d8d1805..959d9dd3 100644 --- a/tests/fixtures/app/operations/2021_10_26_143304_run_disallow.php +++ b/tests/fixtures/app/operations/2021_10_26_143304_run_disallow.php @@ -20,13 +20,13 @@ public function down(): void ]); } - public function allow(): bool + protected function table(): Builder { - return false; + return DB::table('environment'); } - protected function table(): Builder + public function shouldRun(): bool { - return DB::table('environment'); + return false; } }; diff --git a/tests/fixtures/app/operations/2022_08_17_135147_test_before_enabled.php b/tests/fixtures/app/operations/2022_08_17_135147_test_before_enabled.php index 04be3ae5..0bd8af92 100644 --- a/tests/fixtures/app/operations/2022_08_17_135147_test_before_enabled.php +++ b/tests/fixtures/app/operations/2022_08_17_135147_test_before_enabled.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected bool $before = true; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('before'); } + + public function needBefore(): bool + { + return true; + } }; diff --git a/tests/fixtures/app/operations/2022_08_17_135153_test_before_disabled.php b/tests/fixtures/app/operations/2022_08_17_135153_test_before_disabled.php index ea447c8b..5f58c865 100644 --- a/tests/fixtures/app/operations/2022_08_17_135153_test_before_disabled.php +++ b/tests/fixtures/app/operations/2022_08_17_135153_test_before_disabled.php @@ -6,8 +6,6 @@ use Ramsey\Uuid\Uuid; return new class extends Operation { - protected bool $before = false; - public function up(): void { $this->table()->insert([ @@ -26,4 +24,9 @@ protected function table(): Builder { return DB::table('before'); } + + public function needBefore(): bool + { + return false; + } }; diff --git a/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub b/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub index 94a57cc2..dc93b6d9 100644 --- a/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub +++ b/tests/fixtures/app/stubs/2021_02_15_124237_test_success_transactions.stub @@ -14,11 +14,6 @@ return new class extends Operation { ]); } - public function enabledTransactions(): bool - { - return true; - } - protected function table() { return DB::table('transactions'); @@ -28,4 +23,9 @@ return new class extends Operation { { return ['value' => Uuid::uuid4()]; } + + public function withinTransactions(): bool + { + return true; + } }; diff --git a/tests/fixtures/app/stubs/2021_02_15_124852_test_failed_transactions.stub b/tests/fixtures/app/stubs/2021_02_15_124852_test_failed_transactions.stub index e805d0f8..c7bf0a88 100644 --- a/tests/fixtures/app/stubs/2021_02_15_124852_test_failed_transactions.stub +++ b/tests/fixtures/app/stubs/2021_02_15_124852_test_failed_transactions.stub @@ -16,11 +16,6 @@ return new class extends Operation { throw new Exception('Random message'); } - public function enabledTransactions(): bool - { - return true; - } - protected function table() { return DB::table('transactions'); @@ -30,4 +25,9 @@ return new class extends Operation { { return ['value' => Uuid::uuid4()]; } + + public function withinTransactions(): bool + { + return true; + } };