Multi-db support

This commit is contained in:
Hillel Coren 2017-05-01 12:29:45 +03:00
parent aaba8e4ab1
commit b3f53b99fa
9 changed files with 85 additions and 3 deletions

View File

@ -4,6 +4,7 @@ namespace App\Jobs;
use App\Jobs\Job;
use App\Models\Document;
use App\Models\LookupAccount;
use Auth;
use DB;
use Exception;
@ -57,5 +58,16 @@ class PurgeAccountData extends Job
$account->quote_number_counter = 1;
$account->client_number_counter = 1;
$account->save();
if (env('MULTI_DB_ENABLED')) {
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
$lookupAccount = LookupAccount::whereAccountKey($account->account_key)->first();
DB::table('lookup_contacts')->where('lookup_account_id', '=', $lookupAccount->id)->delete();
DB::table('lookup_invitations')->where('lookup_account_id', '=', $lookupAccount->id)->delete();
config(['database.default' => $current]);
}
}
}

View File

@ -1661,7 +1661,6 @@ Account::creating(function ($account)
LookupAccount::createAccount($account->account_key, $account->company_id);
});
Account::updated(function ($account) {
// prevent firing event if the invoice/quote counter was changed
// TODO: remove once counters are moved to separate table
@ -1672,3 +1671,10 @@ Account::updated(function ($account) {
Event::fire(new UserSettingsChanged());
});
Account::deleted(function ($account)
{
LookupAccount::deleteWhere([
'account_key' => $account->account_key
]);
});

View File

@ -47,3 +47,10 @@ AccountToken::creating(function ($token)
'token' => $token->token,
]);
});
AccountToken::deleted(function ($token)
{
LookupAccountToken::deleteWhere([
'token' => $token->token
]);
});

View File

@ -169,3 +169,17 @@ class Company extends Eloquent
return false;
}
}
Company::deleted(function ($company)
{
if (! env('MULTI_DB_ENABLED')) {
return;
}
$server = \App\Models\DbServer::whereName(config('database.default'))->firstOrFail();
LookupCompany::deleteWhere([
'company_id' => $company->id,
'db_server_id' => $server->id,
]);
});

View File

@ -173,3 +173,10 @@ Contact::creating(function ($contact)
'contact_key' => $contact->contact_key,
]);
});
Contact::deleted(function ($contact)
{
LookupContact::deleteWhere([
'contact_key' => $contact->contact_key,
]);
});

View File

@ -170,3 +170,10 @@ Invitation::creating(function ($invitation)
'invitation_key' => $invitation->invitation_key,
]);
});
Invitation::deleted(function ($invitation)
{
LookupInvitation::deleteWhere([
'invitation_key' => $invitation->invitation_key,
]);
});

View File

@ -41,6 +41,21 @@ class LookupModel extends Eloquent
config(['database.default' => $current]);
}
public static function deleteWhere($where)
{
if (! env('MULTI_DB_ENABLED')) {
return;
}
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
static::where($where)->delete();
config(['database.default' => $current]);
}
public static function setServerByField($field, $value)
{
if (! env('MULTI_DB_ENABLED')) {
@ -60,7 +75,7 @@ class LookupModel extends Eloquent
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
if ($lookupUser = static::where($field, '=', $value)->first()) {
if ($value && $lookupUser = static::where($field, '=', $value)->first()) {
$entity = new $className();
$server = $lookupUser->getDbServer();
static::setDbServer($server);

View File

@ -433,3 +433,14 @@ User::updating(function ($user) {
User::updated(function ($user) {
User::onUpdatedUser($user);
});
User::deleted(function ($user)
{
if (! $user->email) {
return;
}
LookupUser::deleteWhere([
'email' => $user->email
]);
});

View File

@ -47,7 +47,10 @@ class AddMultipleDatabaseSupport extends Migration
});
Schema::rename('lookup_tokens', 'lookup_account_tokens');
}
DB::table('db_servers')->insert(
['name' => 'db-server-1']
);
/**
* Reverse the migrations.