| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Services\Asteria;
- use Illuminate\Support\Facades\DB;
- /**
- * Empty-catalog Asteria migration writes Magento entity_id / review_id as Bagisto PK.
- */
- class MagentoPrimaryKey
- {
- /**
- * @param array<int|string> $ids
- * @return array<int, int> occupied id => id
- */
- public static function occupiedIds(string $table, array $ids): array
- {
- $ids = array_values(array_unique(array_filter(
- array_map('intval', $ids),
- fn (int $id) => $id > 0
- )));
- if ($ids === []) {
- return [];
- }
- return DB::table($table)
- ->whereIn('id', $ids)
- ->pluck('id')
- ->map(fn ($id) => (int) $id)
- ->flip()
- ->all();
- }
- public static function bumpAutoIncrement(string $table): void
- {
- $driver = DB::connection()->getDriverName();
- if (! in_array($driver, ['mysql', 'mariadb'], true)) {
- return;
- }
- // DDL implicit-commits MySQL, which would leak rows from PHPUnit DatabaseTransactions.
- if (DB::connection()->transactionLevel() > 0) {
- return;
- }
- $max = (int) DB::table($table)->max('id');
- $next = max(1, $max + 1);
- $prefix = DB::connection()->getTablePrefix();
- $safe = str_replace(['`', '.', ' '], '', $table);
- DB::statement('ALTER TABLE `'.$prefix.$safe.'` AUTO_INCREMENT = '.$next);
- }
- }
|