From 8121a0166d228be2dc022e2a5b2501ddd67bf243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 24 Feb 2020 22:15:07 +0100 Subject: [PATCH] Importing migration data via HTTP (#3365) * Accept migration over HTTP * Comment wip tests --- app/Http/Controllers/MigrationController.php | 13 +++---------- .../Migration/UploadMigrationFileRequest.php | 2 +- app/Jobs/Util/StartMigration.php | 19 +++++++++++++------ routes/api.php | 2 +- tests/Feature/MigrationTest.php | 1 + 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 41cc0db9b5..0fc122b25e 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -184,20 +184,13 @@ class MigrationController extends BaseController * ), * ) */ - public function startMigration(UploadMigrationFileRequest $request) + public function startMigration(Request $request, Company $company) { - $file = $request->file('migration')->storeAs( - 'migrations', $request->file('migration')->getClientOriginalName() - ); - - if(!auth()->user()->company) - return response()->json(['message' => 'Company doesn\'t exists.'], 402); - if($request->has('force')) - $this->purgeCompany(auth()->user()->company); + $this->purgeCompany($company); if(app()->environment() !== 'testing') { - StartMigration::dispatchNow($file, auth()->user(), auth()->user()->company); + StartMigration::dispatchNow($request->file('migration'), auth()->user(), $company); } return response()->json([], 200); diff --git a/app/Http/Requests/Migration/UploadMigrationFileRequest.php b/app/Http/Requests/Migration/UploadMigrationFileRequest.php index 3b1aa251e5..b0c159a3d0 100644 --- a/app/Http/Requests/Migration/UploadMigrationFileRequest.php +++ b/app/Http/Requests/Migration/UploadMigrationFileRequest.php @@ -29,7 +29,7 @@ class UploadMigrationFileRequest extends FormRequest /** We'll skip mime validation while running tests. */ if(app()->environment() !== 'testing') { - $rules['migration'] = ['required', 'mimes:zip']; + $rules['migration'] = ['required', 'file', 'mimes:zip']; } return $rules; diff --git a/app/Jobs/Util/StartMigration.php b/app/Jobs/Util/StartMigration.php index 42e4066d3c..891195f034 100644 --- a/app/Jobs/Util/StartMigration.php +++ b/app/Jobs/Util/StartMigration.php @@ -2,15 +2,15 @@ namespace App\Jobs\Util; -use App\Exceptions\ProcessingMigrationArchiveFailed; -use App\Libraries\MultiDB; -use App\Models\Company; use App\Models\User; +use App\Models\Company; +use App\Libraries\MultiDB; use Illuminate\Bus\Queueable; +use Illuminate\Queue\SerializesModels; +use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; +use App\Exceptions\ProcessingMigrationArchiveFailed; class StartMigration implements ShouldQueue { @@ -50,7 +50,6 @@ class StartMigration implements ShouldQueue */ public function handle() { - MultiDB::setDb($this->company->db); $zip = new \ZipArchive(); @@ -62,6 +61,14 @@ class StartMigration implements ShouldQueue if ($archive) { $zip->extractTo(storage_path("migrations/{$filename}")); $zip->close(); + + $migration_file = storage_path("migrations/$filename/migration.json"); + $handle = fopen($migration_file, "r"); + $migration_file = fread($handle, filesize($migration_file)); + fclose($handle); + + $data = json_decode($migration_file,1); + Import::dispatchNow($data, $this->company, $this->user); } else { throw new ProcessingMigrationArchiveFailed(); } diff --git a/routes/api.php b/routes/api.php index cfb5c93c6d..ec73066042 100644 --- a/routes/api.php +++ b/routes/api.php @@ -99,7 +99,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('migration/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected'); Route::post('migration/purge_save_settings/{company}', 'MigrationController@purgeCompanySaveSettings')->middleware('password_protected'); - Route::post('migration/start', 'MigrationController@startMigration')->middleware('password_protected'); + Route::post('migration/start/{company}', 'MigrationController@startMigration'); Route::resource('companies', 'CompanyController');// name = (companies. index / create / show / update / destroy / edit diff --git a/tests/Feature/MigrationTest.php b/tests/Feature/MigrationTest.php index 673d7f4321..6731883240 100644 --- a/tests/Feature/MigrationTest.php +++ b/tests/Feature/MigrationTest.php @@ -116,5 +116,6 @@ class MigrationTest extends TestCase // $response->assertStatus(200); // $this->assertTrue(file_exists(base_path('storage/migrations/migration/migration.json'))); // } + }