mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Multi-db support
This commit is contained in:
parent
aaba8e4ab1
commit
b3f53b99fa
@ -4,6 +4,7 @@ namespace App\Jobs;
|
|||||||
|
|
||||||
use App\Jobs\Job;
|
use App\Jobs\Job;
|
||||||
use App\Models\Document;
|
use App\Models\Document;
|
||||||
|
use App\Models\LookupAccount;
|
||||||
use Auth;
|
use Auth;
|
||||||
use DB;
|
use DB;
|
||||||
use Exception;
|
use Exception;
|
||||||
@ -57,5 +58,16 @@ class PurgeAccountData extends Job
|
|||||||
$account->quote_number_counter = 1;
|
$account->quote_number_counter = 1;
|
||||||
$account->client_number_counter = 1;
|
$account->client_number_counter = 1;
|
||||||
$account->save();
|
$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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1661,7 +1661,6 @@ Account::creating(function ($account)
|
|||||||
LookupAccount::createAccount($account->account_key, $account->company_id);
|
LookupAccount::createAccount($account->account_key, $account->company_id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
Account::updated(function ($account) {
|
Account::updated(function ($account) {
|
||||||
// prevent firing event if the invoice/quote counter was changed
|
// prevent firing event if the invoice/quote counter was changed
|
||||||
// TODO: remove once counters are moved to separate table
|
// TODO: remove once counters are moved to separate table
|
||||||
@ -1672,3 +1671,10 @@ Account::updated(function ($account) {
|
|||||||
|
|
||||||
Event::fire(new UserSettingsChanged());
|
Event::fire(new UserSettingsChanged());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Account::deleted(function ($account)
|
||||||
|
{
|
||||||
|
LookupAccount::deleteWhere([
|
||||||
|
'account_key' => $account->account_key
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -47,3 +47,10 @@ AccountToken::creating(function ($token)
|
|||||||
'token' => $token->token,
|
'token' => $token->token,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AccountToken::deleted(function ($token)
|
||||||
|
{
|
||||||
|
LookupAccountToken::deleteWhere([
|
||||||
|
'token' => $token->token
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -169,3 +169,17 @@ class Company extends Eloquent
|
|||||||
return false;
|
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,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -173,3 +173,10 @@ Contact::creating(function ($contact)
|
|||||||
'contact_key' => $contact->contact_key,
|
'contact_key' => $contact->contact_key,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Contact::deleted(function ($contact)
|
||||||
|
{
|
||||||
|
LookupContact::deleteWhere([
|
||||||
|
'contact_key' => $contact->contact_key,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -170,3 +170,10 @@ Invitation::creating(function ($invitation)
|
|||||||
'invitation_key' => $invitation->invitation_key,
|
'invitation_key' => $invitation->invitation_key,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Invitation::deleted(function ($invitation)
|
||||||
|
{
|
||||||
|
LookupInvitation::deleteWhere([
|
||||||
|
'invitation_key' => $invitation->invitation_key,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -41,6 +41,21 @@ class LookupModel extends Eloquent
|
|||||||
config(['database.default' => $current]);
|
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)
|
public static function setServerByField($field, $value)
|
||||||
{
|
{
|
||||||
if (! env('MULTI_DB_ENABLED')) {
|
if (! env('MULTI_DB_ENABLED')) {
|
||||||
@ -60,7 +75,7 @@ class LookupModel extends Eloquent
|
|||||||
$current = config('database.default');
|
$current = config('database.default');
|
||||||
config(['database.default' => DB_NINJA_LOOKUP]);
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
||||||
|
|
||||||
if ($lookupUser = static::where($field, '=', $value)->first()) {
|
if ($value && $lookupUser = static::where($field, '=', $value)->first()) {
|
||||||
$entity = new $className();
|
$entity = new $className();
|
||||||
$server = $lookupUser->getDbServer();
|
$server = $lookupUser->getDbServer();
|
||||||
static::setDbServer($server);
|
static::setDbServer($server);
|
||||||
|
@ -433,3 +433,14 @@ User::updating(function ($user) {
|
|||||||
User::updated(function ($user) {
|
User::updated(function ($user) {
|
||||||
User::onUpdatedUser($user);
|
User::onUpdatedUser($user);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
User::deleted(function ($user)
|
||||||
|
{
|
||||||
|
if (! $user->email) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LookupUser::deleteWhere([
|
||||||
|
'email' => $user->email
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
@ -47,7 +47,10 @@ class AddMultipleDatabaseSupport extends Migration
|
|||||||
});
|
});
|
||||||
|
|
||||||
Schema::rename('lookup_tokens', 'lookup_account_tokens');
|
Schema::rename('lookup_tokens', 'lookup_account_tokens');
|
||||||
}
|
|
||||||
|
DB::table('db_servers')->insert(
|
||||||
|
['name' => 'db-server-1']
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse the migrations.
|
* Reverse the migrations.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user