Skip to content

Commit 330cb64

Browse files
committed
feat: add resetMySQLAutoIncrement method to reset auto-increment values for specified tables
1 parent ed6d52d commit 330cb64

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Traits/FixturesTrait.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ public function prepareSequences($tables, $except = [])
195195
app('db.connection')->unprepared($query);
196196
}
197197

198+
public function resetMySQLAutoIncrement($tables, $except = [])
199+
{
200+
$query = array_concat($tables, function ($table) use ($except) {
201+
if (in_array($table, $except)) {
202+
return '';
203+
} else {
204+
return "ALTER TABLE {$table} AUTO_INCREMENT = 1;\n";
205+
}
206+
});
207+
208+
app('db.connection')->unprepared($query);
209+
}
210+
198211
public function exportFile($response, $fixture)
199212
{
200213
$this->exportContent(
@@ -218,7 +231,7 @@ protected function exportContent($content, $fixture)
218231
{
219232
if (env('FAIL_EXPORT_JSON', true)) {
220233
$this->fail(preg_replace('/[ ]+/mu', ' ',
221-
'Looks like you forget to remove exportJson. If it is your local environment add
234+
'Looks like you forget to remove exportJson. If it is your local environment add
222235
FAIL_EXPORT_JSON=false to .env.testing.
223236
If it is dev.testing environment then remove it.'
224237
));

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function setUp(): void
4646

4747
if (config('database.default') === 'pgsql') {
4848
$this->prepareSequences($this->getTables());
49+
} elseif (config('database.default') === 'mysql') {
50+
$this->resetMySQLAutoIncrement($this->getTables());
4951
}
5052

5153
$this->auth = app(JWTAuth::class);

0 commit comments

Comments
 (0)