2020-01-23 21:35:00 +01:00
|
|
|
<?php
|
2020-04-30 13:45:47 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-04-30 13:45:47 +02:00
|
|
|
*/
|
2020-01-23 21:35:00 +01:00
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Exceptions\ClientHostedMigrationException;
|
|
|
|
use App\Exceptions\MigrationValidatorFailed;
|
|
|
|
use App\Exceptions\NonExistingMigrationFile;
|
|
|
|
use App\Exceptions\ProcessingMigrationArchiveFailed;
|
|
|
|
use App\Exceptions\ResourceDependencyMissing;
|
|
|
|
use App\Exceptions\ResourceNotAvailableForMigration;
|
2023-06-08 03:15:02 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Mail\MigrationFailed;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Ninja;
|
2020-01-23 21:35:00 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2023-06-08 03:15:02 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2021-04-25 13:04:48 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-10-26 04:57:44 +02:00
|
|
|
use ZipArchive;
|
2020-01-23 21:35:00 +01:00
|
|
|
|
|
|
|
class StartMigration implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2020-01-23 21:35:00 +01:00
|
|
|
|
|
|
|
private $filepath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Company
|
|
|
|
*/
|
|
|
|
private $company;
|
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
private $silent_migration;
|
|
|
|
|
2020-01-23 21:35:00 +01:00
|
|
|
/**
|
|
|
|
* 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-04-01 10:54:22 +02:00
|
|
|
|
2020-11-24 11:12:05 +01:00
|
|
|
public $timeout = 0;
|
2020-04-01 10:54:22 +02:00
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
public function __construct($filepath, User $user, Company $company, $silent_migration = false)
|
2020-01-23 21:35:00 +01:00
|
|
|
{
|
2020-03-12 21:38:22 +01:00
|
|
|
$this->filepath = $filepath;
|
2020-01-23 21:35:00 +01:00
|
|
|
$this->user = $user;
|
|
|
|
$this->company = $company;
|
2023-07-10 04:55:21 +02:00
|
|
|
$this->silent_migration = $silent_migration;
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return bool
|
2020-01-23 21:35:00 +01:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Inside Migration Job');
|
|
|
|
|
2023-06-08 03:15:02 +02:00
|
|
|
Cache::put("migration-{$this->company->company_key}", "started", 86400);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
set_time_limit(0);
|
|
|
|
|
2020-02-19 21:44:12 +01:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2020-03-04 05:06:27 +01:00
|
|
|
auth()->login($this->user, false);
|
|
|
|
|
|
|
|
auth()->user()->setCompany($this->company);
|
2020-03-05 21:30:32 +01:00
|
|
|
|
2020-11-01 04:19:03 +01:00
|
|
|
$this->company->is_disabled = true;
|
|
|
|
$this->company->save();
|
2020-04-01 10:54:22 +02:00
|
|
|
|
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}"));
|
2020-01-23 21:35:00 +01:00
|
|
|
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
|
|
|
|
|
2021-05-10 01:52:58 +02:00
|
|
|
$update_product_flag = $this->company->update_products;
|
|
|
|
|
|
|
|
$this->company->update_products = false;
|
|
|
|
$this->company->save();
|
|
|
|
|
2020-01-23 21:35:00 +01:00
|
|
|
try {
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $archive) {
|
2020-03-05 21:30:32 +01:00
|
|
|
throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
|
2020-11-29 23:58:31 +01:00
|
|
|
$zip->extractTo(public_path("storage/migrations/{$filename}"));
|
2020-03-03 23:44:42 +01:00
|
|
|
$zip->close();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (app()->environment() == 'testing') {
|
2020-05-27 06:46:19 +02:00
|
|
|
return true;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-03 23:44:42 +01:00
|
|
|
|
2020-11-29 23:58:31 +01:00
|
|
|
$file = public_path("storage/migrations/$filename/migration.json");
|
2020-04-01 10:54:22 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! file_exists($file)) {
|
2020-04-01 10:54:22 +02:00
|
|
|
throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.');
|
|
|
|
}
|
|
|
|
|
2023-07-10 04:55:21 +02:00
|
|
|
(new Import($file, $this->company, $this->user, [], $this->silent_migration))->handle();
|
2021-02-10 02:59:30 +01:00
|
|
|
|
2021-04-25 13:04:48 +02:00
|
|
|
Storage::deleteDirectory(public_path("storage/migrations/{$filename}"));
|
|
|
|
|
2021-05-10 01:52:58 +02:00
|
|
|
$this->company->update_products = $update_product_flag;
|
|
|
|
$this->company->save();
|
|
|
|
|
2023-06-08 03:15:02 +02:00
|
|
|
Cache::put("migration-{$this->company->company_key}", "completed", 86400);
|
|
|
|
|
2021-08-25 12:53:13 +02:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
2023-02-09 03:51:03 +01:00
|
|
|
} catch (ClientHostedMigrationException | NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing | \Exception $e) {
|
2021-05-10 01:52:58 +02:00
|
|
|
$this->company->update_products = $update_product_flag;
|
|
|
|
$this->company->save();
|
2021-02-10 02:59:30 +01:00
|
|
|
|
2023-06-08 03:15:02 +02:00
|
|
|
Cache::put("migration-{$this->company->company_key}", "failed", 86400);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2021-05-22 08:16:51 +02:00
|
|
|
app('sentry')->captureException($e);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-03-05 21:30:32 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(!$this->silent_migration) {
|
2023-07-10 04:55:21 +02:00
|
|
|
Mail::to($this->user->email, $this->user->name())->send(new MigrationFailed($e, $this->company, $e->getMessage()));
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2021-08-10 03:40:58 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2021-08-10 03:40:58 +02:00
|
|
|
$migration_failed = new MigrationFailed($e, $this->company, $e->getMessage());
|
|
|
|
$migration_failed->is_system = true;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
Mail::to('contact@invoiceninja.com', 'Failed Migration')->send($migration_failed);
|
2021-08-10 03:40:58 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (app()->environment() !== 'production') {
|
|
|
|
info($e->getMessage());
|
|
|
|
}
|
2023-06-08 03:15:02 +02:00
|
|
|
|
|
|
|
Storage::deleteDirectory(public_path("storage/migrations/{$filename}"));
|
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
}
|
2020-05-27 06:46:19 +02:00
|
|
|
|
|
|
|
return true;
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|
2020-02-25 23:36:11 +01:00
|
|
|
|
2020-04-01 10:54:22 +02:00
|
|
|
public function failed($exception = null)
|
2020-02-25 23:36:11 +01:00
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
info(print_r($exception->getMessage(), 1));
|
2020-02-25 23:36:11 +01:00
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|