mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 04:04:47 -04:00
Merge pull request #4296 from beganovich/v5-migration-fixes
(v5) (wip) (no-merge) Refactor accepting new migrations
This commit is contained in:
commit
861c4ef4e7
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoice Ninja (https://invoiceninja.com).
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
*
|
*
|
||||||
@ -198,204 +199,124 @@ class MigrationController extends BaseController
|
|||||||
* @param Company $company
|
* @param Company $company
|
||||||
* @return \Illuminate\Http\JsonResponse|void
|
* @return \Illuminate\Http\JsonResponse|void
|
||||||
*/
|
*/
|
||||||
public function startMigration(Request $request, Company $company)
|
public function startMigration(Request $request)
|
||||||
{
|
{
|
||||||
$user = auth()->user();
|
$companies = json_decode($request->companies);
|
||||||
|
|
||||||
if (app()->environment() === 'local') {
|
if (app()->environment() === 'local') {
|
||||||
info([
|
info($request->all());
|
||||||
'Company key' => $company->company_key,
|
|
||||||
'Request key' => $request->company_key,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$existing_company = Company::where('company_key', $request->company_key)->first();
|
foreach ($companies as $company) {
|
||||||
|
$is_valid = $request->file($company->company_key)->isValid();
|
||||||
|
|
||||||
$checks = [
|
if (!$is_valid) {
|
||||||
'same_keys' => $request->company_key == $company->company_key,
|
// We might want to send user something's wrong with migration or nope?
|
||||||
'existing_company' => (bool) $existing_company,
|
|
||||||
'with_force' => (bool) ($request->has('force') && ! empty($request->force)),
|
|
||||||
];
|
|
||||||
|
|
||||||
// If same company keys, and force provided.
|
continue;
|
||||||
if ($checks['same_keys'] && $checks['with_force']) {
|
|
||||||
info('Migrating: Same company keys, with force.');
|
|
||||||
|
|
||||||
if ($company) {
|
|
||||||
$this->purgeCompany($company);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$account = auth()->user()->account;
|
$user = auth()->user();
|
||||||
$company = (new ImportMigrations())->getCompany($account);
|
|
||||||
$company->is_disabled = true;
|
|
||||||
$company->save();
|
|
||||||
|
|
||||||
$account->default_company_id = $company->id;
|
// Look for possible existing company (based on company keys).
|
||||||
$account->save();
|
$existing_company = Company::where('company_key', $request->company_key)->first();
|
||||||
|
|
||||||
$company_token = new CompanyToken();
|
$checks = [
|
||||||
$company_token->user_id = $user->id;
|
'existing_company' => (bool) $existing_company,
|
||||||
$company_token->company_id = $company->id;
|
'force' => property_exists($company, 'force') ? (bool) $company->force : false,
|
||||||
$company_token->account_id = $account->id;
|
];
|
||||||
$company_token->name = $request->token_name ?? Str::random(12);
|
|
||||||
$company_token->token = $request->token ?? Str::random(64);
|
|
||||||
$company_token->is_system = true;
|
|
||||||
$company_token->save();
|
|
||||||
|
|
||||||
$user->companies()->attach($company->id, [
|
// If there's existing company and ** no ** force is provided - skip migration.
|
||||||
'account_id' => $account->id,
|
if ($checks['existing_company'] == true && $checks['force'] == false) {
|
||||||
'is_owner' => 1,
|
info('Migrating: Existing company without force. (CASE_01)');
|
||||||
'is_admin' => 1,
|
|
||||||
'is_locked' => 0,
|
|
||||||
'notifications' => CompanySettings::notificationDefaults(),
|
|
||||||
'permissions' => '',
|
|
||||||
'settings' => null,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If keys are same and no force has been provided.
|
MailRouter::dispatch(new ExistingMigration(), $company, $user);
|
||||||
if ($checks['same_keys'] && ! $checks['with_force']) {
|
|
||||||
info('Migrating: Same company keys, no force provided.');
|
|
||||||
|
|
||||||
MailRouter::dispatch(new ExistingMigration(), $company, $user);
|
return response()->json([
|
||||||
|
'_id' => Str::uuid(),
|
||||||
return response()->json([
|
'method' => config('queue.default'),
|
||||||
'_id' => Str::uuid(),
|
'started_at' => now(),
|
||||||
'method' => config('queue.default'),
|
], 200);
|
||||||
'started_at' => now(),
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If keys ain't same, but existing company without force.
|
|
||||||
if (! $checks['same_keys'] && $checks['existing_company'] && ! $checks['with_force']) {
|
|
||||||
info('Migrating: Different keys, existing company with the key without the force option.');
|
|
||||||
|
|
||||||
MailRouter::dispatch(new ExistingMigration(), $company, $user);
|
|
||||||
|
|
||||||
return response()->json([
|
|
||||||
'_id' => Str::uuid(),
|
|
||||||
'method' => config('queue.default'),
|
|
||||||
'started_at' => now(),
|
|
||||||
], 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If keys ain't same, but existing company with force.
|
|
||||||
if (! $checks['same_keys'] && $checks['existing_company'] && $checks['with_force']) {
|
|
||||||
info('Migrating: Different keys, existing company with force option.');
|
|
||||||
|
|
||||||
if ($company) {
|
|
||||||
$this->purgeCompany($company);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$account = auth()->user()->account;
|
// If there's existing company and force ** is provided ** - purge the company and migrate again.
|
||||||
$company = (new ImportMigrations())->getCompany($account);
|
if ($checks['existing_company'] == true && $checks['force'] == true) {
|
||||||
|
|
||||||
$company->is_disabled = true;
|
|
||||||
$company->save();
|
|
||||||
|
|
||||||
$account->default_company_id = $company->id;
|
|
||||||
$account->save();
|
|
||||||
|
|
||||||
$company_token = new CompanyToken();
|
|
||||||
$company_token->user_id = $user->id;
|
|
||||||
$company_token->company_id = $company->id;
|
|
||||||
$company_token->account_id = $account->id;
|
|
||||||
$company_token->name = $request->token_name ?? Str::random(12);
|
|
||||||
$company_token->token = $request->token ?? Str::random(64);
|
|
||||||
$company_token->is_system = true;
|
|
||||||
|
|
||||||
$company_token->save();
|
|
||||||
|
|
||||||
$user->companies()->attach($company->id, [
|
|
||||||
'account_id' => $account->id,
|
|
||||||
'is_owner' => 1,
|
|
||||||
'is_admin' => 1,
|
|
||||||
'is_locked' => 0,
|
|
||||||
'notifications' => CompanySettings::notificationDefaults(),
|
|
||||||
'permissions' => '',
|
|
||||||
'settings' => null,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If keys ain't same, but with force.
|
|
||||||
if (! $checks['same_keys'] && $checks['with_force']) {
|
|
||||||
info('Migrating: Different keys with force.');
|
|
||||||
|
|
||||||
if ($existing_company) {
|
|
||||||
$this->purgeCompany($existing_company);
|
$this->purgeCompany($existing_company);
|
||||||
|
|
||||||
|
$account = auth()->user()->account;
|
||||||
|
$fresh_company = (new ImportMigrations())->getCompany($account);
|
||||||
|
$fresh_company->is_disabled = true;
|
||||||
|
$fresh_company->save();
|
||||||
|
|
||||||
|
$account->default_company_id = $fresh_company->id;
|
||||||
|
$account->save();
|
||||||
|
|
||||||
|
$fresh_company_token = new CompanyToken();
|
||||||
|
$fresh_company_token->user_id = $user->id;
|
||||||
|
$fresh_company_token->company_id = $fresh_company->id;
|
||||||
|
$fresh_company_token->account_id = $account->id;
|
||||||
|
$fresh_company_token->name = $request->token_name ?? Str::random(12);
|
||||||
|
$fresh_company_token->token = $request->token ?? Str::random(64);
|
||||||
|
$fresh_company_token->is_system = true;
|
||||||
|
$fresh_company_token->save();
|
||||||
|
|
||||||
|
$user->companies()->attach($fresh_company->id, [
|
||||||
|
'account_id' => $account->id,
|
||||||
|
'is_owner' => 1,
|
||||||
|
'is_admin' => 1,
|
||||||
|
'is_locked' => 0,
|
||||||
|
'notifications' => CompanySettings::notificationDefaults(),
|
||||||
|
'permissions' => '',
|
||||||
|
'settings' => null,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$account = auth()->user()->account;
|
// If there's no existing company migrate just normally.
|
||||||
$company = (new ImportMigrations())->getCompany($account);
|
if ($checks['existing_company'] == false) {
|
||||||
|
$account = auth()->user()->account;
|
||||||
$company->is_disabled = true;
|
$fresh_company = (new ImportMigrations())->getCompany($account);
|
||||||
$company->save();
|
|
||||||
|
|
||||||
$account->default_company_id = $company->id;
|
|
||||||
$account->save();
|
|
||||||
|
|
||||||
$company_token = new CompanyToken();
|
$fresh_company->is_disabled = true;
|
||||||
$company_token->user_id = $user->id;
|
$fresh_company->save();
|
||||||
$company_token->company_id = $company->id;
|
|
||||||
$company_token->account_id = $account->id;
|
|
||||||
$company_token->name = $request->token_name ?? Str::random(12);
|
|
||||||
$company_token->token = $request->token ?? Str::random(64);
|
|
||||||
$company_token->is_system = true;
|
|
||||||
|
|
||||||
$company_token->save();
|
$fresh_company_token = new CompanyToken();
|
||||||
|
$fresh_company_token->user_id = $user->id;
|
||||||
|
$fresh_company_token->company_id = $fresh_company->id;
|
||||||
|
$fresh_company_token->account_id = $account->id;
|
||||||
|
$fresh_company_token->name = $request->token_name ?? Str::random(12);
|
||||||
|
$fresh_company_token->token = $request->token ?? Str::random(64);
|
||||||
|
$fresh_company_token->is_system = true;
|
||||||
|
|
||||||
$user->companies()->attach($company->id, [
|
$fresh_company_token->save();
|
||||||
'account_id' => $account->id,
|
|
||||||
'is_owner' => 1,
|
$user->companies()->attach($fresh_company->id, [
|
||||||
'is_admin' => 1,
|
'account_id' => $account->id,
|
||||||
'is_locked' => 0,
|
'is_owner' => 1,
|
||||||
'notifications' => CompanySettings::notificationDefaults(),
|
'is_admin' => 1,
|
||||||
'permissions' => '',
|
'is_locked' => 0,
|
||||||
'settings' => null,
|
'notifications' => CompanySettings::notificationDefaults(),
|
||||||
]);
|
'permissions' => '',
|
||||||
|
'settings' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$migration_file = $request->file($company->company_key)
|
||||||
|
->storeAs(
|
||||||
|
'migrations',
|
||||||
|
$request->file($company->company_key)->getClientOriginalName()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (app()->environment() == 'testing') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
StartMigration::dispatch(base_path("storage/app/public/$migration_file"), $user, $fresh_company)->delay(now()->addSeconds(60));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
info($e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If keys ain't same, fresh migrate.
|
|
||||||
if (! $checks['same_keys'] && ! $checks['with_force']) {
|
|
||||||
info('Migrating: Vanilla, fresh migrate.');
|
|
||||||
|
|
||||||
$account = auth()->user()->account;
|
|
||||||
$company = (new ImportMigrations())->getCompany($account);
|
|
||||||
|
|
||||||
$company->is_disabled = true;
|
|
||||||
$company->save();
|
|
||||||
|
|
||||||
|
|
||||||
$company_token = new CompanyToken();
|
|
||||||
$company_token->user_id = $user->id;
|
|
||||||
$company_token->company_id = $company->id;
|
|
||||||
$company_token->account_id = $account->id;
|
|
||||||
$company_token->name = $request->token_name ?? Str::random(12);
|
|
||||||
$company_token->token = $request->token ?? Str::random(64);
|
|
||||||
$company_token->is_system = true;
|
|
||||||
|
|
||||||
$company_token->save();
|
|
||||||
|
|
||||||
$user->companies()->attach($company->id, [
|
|
||||||
'account_id' => $account->id,
|
|
||||||
'is_owner' => 1,
|
|
||||||
'is_admin' => 1,
|
|
||||||
'is_locked' => 0,
|
|
||||||
'notifications' => CompanySettings::notificationDefaults(),
|
|
||||||
'permissions' => '',
|
|
||||||
'settings' => null,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$migration_file = $request->file('migration')
|
|
||||||
->storeAs('migrations', $request->file('migration')->getClientOriginalName());
|
|
||||||
|
|
||||||
if (app()->environment() == 'testing') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StartMigration::dispatch(base_path("storage/app/public/$migration_file"), $user, $company)->delay(now()->addSeconds(60));
|
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'_id' => Str::uuid(),
|
'_id' => Str::uuid(),
|
||||||
'method' => config('queue.default'),
|
'method' => config('queue.default'),
|
||||||
|
@ -107,8 +107,7 @@ class StartMigration implements ShouldQueue
|
|||||||
|
|
||||||
$data = json_decode(file_get_contents($file), 1);
|
$data = json_decode(file_get_contents($file), 1);
|
||||||
|
|
||||||
Import::dispatchNow($data, $this->company, $this->user);
|
Import::dispatchNow($data['data'], $this->company, $this->user);
|
||||||
|
|
||||||
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
||||||
|
|
||||||
Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage()));
|
Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage()));
|
||||||
|
@ -123,7 +123,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
|
|||||||
Route::post('companies/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected');
|
Route::post('companies/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected');
|
||||||
Route::post('companies/purge_save_settings/{company}', 'MigrationController@purgeCompanySaveSettings')->middleware('password_protected');
|
Route::post('companies/purge_save_settings/{company}', 'MigrationController@purgeCompanySaveSettings')->middleware('password_protected');
|
||||||
|
|
||||||
Route::post('migration/start/{company}', 'MigrationController@startMigration');
|
Route::post('migration/start', 'MigrationController@startMigration');
|
||||||
|
|
||||||
Route::resource('companies', 'CompanyController'); // name = (companies. index / create / show / update / destroy / edit
|
Route::resource('companies', 'CompanyController'); // name = (companies. index / create / show / update / destroy / edit
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user