From b9a043863210237a3e857aba5237f649abddce23 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 25 Jul 2021 15:53:27 +1000 Subject: [PATCH] Minor fixes for migration --- app/Console/Commands/ForceMigration.php | 90 +++++++++++++++++++---- app/Traits/GenerateMigrationResources.php | 4 + 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/app/Console/Commands/ForceMigration.php b/app/Console/Commands/ForceMigration.php index d18b52513ab1..b7d097008c77 100644 --- a/app/Console/Commands/ForceMigration.php +++ b/app/Console/Commands/ForceMigration.php @@ -11,13 +11,20 @@ use Illuminate\Support\Facades\Auth; class ForceMigration extends Command { + // define('DB_NINJA_1', 'db-ninja-1'); + // define('DB_NINJA_2', 'db-ninja-2'); + public $db = DB_NINJA_1; + + public $force = false; + + public $user = false; /** * The name and signature of the console command. * * @var string */ - protected $signature = 'ninja:force-migrate-v5'; + protected $signature = 'ninja:force-migrate-v5 {--email=} {--force=} {--database=}'; /** * The console command description. @@ -42,34 +49,90 @@ class ForceMigration extends Command */ public function handle() { - return; + + if($this->option('database')) + $this->db = $this->option('database'); + + if($this->option('force')) + $this->force = $this->option('force'); if(!Utils::isNinjaProd()) return; - config(['database.default' => DB_NINJA_1]); + config(['database.default' => $this->db]); - $this->forceMigrate(); + $company = $this->getCompany(); + + if(!$company){ + + $this->logMessage('Could not find a company with that email address'); + exit; + + } + + $this->forceMigrate($company); } - private function forceMigrate() - { - $data = []; - $company = Company::on(DB_NINJA_1) + private function getCompany() + { + + if($this->option('email')){ + + $user = User::on($this->db) + ->where('email', $this->option('email')) + ->whereNull('public_id') + ->orWhere('public_id', 0) + ->first(); + + if(!$user){ + $this->logMessage('Could not find an owner user with that email address'); + exit; + } + + $this->user = $user; + + return $user->account->company; + + } + + $company = Company::on($this->db) ->whereNull('plan') ->orWhereIn('plan', ['','free']) ->whereHas('accounts', function ($query){ - $query->where('account_key', 'NOT LIKE', substr(NINJA_ACCOUNT_KEY, 0, 30) . '%'); - }) + $query->where('account_key', '!=', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h'); + $query->orWhere('account_key', '!=', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx702'); + $query->orWhere('account_key', '!=', 'AsFmBAeLXF0IKf7tmi0eiyZfmWW9hxMT'); + }) ->with('accounts') ->withCount('accounts') ->having('accounts_count', '>=', 1) ->first(); - $user = $company->accounts->first()->users()->whereNull('public_id')->orWhere('public_id', 0)->first(); - $db = DB_NINJA_1; + return $company; + + } + + private function logMessage($str) + { + $str = date('Y-m-d h:i:s').' '.$str; + $this->info($str); + $this->log .= $str."\n"; + } + + private function forceMigrate($company) + { + $data = []; + + if(!$this->user) + $this->user = $company->accounts->first()->users()->whereNull('public_id')->orWhere('public_id', 0)->first(); + + + if(!$this->user){ + $this->logMessage('Could not find an owner user with that email address'); + exit; + } if($company){ @@ -77,10 +140,11 @@ class ForceMigration extends Command { $data['companies'][$key]['id'] = $account->id; + $data['companies'][$key]['force'] = $this->force; } - $this->dispatch(new HostedMigration($user, $data, $db, true)); + $this->dispatch(new HostedMigration($this->user, $data, $this->db, true)); $company->is_migrated = true; $company->save(); diff --git a/app/Traits/GenerateMigrationResources.php b/app/Traits/GenerateMigrationResources.php index 1d25e554bcb3..5518c663af5c 100644 --- a/app/Traits/GenerateMigrationResources.php +++ b/app/Traits/GenerateMigrationResources.php @@ -2,6 +2,7 @@ namespace App\Traits; +use App\Libraries\Utils; use App\Models\AccountGateway; use App\Models\AccountGatewaySettings; use App\Models\AccountGatewayToken; @@ -432,6 +433,9 @@ trait GenerateMigrationResources { $transformed = []; + if(!Utils::isNinja()) + return $transformed; + $ninja_client = Client::where('public_id', $this->account->id)->first(); if(!$ninja_client)