Minor fixes for migration

This commit is contained in:
David Bomba 2021-07-25 15:53:27 +10:00
parent 74a56afe6d
commit b9a0438632
2 changed files with 81 additions and 13 deletions

View File

@ -11,13 +11,20 @@ use Illuminate\Support\Facades\Auth;
class ForceMigration extends Command 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. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'ninja:force-migrate-v5'; protected $signature = 'ninja:force-migrate-v5 {--email=} {--force=} {--database=}';
/** /**
* The console command description. * The console command description.
@ -42,34 +49,90 @@ class ForceMigration extends Command
*/ */
public function handle() 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()) if(!Utils::isNinjaProd())
return; 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') ->whereNull('plan')
->orWhereIn('plan', ['','free']) ->orWhereIn('plan', ['','free'])
->whereHas('accounts', function ($query){ ->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') ->with('accounts')
->withCount('accounts') ->withCount('accounts')
->having('accounts_count', '>=', 1) ->having('accounts_count', '>=', 1)
->first(); ->first();
$user = $company->accounts->first()->users()->whereNull('public_id')->orWhere('public_id', 0)->first(); return $company;
$db = DB_NINJA_1;
}
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){ if($company){
@ -77,10 +140,11 @@ class ForceMigration extends Command
{ {
$data['companies'][$key]['id'] = $account->id; $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->is_migrated = true;
$company->save(); $company->save();

View File

@ -2,6 +2,7 @@
namespace App\Traits; namespace App\Traits;
use App\Libraries\Utils;
use App\Models\AccountGateway; use App\Models\AccountGateway;
use App\Models\AccountGatewaySettings; use App\Models\AccountGatewaySettings;
use App\Models\AccountGatewayToken; use App\Models\AccountGatewayToken;
@ -432,6 +433,9 @@ trait GenerateMigrationResources
{ {
$transformed = []; $transformed = [];
if(!Utils::isNinja())
return $transformed;
$ninja_client = Client::where('public_id', $this->account->id)->first(); $ninja_client = Client::where('public_id', $this->account->id)->first();
if(!$ninja_client) if(!$ninja_client)