From c032143c4758f7860c9207b83725f8a302f74cfc Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 2 May 2017 21:55:36 +0300 Subject: [PATCH] Multi-db support --- .env.travis | 22 ++++++++++++ .travis.yml | 48 +++++++++++++++----------- app/Console/Commands/InitLookup.php | 12 +++++++ app/Http/Controllers/AppController.php | 1 + app/Models/LookupModel.php | 5 +-- database/seeds/LanguageSeeder.php | 2 +- 6 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 .env.travis diff --git a/.env.travis b/.env.travis new file mode 100644 index 000000000000..166606f0eb03 --- /dev/null +++ b/.env.travis @@ -0,0 +1,22 @@ +APP_ENV=development +APP_DEBUG=true +APP_URL=http://ninja.dev +APP_KEY=SomeRandomStringSomeRandomString +APP_CIPHER=AES-256-CBC +APP_LOCALE=en + +MULTI_DB_ENABLED=true +MULTI_DB_CACHE_ENABLED=true + +DB_TYPE=db-ninja-1 +DB_STRICT=false +DB_HOST=localhost +DB_USERNAME=ninja +DB_PASSWORD=ninja + +DB_DATABASE0=ninja0 +DB_DATABASE1=ninja1 +DB_DATABASE2=ninja2 + +MAIL_DRIVER=log +TRAVIS=true diff --git a/.travis.yml b/.travis.yml index 067ed7c1530e..89dbfeb9d81e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,21 +44,21 @@ before_script: # prevent MySQL went away error - mysql -u root -e 'SET @@GLOBAL.wait_timeout=28800;' # copy configuration files - - cp .env.example .env + - cp .env.travis .env - cp tests/_bootstrap.php.default tests/_bootstrap.php - php artisan key:generate --no-interaction - - sed -i 's/APP_ENV=production/APP_ENV=development/g' .env - - sed -i 's/APP_DEBUG=false/APP_DEBUG=true/g' .env - - sed -i 's/MAIL_DRIVER=smtp/MAIL_DRIVER=log/g' .env - - sed -i 's/PHANTOMJS_CLOUD_KEY/#PHANTOMJS_CLOUD_KEY/g' .env - sed -i '$a NINJA_DEV=true' .env - - sed -i '$a TRAVIS=true' .env # create the database and user - - mysql -u root -e "create database IF NOT EXISTS ninja;" - - mysql -u root -e "GRANT ALL PRIVILEGES ON ninja.* To 'ninja'@'localhost' IDENTIFIED BY 'ninja'; FLUSH PRIVILEGES;" + - mysql -u root -e "create database IF NOT EXISTS ninja0;" + - mysql -u root -e "create database IF NOT EXISTS ninja1;" + - mysql -u root -e "create database IF NOT EXISTS ninja2;" + - mysql -u root -e "GRANT ALL PRIVILEGES ON ninja0.* To 'ninja'@'localhost' IDENTIFIED BY 'ninja'; FLUSH PRIVILEGES;" + - mysql -u root -e "GRANT ALL PRIVILEGES ON ninja1.* To 'ninja'@'localhost' IDENTIFIED BY 'ninja'; FLUSH PRIVILEGES;" + - mysql -u root -e "GRANT ALL PRIVILEGES ON ninja2.* To 'ninja'@'localhost' IDENTIFIED BY 'ninja'; FLUSH PRIVILEGES;" # migrate and seed the database - - php artisan migrate --no-interaction - - php artisan db:seed --no-interaction # default seed + - php artisan migrate --database=ninja0 --seed --no-interaction + - php artisan migrate --database=ninja1 --seed --no-interaction + - php artisan migrate --database=ninja2 --seed --no-interaction # Start webserver on ninja.dev:8000 - php artisan serve --host=ninja.dev --port=8000 & # '&' allows to run in background # Start PhantomJS @@ -69,6 +69,7 @@ before_script: - curl -L http://ninja.dev:8000/update - php artisan ninja:create-test-data 4 true - php artisan db:seed --no-interaction --class=UserTableSeeder # development seed + - sed -i 's/DB_TYPE=db-ninja-1/DB_TYPE=db-ninja-2/g' .env script: - php ./vendor/codeception/codeception/codecept run --debug acceptance APICest.php @@ -92,17 +93,22 @@ script: after_script: - php artisan ninja:check-data --no-interaction - cat .env - - mysql -u root -e 'select * from accounts;' ninja - - mysql -u root -e 'select * from users;' ninja - - mysql -u root -e 'select * from account_gateways;' ninja - - mysql -u root -e 'select * from clients;' ninja - - mysql -u root -e 'select * from contacts;' ninja - - mysql -u root -e 'select * from invoices;' ninja - - mysql -u root -e 'select * from invoice_items;' ninja - - mysql -u root -e 'select * from invitations;' ninja - - mysql -u root -e 'select * from payments;' ninja - - mysql -u root -e 'select * from credits;' ninja - - mysql -u root -e 'select * from expenses;' ninja + - mysql -u root -e 'select * from lookup_companies;' ninja0 + - mysql -u root -e 'select * from lookup_accounts;' ninja0 + - mysql -u root -e 'select * from lookup_contacts;' ninja0 + - mysql -u root -e 'select * from lookup_invitations;' ninja0 + - mysql -u root -e 'select * from accounts;' ninja1 + - mysql -u root -e 'select * from users;' ninja1 + - mysql -u root -e 'select * from account_gateways;' ninja1 + - mysql -u root -e 'select * from clients;' ninja1 + - mysql -u root -e 'select * from contacts;' ninja1 + - mysql -u root -e 'select * from invoices;' ninja1 + - mysql -u root -e 'select * from invoice_items;' ninja1 + - mysql -u root -e 'select * from invitations;' ninja1 + - mysql -u root -e 'select * from payments;' ninja1 + - mysql -u root -e 'select * from credits;' ninja1 + - mysql -u root -e 'select * from expenses;' ninja1 + - mysql -u root -e 'select * from accounts;' ninja2 - cat storage/logs/laravel-error.log - cat storage/logs/laravel-info.log - FILES=$(find tests/_output -type f -name '*.png' | sort -nr) diff --git a/app/Console/Commands/InitLookup.php b/app/Console/Commands/InitLookup.php index 699b895e40c2..64944f6d47af 100644 --- a/app/Console/Commands/InitLookup.php +++ b/app/Console/Commands/InitLookup.php @@ -4,6 +4,8 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use DB; +use Mail; +use Exception; use App\Models\DbServer; use App\Models\LookupCompany; use App\Models\LookupAccount; @@ -70,6 +72,16 @@ class InitLookup extends Command } $this->info($this->log); + + if ($this->option('validate') && $errorEmail) { + Mail::raw($this->log, function ($message) use ($errorEmail, $database) { + $message->to($errorEmail) + ->from(CONTACT_EMAIL) + ->subject("Check-Lookups [{$database}]: " . strtoupper($this->isValid ? RESULT_SUCCESS : RESULT_FAILURE)); + }); + } elseif (! $this->isValid) { + throw new Exception('Check data failed!!'); + } } private function initCompanies($dbServerId, $offset = 0) diff --git a/app/Http/Controllers/AppController.php b/app/Http/Controllers/AppController.php index e269a7290fc5..c02de66e855a 100644 --- a/app/Http/Controllers/AppController.php +++ b/app/Http/Controllers/AppController.php @@ -351,6 +351,7 @@ class AppController extends BaseController { try { Artisan::call('ninja:check-data'); + //Artisan::call('ninja:check-data'); return RESULT_SUCCESS; } catch (Exception $exception) { return RESULT_FAILURE; diff --git a/app/Models/LookupModel.php b/app/Models/LookupModel.php index 7156e2afe749..16e24805456c 100644 --- a/app/Models/LookupModel.php +++ b/app/Models/LookupModel.php @@ -3,6 +3,7 @@ namespace App\Models; use Eloquent; +use Cache; /** * Class ExpenseCategory. @@ -68,7 +69,7 @@ class LookupModel extends Eloquent $isUser = $className == 'App\Models\User'; // check if we've cached this lookup - if (env('MULTI_DB_CACHE_ENABLED') && $server = session($key)) { + if (env('MULTI_DB_CACHE_ENABLED') && $server = Cache::get($key)) { static::setDbServer($server, $isUser); return; } @@ -87,7 +88,7 @@ class LookupModel extends Eloquent abort("Looked up {$className} not found: {$field} => {$value}"); } - session([$key => $server]); + Cache::put($key, $server, 120); } else { config(['database.default' => $current]); } diff --git a/database/seeds/LanguageSeeder.php b/database/seeds/LanguageSeeder.php index 2ccce1c7b779..2e25215108b0 100644 --- a/database/seeds/LanguageSeeder.php +++ b/database/seeds/LanguageSeeder.php @@ -16,7 +16,7 @@ class LanguageSeeder extends Seeder ['name' => 'Italian', 'locale' => 'it'], ['name' => 'German', 'locale' => 'de'], ['name' => 'French', 'locale' => 'fr'], - ['name' => 'Brazilian Portuguese', 'locale' => 'pt_BR'], + ['name' => 'Portuguese - Brazilian', 'locale' => 'pt_BR'], ['name' => 'Dutch', 'locale' => 'nl'], ['name' => 'Spanish', 'locale' => 'es'], ['name' => 'Norwegian', 'locale' => 'nb_NO'],