1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Util/StartMigration.php

160 lines
4.4 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Util;
use App\Exceptions\MigrationValidatorFailed;
use App\Exceptions\NonExistingMigrationFile;
use App\Exceptions\ProcessingMigrationArchiveFailed;
use App\Exceptions\ResourceDependencyMissing;
use App\Exceptions\ResourceNotAvailableForMigration;
use App\Libraries\MultiDB;
use App\Mail\MigrationFailed;
use App\Models\Company;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
2020-10-28 11:10:49 +01:00
use ZipArchive;
class StartMigration implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $filepath;
/**
* @var User
*/
private $user;
/**
* @var Company
*/
private $company;
/**
* Create a new job instance.
*
* @param $filepath
* @param User $user
* @param Company $company
*/
2020-11-18 11:46:36 +01:00
public $tries = 1;
2020-11-24 11:12:05 +01:00
public $timeout = 0;
2020-11-25 15:19:52 +01:00
// public $maxExceptions = 2;
2020-11-18 11:46:36 +01:00
2020-11-25 03:59:23 +01:00
//public $backoff = 86430;
public function __construct($filepath, User $user, Company $company)
{
$this->filepath = $filepath;
$this->user = $user;
$this->company = $company;
}
/**
* Execute the job.
*
2020-10-28 11:10:49 +01:00
* @return bool
*/
public function handle()
{
2021-01-28 03:53:40 +01:00
nlog("Inside Migration Job");
set_time_limit(0);
MultiDB::setDb($this->company->db);
auth()->login($this->user, false);
auth()->user()->setCompany($this->company);
2020-11-01 04:19:03 +01:00
$this->company->is_disabled = true;
$this->company->save();
2020-10-28 11:10:49 +01:00
$zip = new ZipArchive();
2020-11-29 23:58:31 +01:00
$archive = $zip->open(public_path("storage/{$this->filepath}"));
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
if($this->company->id == $this->company->account->default_company_id)
{
$new_default_company = $this->company->account->companies->first();
if ($new_default_company) {
$this->company->account->default_company_id = $new_default_company->id;
$this->company->account->save();
}
}
$update_product_flag = $this->company->update_products;
$this->company->update_products = false;
$this->company->save();
try {
if (! $archive) {
throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.');
}
2020-11-29 23:58:31 +01:00
$zip->extractTo(public_path("storage/migrations/{$filename}"));
$zip->close();
if (app()->environment() == 'testing') {
return true;
}
2020-11-29 23:58:31 +01:00
$file = public_path("storage/migrations/$filename/migration.json");
if (! file_exists($file)) {
throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.');
}
2021-05-03 14:25:45 +02:00
Import::dispatchNow($file, $this->company, $this->user);
Storage::deleteDirectory(public_path("storage/migrations/{$filename}"));
$this->company->account->default_company_id = $this->company->id;
$this->company->account->save();
$this->company->update_products = $update_product_flag;
$this->company->save();
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing | \Exception $e) {
$this->company->update_products = $update_product_flag;
$this->company->save();
Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage()));
if (app()->environment() !== 'production') {
info($e->getMessage());
}
}
//always make sure we unset the migration as running
return true;
}
public function failed($exception = null)
{
2020-11-25 15:19:52 +01:00
info(print_r($exception->getMessage(), 1));
}
}