1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Console/Commands/ForceMigration.php

154 lines
3.7 KiB
PHP
Raw Normal View History

2021-07-23 01:37:08 +02:00
<?php
namespace App\Console\Commands;
use App\Jobs\HostedMigration;
use App\Libraries\Utils;
use App\Models\Company;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
class ForceMigration extends Command
{
2021-07-25 07:53:27 +02:00
// define('DB_NINJA_1', 'db-ninja-1');
// define('DB_NINJA_2', 'db-ninja-2');
2021-07-23 01:37:08 +02:00
2021-07-25 07:53:27 +02:00
public $db = DB_NINJA_1;
public $force = false;
public $user = false;
2021-07-23 01:37:08 +02:00
/**
* The name and signature of the console command.
*
* @var string
*/
2021-07-25 07:53:27 +02:00
protected $signature = 'ninja:force-migrate-v5 {--email=} {--force=} {--database=}';
2021-07-23 01:37:08 +02:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Force migrate accounts to v5 - (Hosted function only)';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2021-07-25 07:53:27 +02:00
if($this->option('database'))
$this->db = $this->option('database');
if($this->option('force'))
$this->force = $this->option('force');
2021-07-23 01:37:08 +02:00
if(!Utils::isNinjaProd())
return;
2021-07-25 07:53:27 +02:00
config(['database.default' => $this->db]);
$company = $this->getCompany();
if(!$company){
$this->logMessage('Could not find a company with that email address');
exit;
}
2021-07-23 01:37:08 +02:00
2021-07-25 07:53:27 +02:00
$this->forceMigrate($company);
2021-07-23 01:37:08 +02:00
}
2021-07-25 07:53:27 +02:00
private function getCompany()
2021-07-23 01:37:08 +02:00
{
2021-07-25 07:53:27 +02:00
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)
2021-07-25 03:41:13 +02:00
->whereNull('plan')
->orWhereIn('plan', ['','free'])
->whereHas('accounts', function ($query){
2021-07-25 07:53:27 +02:00
$query->where('account_key', '!=', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h');
$query->orWhere('account_key', '!=', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx702');
$query->orWhere('account_key', '!=', 'AsFmBAeLXF0IKf7tmi0eiyZfmWW9hxMT');
})
2021-07-25 03:41:13 +02:00
->with('accounts')
->withCount('accounts')
->having('accounts_count', '>=', 1)
->first();
2021-07-23 01:37:08 +02:00
2021-07-25 07:53:27 +02:00
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;
}
2021-07-23 01:37:08 +02:00
if($company){
foreach($company->accounts as $key => $account)
{
$data['companies'][$key]['id'] = $account->id;
2021-07-25 07:53:27 +02:00
$data['companies'][$key]['force'] = $this->force;
2021-07-23 01:37:08 +02:00
}
2021-07-25 07:53:27 +02:00
$this->dispatch(new HostedMigration($this->user, $data, $this->db, true));
2021-07-24 02:33:07 +02:00
$company->is_migrated = true;
$company->save();
2021-07-23 01:37:08 +02:00
}
}
}