Skip to content

Commit

Permalink
Changed description and added argument to make disabling foreign keys…
Browse files Browse the repository at this point in the history
… optional, adjusted other classes which are using.
  • Loading branch information
jamielsharief committed Jun 13, 2020
1 parent 4817e54 commit 528129f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
20 changes: 14 additions & 6 deletions src/Model/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public function __construct(array $config = [])
}

/**
* Disables foreign keys then runs a callback as a transaction, if an exception is
* thrown or or the callback returns false then the transaction will be rolledback.
* Creates and handles a DB transaction with the option to disable foreign key constraints.
*
* @example
*
Expand All @@ -135,22 +134,31 @@ public function __construct(array $config = [])
* });
*
* @param callable $callback
* @param boolean $disbleForeignKeyConstraints
* @return mixed
*/
public function transaction(callable $callback)
public function transaction(callable $callback, bool $disbleForeignKeyConstraints = false)
{
$this->begin();
$this->disableForeignKeyConstraints();

if ($disbleForeignKeyConstraints) {
$this->disableForeignKeyConstraints();
}

try {
$result = $callback($this);
} catch (Exception $exception) {
$this->enableForeignKeyConstraints();
if ($disbleForeignKeyConstraints) {
$this->enableForeignKeyConstraints();
}

$this->rollback();
throw $exception;
}

$this->enableForeignKeyConstraints();
if ($disbleForeignKeyConstraints) {
$this->enableForeignKeyConstraints();
}

if ($result === false) {
$this->rollback();
Expand Down
19 changes: 13 additions & 6 deletions src/Model/Engine/SqliteEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public function databases(): array
}

/**
* Disables foreign keys then runs a callback as a transaction, if an exception is
* thrown or or the callback returns false then the transaction will be rolledback.
* Creates and handles a DB transaction with the option to disable foreign key constraints.
*
* @example
*
Expand All @@ -86,18 +85,24 @@ public function databases(): array
* });
*
* @param callable $callback
* @param boolean $disbleForeignKeyConstraints
* @return mixed
*/
public function transaction(callable $callback)
public function transaction(callable $callback, bool $disbleForeignKeyConstraints = false)
{
$this->disableForeignKeyConstraints();
if ($disbleForeignKeyConstraints) {
$this->disableForeignKeyConstraints();
}

$this->begin();

try {
$result = $callback($this);
} catch (Exception $exception) {
$this->rollback();
$this->enableForeignKeyConstraints();
if ($disbleForeignKeyConstraints) {
$this->enableForeignKeyConstraints();
}

throw $exception;
}
Expand All @@ -108,7 +113,9 @@ public function transaction(callable $callback)
$this->commit();
}

$this->enableForeignKeyConstraints();
if ($disbleForeignKeyConstraints) {
$this->enableForeignKeyConstraints();
}

return $result;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Model/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ protected function executeStatements(array $statements)
foreach ($statements as $statement) {
$this->assertTrue($connection->execute($statement));
}
});
}, true);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Model/SeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ protected function executeStatements(array $statements)
$this->assertTrue($connection->execute($sql));
}
}
});
}, true);
}
}

0 comments on commit 528129f

Please sign in to comment.