filepath = $filepath; $this->user = $user; $this->company = $company; } /** * Execute the job. * * @return bool */ public function handle() { nlog("Inside Migration Job"); set_time_limit(0); MultiDB::setDb($this->company->db); auth()->login($this->user, false); auth()->user()->setCompany($this->company); $this->company->is_disabled = true; $this->company->save(); $zip = new ZipArchive(); $archive = $zip->open(public_path("storage/{$this->filepath}")); $filename = pathinfo($this->filepath, PATHINFO_FILENAME); $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.'); } $zip->extractTo(public_path("storage/migrations/{$filename}")); $zip->close(); if (app()->environment() == 'testing') { return true; } $file = public_path("storage/migrations/$filename/migration.json"); if (! file_exists($file)) { throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.'); } Import::dispatchNow($file, $this->company, $this->user); Storage::deleteDirectory(public_path("storage/migrations/{$filename}")); $this->company->update_products = $update_product_flag; $this->company->save(); App::forgetInstance('translator'); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->company->settings)); } catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing | \Exception $e) { $this->company->update_products = $update_product_flag; $this->company->save(); if(Ninja::isHosted()) app('sentry')->captureException($e); Mail::to($this->user->email, $this->user->name())->send(new MigrationFailed($e, $this->company, $e->getMessage())); if(Ninja::isHosted()){ $migration_failed = new MigrationFailed($e, $this->company, $e->getMessage()); $migration_failed->is_system = true; Mail::to('contact@invoiceninja.com', 'Failed Migration')->send($migration_failed); } if (app()->environment() !== 'production') { info($e->getMessage()); } } //always make sure we unset the migration as running return true; } public function failed($exception = null) { info(print_r($exception->getMessage(), 1)); } }