From 782ae969a40bce0c702bc1f7cc1e832ec038cac9 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 3 May 2017 19:26:03 +0300 Subject: [PATCH] Multi-db support --- app/Console/Commands/CheckData.php | 24 ++++++++++++ app/Console/Commands/InitLookup.php | 2 +- app/Console/Commands/PruneData.php | 40 +++++++++++--------- app/Ninja/Repositories/AccountRepository.php | 1 + 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index d769d2ed5b..636b9680c1 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -105,6 +105,29 @@ class CheckData extends Command private function checkContacts() { + // check for contacts with the contact_key value set + $contacts = DB::table('contacts') + ->whereNull('contact_key') + ->orderBy('id') + ->get(['id']); + $this->logMessage(count($contacts) . ' contacts without a contact_key'); + + if (count($contacts) > 0) { + $this->isValid = false; + } + + if ($this->option('fix') == 'true') { + foreach ($contacts as $contact) { + DB::table('contacts') + ->where('id', $contact->id) + ->whereNull('contact_key') + ->update([ + 'contact_key' => strtolower(str_random(RANDOM_KEY_LENGTH)), + ]); + } + } + + // check for missing contacts $clients = DB::table('clients') ->leftJoin('contacts', function($join) { $join->on('contacts.client_id', '=', 'clients.id') @@ -138,6 +161,7 @@ class CheckData extends Command } } + // check for more than one primary contact $clients = DB::table('clients') ->leftJoin('contacts', function($join) { $join->on('contacts.client_id', '=', 'clients.id') diff --git a/app/Console/Commands/InitLookup.php b/app/Console/Commands/InitLookup.php index acfc9c6b0e..2dfa0facd7 100644 --- a/app/Console/Commands/InitLookup.php +++ b/app/Console/Commands/InitLookup.php @@ -144,7 +144,7 @@ class InitLookup extends Command } else { LookupUser::create([ 'lookup_account_id' => $lookupAccount->id, - 'email' => $user['email'], + 'email' => $user['email'] ?: null, 'user_id' => $user['user_id'], ]); } diff --git a/app/Console/Commands/PruneData.php b/app/Console/Commands/PruneData.php index e42b21767f..d0b889f5ab 100644 --- a/app/Console/Commands/PruneData.php +++ b/app/Console/Commands/PruneData.php @@ -31,28 +31,32 @@ class PruneData extends Command // delete accounts who never registered, didn't create any invoices, // hansn't logged in within the past 6 months and isn't linked to another account - $sql = 'select a.id - from (select id, last_login from accounts) a - left join users u on u.account_id = a.id and u.public_id = 0 - left join invoices i on i.account_id = a.id - left join user_accounts ua1 on ua1.user_id1 = u.id - left join user_accounts ua2 on ua2.user_id2 = u.id - left join user_accounts ua3 on ua3.user_id3 = u.id - left join user_accounts ua4 on ua4.user_id4 = u.id - left join user_accounts ua5 on ua5.user_id5 = u.id - where u.registered = 0 - and a.last_login < DATE_SUB(now(), INTERVAL 6 MONTH) - and (ua1.id is null and ua2.id is null and ua3.id is null and ua4.id is null and ua5.id is null) - group by a.id - having count(i.id) = 0'; + $sql = 'select c.id + from companies c + left join accounts a on a.company_id = c.id + left join clients c on c.account_id = a.id + left join tasks t on t.account_id = a.id + left join expenses e on e.account_id = a.id + left join users u on u.account_id = a.id and u.registered = 1 + where c.created_at < DATE_SUB(now(), INTERVAL 6 MONTH) + group by c.id + having count(c.id) = 0 + and count(t.id) = 0 + and count(e.id) = 0 + and count(u.id) = 0'; $results = DB::select($sql); foreach ($results as $result) { - $this->info("Deleting {$result->id}"); - DB::table('accounts') - ->where('id', '=', $result->id) - ->delete(); + $this->info("Deleting company: {$result->id}"); + try { + DB::table('companies') + ->where('id', '=', $result->id) + ->delete(); + } catch (\Illuminate\Database\QueryException $e) { + // most likely because a user_account record exists which doesn't cascade delete + $this->info("Unable to delete companyId: {$result->id}"); + } } $this->info('Done'); diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 51ff426c2b..c6aa4875f6 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -415,6 +415,7 @@ class AccountRepository $contact->user_id = $ninjaUser->id; $contact->account_id = $ninjaAccount->id; $contact->public_id = $account->id; + $contact->contact_key = strtolower(str_random(RANDOM_KEY_LENGTH)); $contact->is_primary = true; }