Updates for silent migrations

This commit is contained in:
David Bomba 2023-07-10 12:55:21 +10:00
parent 40385a1cd7
commit dd61b41919
5 changed files with 35 additions and 15 deletions

View File

@ -32,6 +32,8 @@ class MigrationController extends BaseController
{
use DispatchesJobs;
public bool $silent_migration = false;
public function __construct()
{
parent::__construct();
@ -260,6 +262,9 @@ class MigrationController extends BaseController
{
nlog('Starting Migration');
if($request->has('silent_migration'))
$this->silent_migration = true;
if ($request->companies) {
//handle Laravel 5.5 UniHTTP
$companies = json_decode($request->companies, 1);
@ -312,7 +317,9 @@ class MigrationController extends BaseController
$nmo->company = $user->account->companies()->first();
$nmo->settings = $user->account->companies()->first()->settings;
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo, true);
if(!$this->silent_migration)
NinjaMailerJob::dispatch($nmo, true);
return;
} elseif ($existing_company && $company_count > 10) {
@ -321,7 +328,9 @@ class MigrationController extends BaseController
$nmo->company = $user->account->companies()->first();
$nmo->settings = $user->account->companies()->first()->settings;
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo, true);
if(!$this->silent_migration)
NinjaMailerJob::dispatch($nmo, true);
return;
}
@ -341,7 +350,8 @@ class MigrationController extends BaseController
$nmo->settings = $user->account->companies()->first();
$nmo->to_user = $user;
NinjaMailerJob::dispatch($nmo, true);
if(!$this->silent_migration)
NinjaMailerJob::dispatch($nmo, true);
return response()->json([
'_id' => Str::uuid(),
@ -431,9 +441,9 @@ class MigrationController extends BaseController
nlog($migration_file);
if (Ninja::isHosted()) {
StartMigration::dispatch($migration_file, $user, $fresh_company)->onQueue('migration');
StartMigration::dispatch($migration_file, $user, $fresh_company, $this->silent_migration)->onQueue('migration');
} else {
StartMigration::dispatch($migration_file, $user, $fresh_company);
StartMigration::dispatch($migration_file, $user, $fresh_company, $this->silent_migration);
}
}

View File

@ -35,6 +35,7 @@ class ProtectedDownloadController extends BaseController
return response()->streamDownload(function () use ($hashed_path) {
echo Storage::get($hashed_path);
}, basename($hashed_path), []);
}

View File

@ -165,6 +165,8 @@ class Import implements ShouldQueue
public $timeout = 10000000;
public $silent_migration;
// public $backoff = 86430;
// public $maxExceptions = 2;
@ -176,12 +178,13 @@ class Import implements ShouldQueue
* @param User $user
* @param array $resources
*/
public function __construct(string $file_path, Company $company, User $user, array $resources = [])
public function __construct(string $file_path, Company $company, User $user, array $resources = [], $silent_migration = false)
{
$this->file_path = $file_path;
$this->company = $company;
$this->user = $user;
$this->resources = $resources;
$this->silent_migration = $silent_migration;
}
public function middleware()
@ -263,8 +266,9 @@ class Import implements ShouldQueue
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
Mail::to($this->user->email, $this->user->name())
->send(new MigrationCompleted($this->company->id, $this->company->db, implode("<br>", $check_data)));
if(!$this->silent_migration)
Mail::to($this->user->email, $this->user->name())->send(new MigrationCompleted($this->company->id, $this->company->db, implode("<br>", $check_data)));
} catch(\Exception $e) {
nlog($e->getMessage());
}
@ -641,7 +645,6 @@ class Import implements ShouldQueue
$user = $user_repository->save($modified, $this->fetchUser($resource['email']), true, true);
$user->email_verified_at = now();
// $user->confirmation_code = '';
if ($modified['deleted_at']) {
$user->deleted_at = now();
@ -1590,7 +1593,9 @@ class Import implements ShouldQueue
$nmo->company = $this->company;
$nmo->settings = $this->company->settings;
$nmo->to_user = $this->user;
NinjaMailerJob::dispatch($nmo, true);
if(!$this->silent_migration)
NinjaMailerJob::dispatch($nmo, true);
$modified['gateway_key'] = 'd14dd26a47cecc30fdd65700bfb67b34';
}

View File

@ -49,6 +49,8 @@ class StartMigration implements ShouldQueue
*/
private $company;
private $silent_migration;
/**
* Create a new job instance.
*
@ -60,11 +62,12 @@ class StartMigration implements ShouldQueue
public $timeout = 0;
public function __construct($filepath, User $user, Company $company)
public function __construct($filepath, User $user, Company $company, $silent_migration = false)
{
$this->filepath = $filepath;
$this->user = $user;
$this->company = $company;
$this->silent_migration = $silent_migration;
}
/**
@ -116,7 +119,7 @@ class StartMigration implements ShouldQueue
throw new NonExistingMigrationFile('Migration file does not exist, or it is corrupted.');
}
(new Import($file, $this->company, $this->user))->handle();
(new Import($file, $this->company, $this->user, [], $this->silent_migration))->handle();
Storage::deleteDirectory(public_path("storage/migrations/{$filename}"));
@ -138,7 +141,8 @@ class StartMigration implements ShouldQueue
app('sentry')->captureException($e);
}
Mail::to($this->user->email, $this->user->name())->send(new MigrationFailed($e, $this->company, $e->getMessage()));
if(!$this->silent_migration)
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());

View File

@ -39,7 +39,7 @@ class UserRepository extends BaseRepository
* @param bool $unset_company_user
* @return \App\Models\User user Object
*/
public function save(array $data, User $user, $unset_company_user = false)
public function save(array $data, User $user, $unset_company_user = false, $is_migrating = false)
{
$details = $data;
@ -71,7 +71,7 @@ class UserRepository extends BaseRepository
$user->password = Hash::make($data['password']);
}
if (! $user->confirmation_code) {
if (! $user->confirmation_code && !$is_migrating) {
$user->confirmation_code = $this->createDbHash($company->db);
}