Multi-db support

This commit is contained in:
Hillel Coren 2017-04-30 21:44:52 +03:00
parent d6b768594d
commit 14072b6334
5 changed files with 158 additions and 25 deletions

View File

@ -19,7 +19,7 @@ class InitLookup extends Command
*
* @var string
*/
protected $signature = 'ninja:init-lookup';
protected $signature = 'ninja:init-lookup {--truncate=} {--company_id=}';
/**
* The console command description.
@ -45,50 +45,104 @@ class InitLookup extends Command
*/
public function handle()
{
$this->info(date('Y-m-d') . ' Running InitLookup...');
$this->info(date('Y-m-d h:i:s') . ' Running InitLookup...');
config(['database.default' => DB_NINJA_0]);
config(['database.default' => DB_NINJA_LOOKUP]);
if (DbServer::count()) {
//exit('db_server record exists!');
}
$dbServer = DbServer::first();
} else {
$dbServer = DbServer::create(['name' => DB_NINJA_1]);
$count = DB::table('companies')->count();
for ($i=0; $i<$count; $i += 100) {
$this->initCompanies($offset);
}
}
private function initCompanies($offset = 0)
{
$this->info(date('Y-m-d') . ' initCompanies - offset: ' . $offset);
if ($this->option('truncate')) {
$this->truncateTables();
}
config(['database.default' => DB_NINJA_1]);
$companies = DB::table('companies')->orderBy('id')->get(['id']);
$count = DB::table('companies')
->where('id', '>=', $this->option('company_id') ?: 1)
->count();
for ($i=0; $i<$count; $i += 100) {
$this->initCompanies($dbServer->id, $i);
}
}
private function initCompanies($dbServerId, $offset = 0)
{
$this->info(date('Y-m-d h:i:s') . ' initCompanies - offset: ' . $offset);
$data = [];
config(['database.default' => DB_NINJA_1]);
$companies = DB::table('companies')
->offset($offset)
->limit(100)
->orderBy('id')
->where('id', '>=', $this->option('company_id') ?: 1)
->get(['id']);
foreach ($companies as $company) {
$this->parseCompany($dbServer->id, $company->id);
$data[$company->id] = $this->parseCompany($company->id);
}
config(['database.default' => DB_NINJA_LOOKUP]);
foreach ($data as $companyId => $company) {
$this->info(date('Y-m-d h:i:s') . ' company: ' . $companyId);
$lookupCompany = LookupCompany::create([
'db_server_id' => $dbServerId,
'company_id' => $companyId,
]);
foreach ($company as $accountKey => $account) {
$lookupAccount = LookupAccount::create([
'lookup_company_id' => $lookupCompany->id,
'account_key' => $accountKey
]);
foreach ($account['users'] as $user) {
LookupUser::create([
'lookup_account_id' => $lookupAccount->id,
'email' => $user['email'],
]);
}
foreach ($account['contacts'] as $contact) {
LookupContact::create([
'lookup_account_id' => $lookupAccount->id,
'contact_key' => $contact['contact_key'],
]);
}
foreach ($account['invitations'] as $invitation) {
LookupInvitation::create([
'lookup_account_id' => $lookupAccount->id,
'invitation_key' => $invitation['invitation_key'],
'message_id' => $invitation['message_id'] ?: null,
]);
}
foreach ($account['tokens'] as $token) {
LookupToken::create([
'lookup_account_id' => $lookupAccount->id,
'token' => $token['token'],
]);
}
}
}
}
private function parseCompany($dbServerId, $companyId)
private function parseCompany($companyId)
{
$data = [];
config(['database.default' => DB_NINJA_1]);
$accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get(['id']);
$accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get(['id', 'account_key']);
foreach ($accounts as $account) {
$data[$account->id] = $this->parseAccount($account->id);
$data[$account->account_key] = $this->parseAccount($account->id);
}
print_r($data);exit;
config(['database.default' => DB_NINJA_0]);
///$lookupCompany = LookupCompany::create(['db_server_id' => $dbServerId]);
return $data;
}
private function parseAccount($accountId)
@ -122,4 +176,25 @@ class InitLookup extends Command
return $data;
}
private function truncateTables()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
DB::statement('truncate lookup_companies');
DB::statement('truncate lookup_accounts');
DB::statement('truncate lookup_users');
DB::statement('truncate lookup_contacts');
DB::statement('truncate lookup_invitations');
DB::statement('truncate lookup_tokens');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
protected function getOptions()
{
return [
['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null],
['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null],
];
}
}

View File

@ -335,7 +335,7 @@ if (! defined('APP_NAME')) {
define('BLANK_IMAGE', 'data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=');
define('DB_NINJA_0', 'db-ninja-0');
define('DB_NINJA_LOOKUP', 'db-ninja-0');
define('DB_NINJA_1', 'db-ninja-1');
define('DB_NINJA_2', 'db-ninja-2');

View File

@ -19,6 +19,7 @@ class LookupCompany extends Eloquent
*/
protected $fillable = [
'db_server_id',
'company_id',
];
}

View File

@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddMultipleDatabaseSupport extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('lookup_companies', function ($table) {
$table->unsignedInteger('company_id')->index();
});
Schema::table('lookup_companies', function ($table) {
$table->unique(['db_server_id', 'company_id']);
});
Schema::table('lookup_accounts', function ($table) {
$table->string('account_key')->change()->unique();
});
Schema::table('lookup_users', function ($table) {
$table->string('email')->change()->unique();
});
Schema::table('lookup_contacts', function ($table) {
$table->string('contact_key')->change()->unique();
});
Schema::table('lookup_invitations', function ($table) {
$table->string('invitation_key')->change()->unique();
$table->string('message_id')->change()->nullable()->unique();
});
Schema::table('lookup_tokens', function ($table) {
$table->string('token')->change()->unique();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('lookup_companies', function ($table) {
$table->dropColumn('company_id');
});
}
}

View File

@ -85,6 +85,7 @@ class UserTableSeeder extends Seeder
'email' => env('TEST_EMAIL', TEST_USERNAME),
'is_primary' => true,
'send_invoice' => true,
'contact_key' => strtolower(str_random(RANDOM_KEY_LENGTH)),
]);
Product::create([