Skip to content

Commit 29291cf

Browse files
committed
- Added Database::dryRun(callable) for dry run simulations. This could be especially useful in unittesting-environments. InnoDB only (mysql).
1 parent e292fef commit 29291cf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Database.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,11 @@ public function transactionRollback();
101101
* @throws \Exception
102102
*/
103103
public function transaction($tries = 1, $callback = null);
104+
105+
/**
106+
* @param callable|null $callback
107+
* @return mixed
108+
* @throws \Exception
109+
*/
110+
public function dryRun($callback = null);
104111
}

src/Databases/MySQL.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,28 @@ public function transactionRollback() {
256256
});
257257
}
258258

259+
/**
260+
* @param callable|null $callback
261+
* @return mixed
262+
* @throws \Exception
263+
* @throws null
264+
*/
265+
public function dryRun($callback = null) {
266+
$result = null;
267+
$exception = null;
268+
$this->transactionStart();
269+
try {
270+
$result = call_user_func($callback, $this);
271+
} catch (\Exception $e) {
272+
$exception = $e;
273+
}
274+
$this->transactionRollback();
275+
if($exception !== null) {
276+
throw $exception;
277+
}
278+
return $result;
279+
}
280+
259281
/**
260282
* @param int|callable $tries
261283
* @param callable|null $callback

0 commit comments

Comments
 (0)