Merge pull request #1801 from burakcakirel/migration-patch

Fix migration issue
This commit is contained in:
Cüneyt Şentürk 2021-01-26 13:30:19 +03:00 committed by GitHub
commit 4fc30e354d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -100,8 +100,8 @@ class Version210 extends Listener
$this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note'])); $this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note']));
// Sort table's count by ascending to improve performance. // Sort table's count by ascending to improve performance.
foreach ($this->totals->sort() as $table => $count) { foreach ($this->totals->sortBy('count') as $total) {
$method = 'copy' . Str::plural(Str::studly($table)); $method = 'copy' . Str::plural(Str::studly($total->type));
$this->$method(); $this->$method();
} }
@ -125,10 +125,8 @@ class Version210 extends Listener
private function updateInvoiceIds(): void private function updateInvoiceIds(): void
{ {
$sorted = $this->totals->sortDesc()->keys();
// Invoice ids do not changed // Invoice ids do not changed
if ('invoice' === $sorted->first()) { if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -153,10 +151,8 @@ class Version210 extends Listener
private function updateBillIds(): void private function updateBillIds(): void
{ {
$sorted = $this->totals->sortDesc()->keys();
// Bill ids do not changed // Bill ids do not changed
if ('bill' === $sorted->first()) { if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -181,6 +177,7 @@ class Version210 extends Listener
private function updateDocumentIds() private function updateDocumentIds()
{ {
$this->totals = $this->getTotals();
$this->updateInvoiceIds(); $this->updateInvoiceIds();
$this->updateBillIds(); $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; $incrementAmount = 0;
foreach ($this->totals->sortDesc()->keys()->takeUntil($key) as $table) { foreach ($this->totals->sortByDesc('count')->pluck('type')->takeUntil($type) as $table) {
$incrementAmount += optional( $incrementAmount += optional(
DB::table($table . $suffix)->orderByDesc('id')->first('id'), DB::table($table . $suffix)->orderByDesc('id')->first('id'),
function ($document) { 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 = []; $counts = [];
foreach ($tables as $table) { foreach ($types as $type) {
if (!Schema::hasTable(Str::plural($table))) { if (!Schema::hasTable(Str::plural($type))) {
continue; continue;
} }
$counts[$table] = DB::table(Str::plural($table))->count(); $count = DB::table(Str::plural($type))->count();
if ($count === 0) {
continue;
} }
return $counts; $values = new \stdClass();
$values->type = $type;
$values->count = $count;
$counts[] = $values;
}
return collect($counts);
} }
private function batchCopyRelations(string $table, string $type): void private function batchCopyRelations(string $table, string $type): void