Multi-db support

This commit is contained in:
Hillel Coren 2017-05-03 19:26:03 +03:00
parent 517804f987
commit 782ae969a4
4 changed files with 48 additions and 19 deletions

View File

@ -105,6 +105,29 @@ class CheckData extends Command
private function checkContacts() 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') $clients = DB::table('clients')
->leftJoin('contacts', function($join) { ->leftJoin('contacts', function($join) {
$join->on('contacts.client_id', '=', 'clients.id') $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') $clients = DB::table('clients')
->leftJoin('contacts', function($join) { ->leftJoin('contacts', function($join) {
$join->on('contacts.client_id', '=', 'clients.id') $join->on('contacts.client_id', '=', 'clients.id')

View File

@ -144,7 +144,7 @@ class InitLookup extends Command
} else { } else {
LookupUser::create([ LookupUser::create([
'lookup_account_id' => $lookupAccount->id, 'lookup_account_id' => $lookupAccount->id,
'email' => $user['email'], 'email' => $user['email'] ?: null,
'user_id' => $user['user_id'], 'user_id' => $user['user_id'],
]); ]);
} }

View File

@ -31,28 +31,32 @@ class PruneData extends Command
// delete accounts who never registered, didn't create any invoices, // 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 // hansn't logged in within the past 6 months and isn't linked to another account
$sql = 'select a.id $sql = 'select c.id
from (select id, last_login from accounts) a from companies c
left join users u on u.account_id = a.id and u.public_id = 0 left join accounts a on a.company_id = c.id
left join invoices i on i.account_id = a.id left join clients c on c.account_id = a.id
left join user_accounts ua1 on ua1.user_id1 = u.id left join tasks t on t.account_id = a.id
left join user_accounts ua2 on ua2.user_id2 = u.id left join expenses e on e.account_id = a.id
left join user_accounts ua3 on ua3.user_id3 = u.id left join users u on u.account_id = a.id and u.registered = 1
left join user_accounts ua4 on ua4.user_id4 = u.id where c.created_at < DATE_SUB(now(), INTERVAL 6 MONTH)
left join user_accounts ua5 on ua5.user_id5 = u.id group by c.id
where u.registered = 0 having count(c.id) = 0
and a.last_login < DATE_SUB(now(), INTERVAL 6 MONTH) and count(t.id) = 0
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) and count(e.id) = 0
group by a.id and count(u.id) = 0';
having count(i.id) = 0';
$results = DB::select($sql); $results = DB::select($sql);
foreach ($results as $result) { foreach ($results as $result) {
$this->info("Deleting {$result->id}"); $this->info("Deleting company: {$result->id}");
DB::table('accounts') try {
DB::table('companies')
->where('id', '=', $result->id) ->where('id', '=', $result->id)
->delete(); ->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'); $this->info('Done');

View File

@ -415,6 +415,7 @@ class AccountRepository
$contact->user_id = $ninjaUser->id; $contact->user_id = $ninjaUser->id;
$contact->account_id = $ninjaAccount->id; $contact->account_id = $ninjaAccount->id;
$contact->public_id = $account->id; $contact->public_id = $account->id;
$contact->contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
$contact->is_primary = true; $contact->is_primary = true;
} }