Skip to content

Commit 4422454

Browse files
author
Andrey Helldar
committed
Added the ability to change the number of attempts to write data to the database in transactions
1 parent fefe419 commit 4422454

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class AddSomeData extends Actionable
147147
{
148148
protected $transactions = true;
149149

150+
protected $transaction_attempts = 3;
151+
150152
public function up(): void
151153
{
152154
// ...

src/Support/Actionable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ abstract class Actionable extends Migration implements Contract
2626
*/
2727
protected $transactions = false;
2828

29+
/**
30+
* The number of attempts to execute a request within a transaction before throwing an error.
31+
*
32+
* @var int
33+
*/
34+
protected $transaction_attempts = 1;
35+
2936
/**
3037
* Reverse the actions.
3138
*/
@@ -55,4 +62,14 @@ public function enabledTransactions(): bool
5562
{
5663
return $this->transactions;
5764
}
65+
66+
/**
67+
* The number of attempts to execute a request within a transaction before throwing an error.
68+
*
69+
* @return int
70+
*/
71+
public function transactionAttempts(): int
72+
{
73+
return $this->transaction_attempts;
74+
}
5875
}

src/Support/Migrator.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function runMigration($migration, $method)
7070
if ($this->enabledTransactions($migration)) {
7171
DB::transaction(function () use ($migration, $method) {
7272
parent::runMigration($migration, $method);
73-
});
73+
}, $this->transactionAttempts($migration));
7474

7575
return;
7676
}
@@ -93,12 +93,26 @@ protected function allowLogging($migration): bool
9393
/**
9494
* Whether it is necessary to call database transactions at runtime.
9595
*
96-
* @param object $migration
96+
* @param \Helldar\LaravelActions\Support\Actionable|object $migration
9797
*
9898
* @return bool
9999
*/
100100
protected function enabledTransactions($migration): bool
101101
{
102102
return $migration->enabledTransactions();
103103
}
104+
105+
/**
106+
* The number of attempts to execute a request within a transaction before throwing an error.
107+
*
108+
* @param \Helldar\LaravelActions\Support\Actionable|object $migration
109+
*
110+
* @return int
111+
*/
112+
protected function transactionAttempts($migration): int
113+
{
114+
$value = $migration->transactionAttempts();
115+
116+
return (int) abs($value);
117+
}
104118
}

0 commit comments

Comments
 (0)