From 21a465cdfd13d865334af732f82b2b540190df76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 26 Jan 2021 13:02:16 +0300 Subject: [PATCH] Fix migration issue --- app/Listeners/Update/V21/Version210.php | 46 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/app/Listeners/Update/V21/Version210.php b/app/Listeners/Update/V21/Version210.php index f58a83ad5..ef4f74602 100644 --- a/app/Listeners/Update/V21/Version210.php +++ b/app/Listeners/Update/V21/Version210.php @@ -100,8 +100,8 @@ class Version210 extends Listener $this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note'])); // Sort table's count by ascending to improve performance. - foreach ($this->totals->sort() as $table => $count) { - $method = 'copy' . Str::plural(Str::studly($table)); + foreach ($this->totals->sortBy('count') as $total) { + $method = 'copy' . Str::plural(Str::studly($total->type)); $this->$method(); } @@ -125,10 +125,8 @@ class Version210 extends Listener private function updateInvoiceIds(): void { - $sorted = $this->totals->sortDesc()->keys(); - // Invoice ids do not changed - if ('invoice' === $sorted->first()) { + if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) { return; } @@ -153,10 +151,8 @@ class Version210 extends Listener private function updateBillIds(): void { - $sorted = $this->totals->sortDesc()->keys(); - // Bill ids do not changed - if ('bill' === $sorted->first()) { + if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) { return; } @@ -181,6 +177,7 @@ class Version210 extends Listener private function updateDocumentIds() { + $this->totals = $this->getTotals(); $this->updateInvoiceIds(); $this->updateBillIds(); @@ -420,11 +417,11 @@ class Version210 extends Listener } } - private function getIncrementAmount(string $key, string $suffix): int + private function getIncrementAmount(string $type, string $suffix): int { $incrementAmount = 0; - foreach ($this->totals->sortDesc()->keys()->takeUntil($key) as $table) { + foreach ($this->totals->sortByDesc('count')->pluck('type')->takeUntil($type) as $table) { $incrementAmount += optional( DB::table($table . $suffix)->orderByDesc('id')->first('id'), function ($document) { @@ -571,18 +568,37 @@ class Version210 extends Listener } } - private function getTotals(array $tables): array + private function getTotals(array $types = []): Collection { + if (DB::table('documents')->count() > 0) { + $counts = DB::table('documents') + ->select('type', DB::raw('COUNT(id) count')) + ->groupBy('type') + ->orderBy('id') + ->get(); + + return $counts; + } + $counts = []; - foreach ($tables as $table) { - if (!Schema::hasTable(Str::plural($table))) { + foreach ($types as $type) { + if (!Schema::hasTable(Str::plural($type))) { continue; } - $counts[$table] = DB::table(Str::plural($table))->count(); + $count = DB::table(Str::plural($type))->count(); + + if ($count === 0) { + continue; + } + + $values = new \stdClass(); + $values->type = $type; + $values->count = $count; + $counts[] = $values; } - return $counts; + return collect($counts); } private function batchCopyRelations(string $table, string $type): void