Skip to content

Commit d51a25b

Browse files
author
Emmanuel Obua
committed
added checks for setting identity_insert
1 parent 69604d9 commit d51a25b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Console/TransformAndSeedLocale.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Intanode\UgLocale\Services\LocaleService;
77
use Intanode\UgLocale\Models\{District, County, SubCounty, Parish, Village, Region};
88
use Illuminate\Support\Facades\{Artisan, DB, Schema, Log};
9+
use Illuminate\Support\Facades\Schema;
910

1011
class TransformAndSeedLocale extends Command
1112
{
@@ -112,6 +113,16 @@ private function seedTable(string $name, string $modelClass, callable $transform
112113
$this->info("📊 Total $name records: $totalRecords");
113114

114115
DB::transaction(function () use ($modelClass, $data, $batchSize, $name, $totalRecords) {
116+
117+
$driver = Schema::getConnection()->getDriverName();
118+
119+
$table = (new $modelClass)->getTable();
120+
121+
if ($driver === 'sqlsrv') {
122+
DB::statement("SET IDENTITY_INSERT {$table} ON");
123+
}
124+
125+
115126
if ($batchSize) {
116127
$chunks = array_chunk($data, $batchSize);
117128
$this->output->progressStart(count($chunks));
@@ -130,6 +141,11 @@ private function seedTable(string $name, string $modelClass, callable $transform
130141
}
131142
$this->output->progressFinish();
132143
}
144+
145+
if ($driver === 'sqlsrv') {
146+
DB::statement("SET IDENTITY_INSERT {$table} OFF");
147+
}
148+
133149
});
134150

135151
$this->info("$name have been successfully seeded!");

0 commit comments

Comments
 (0)