From 282421deb8e2ecbdaac3586548640cb90e20e583 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 5 Feb 2018 16:16:24 -0600 Subject: [PATCH] Add a /update to update an install #164 --- .../Http/Controllers/InstallerController.php | 17 ++-- .../Http/Controllers/UpdaterController.php | 75 ++++++++++++++++++ modules/Installer/Http/Routes/update.php | 8 +- .../Providers/InstallerServiceProvider.php | 4 +- .../views/{ => install}/index-start.blade.php | 0 .../steps/step1-requirements.blade.php | 0 .../{ => install}/steps/step2-db.blade.php | 0 .../steps/step2a-db_output.blade.php | 0 .../{ => install}/steps/step3-user.blade.php | 0 .../steps/step3a-completed.blade.php | 0 .../views/update/index-start.blade.php | 12 +++ .../update/steps/step1-no-update.blade.php | 13 ++++ .../steps/step1-update-available.blade.php | 12 +++ .../steps/step2-migrations-done.blade.php | 18 +++++ .../Installer/Services/DatabaseService.php | 6 -- .../Installer/Services/MigrationService.php | 77 +++++++++++++++++++ resources/stubs/modules/migration/add.stub | 2 +- resources/stubs/modules/migration/create.stub | 2 +- resources/stubs/modules/migration/delete.stub | 2 +- resources/stubs/modules/migration/drop.stub | 2 +- resources/stubs/modules/migration/plain.stub | 2 +- 21 files changed, 232 insertions(+), 20 deletions(-) create mode 100644 modules/Installer/Http/Controllers/UpdaterController.php rename modules/Installer/Resources/views/{ => install}/index-start.blade.php (100%) rename modules/Installer/Resources/views/{ => install}/steps/step1-requirements.blade.php (100%) rename modules/Installer/Resources/views/{ => install}/steps/step2-db.blade.php (100%) rename modules/Installer/Resources/views/{ => install}/steps/step2a-db_output.blade.php (100%) rename modules/Installer/Resources/views/{ => install}/steps/step3-user.blade.php (100%) rename modules/Installer/Resources/views/{ => install}/steps/step3a-completed.blade.php (100%) create mode 100644 modules/Installer/Resources/views/update/index-start.blade.php create mode 100644 modules/Installer/Resources/views/update/steps/step1-no-update.blade.php create mode 100644 modules/Installer/Resources/views/update/steps/step1-update-available.blade.php create mode 100644 modules/Installer/Resources/views/update/steps/step2-migrations-done.blade.php create mode 100644 modules/Installer/Services/MigrationService.php diff --git a/modules/Installer/Http/Controllers/InstallerController.php b/modules/Installer/Http/Controllers/InstallerController.php index 8fae1a5d..09d28717 100644 --- a/modules/Installer/Http/Controllers/InstallerController.php +++ b/modules/Installer/Http/Controllers/InstallerController.php @@ -19,6 +19,7 @@ use App\Http\Controllers\Controller; use Modules\Installer\Services\DatabaseService; use Modules\Installer\Services\ConfigService; +use Modules\Installer\Services\MigrationService; use Modules\Installer\Services\RequirementsService; use Symfony\Component\HttpFoundation\File\Exception\FileException; @@ -29,6 +30,7 @@ class InstallerController extends Controller $analyticsSvc, $dbService, $envService, + $migrationSvc, $reqService, $userService; @@ -37,6 +39,7 @@ class InstallerController extends Controller AnalyticsService $analyticsSvc, DatabaseService $dbService, ConfigService $envService, + MigrationService $migrationSvc, RequirementsService $reqService, UserService $userService ) { @@ -44,6 +47,7 @@ class InstallerController extends Controller $this->analyticsSvc = $analyticsSvc; $this->dbService = $dbService; $this->envService = $envService; + $this->migrationSvc = $migrationSvc; $this->reqService = $reqService; $this->userService = $userService; } @@ -56,7 +60,7 @@ class InstallerController extends Controller return view('installer::errors/already-installed'); } - return view('installer::index-start'); + return view('installer::install/index-start'); } protected function testDb(Request $request) @@ -127,7 +131,7 @@ class InstallerController extends Controller # Make sure there are no false values $passed = !\in_array(false, $statuses, true); - return view('installer::steps/step1-requirements', [ + return view('installer::install/steps/step1-requirements', [ 'php' => $php_version, 'extensions' => $extensions, 'directories' => $directories, @@ -141,7 +145,7 @@ class InstallerController extends Controller public function step2(Request $request) { $db_types = ['mysql' => 'mysql', 'sqlite' => 'sqlite']; - return view('installer::steps/step2-db', [ + return view('installer::install/steps/step2-db', [ 'db_types' => $db_types, ]); } @@ -204,6 +208,7 @@ class InstallerController extends Controller try { $console_out .= $this->dbService->setupDB(); + $console_out .= $this->migrationSvc->runAllMigrations(); } catch(QueryException $e) { flash()->error($e->getMessage()); return redirect(route('installer.step2'))->withInput(); @@ -211,7 +216,7 @@ class InstallerController extends Controller $console_out = trim($console_out); - return view('installer::steps/step2a-db_output', [ + return view('installer::install/steps/step2a-db_output', [ 'console_output' => $console_out ]); } @@ -221,7 +226,7 @@ class InstallerController extends Controller */ public function step3(Request $request) { - return view('installer::steps/step3-user', []); + return view('installer::install/steps/step3-user', []); } /** @@ -283,7 +288,7 @@ class InstallerController extends Controller $this->analyticsSvc->sendInstall(); - return view('installer::steps/step3a-completed', []); + return view('installer::install/steps/step3a-completed', []); } /** diff --git a/modules/Installer/Http/Controllers/UpdaterController.php b/modules/Installer/Http/Controllers/UpdaterController.php new file mode 100644 index 00000000..b9a949ae --- /dev/null +++ b/modules/Installer/Http/Controllers/UpdaterController.php @@ -0,0 +1,75 @@ +migrationSvc = $migrationSvc; + } + + /** + * Display a listing of the resource. + */ + public function index() + { + return view('installer::update/index-start'); + } + + /** + * Step 1. Check if there's an update available. Check if there + * are any unrun migrations + */ + public function step1(Request $request) + { + $migrations = $this->migrationSvc->migrationsAvailable(); + if(\count($migrations) > 0) { + return view('installer::update/steps/step1-update-available'); + } + + Log::info('No migrations found'); + return view('installer::update/steps/step1-no-update'); + } + + /** + * Step 2 Run all of the migrations + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + */ + public function run_migrations(Request $request) + { + Log::info('Update: run_migrations', $request->post()); + + $migrations = $this->migrationSvc->migrationsAvailable(); + if(\count($migrations) === 0) { + return redirect(route('update.complete')); + } + + $output = $this->migrationSvc->runAllMigrations(); + + return view('installer::update/steps/step2-migrations-done', [ + 'console_output' => $output, + ]); + } + + /** + * Final step + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + */ + public function complete(Request $request) + { + return redirect('/login'); + } +} diff --git a/modules/Installer/Http/Routes/update.php b/modules/Installer/Http/Routes/update.php index 5556acaf..82247fb7 100644 --- a/modules/Installer/Http/Routes/update.php +++ b/modules/Installer/Http/Routes/update.php @@ -1,3 +1,9 @@ name('index'); + +Route::get('/step1', 'UpdaterController@step1')->name('step1'); +Route::post('/step1', 'UpdaterController@step1')->name('step1'); + +Route::post('/run-migrations', 'UpdaterController@run_migrations')->name('run_migrations'); +Route::get('/complete', 'UpdaterController@complete')->name('complete'); diff --git a/modules/Installer/Providers/InstallerServiceProvider.php b/modules/Installer/Providers/InstallerServiceProvider.php index ff7d09e1..cf7f9e21 100644 --- a/modules/Installer/Providers/InstallerServiceProvider.php +++ b/modules/Installer/Providers/InstallerServiceProvider.php @@ -44,8 +44,8 @@ class InstallerServiceProvider extends ServiceProvider }); Route::group([ - 'as' => 'installer.', - 'prefix' => 'install', + 'as' => 'update.', + 'prefix' => 'update', 'middleware' => ['web'], 'namespace' => 'Modules\Installer\Http\Controllers' ], function () { diff --git a/modules/Installer/Resources/views/index-start.blade.php b/modules/Installer/Resources/views/install/index-start.blade.php similarity index 100% rename from modules/Installer/Resources/views/index-start.blade.php rename to modules/Installer/Resources/views/install/index-start.blade.php diff --git a/modules/Installer/Resources/views/steps/step1-requirements.blade.php b/modules/Installer/Resources/views/install/steps/step1-requirements.blade.php similarity index 100% rename from modules/Installer/Resources/views/steps/step1-requirements.blade.php rename to modules/Installer/Resources/views/install/steps/step1-requirements.blade.php diff --git a/modules/Installer/Resources/views/steps/step2-db.blade.php b/modules/Installer/Resources/views/install/steps/step2-db.blade.php similarity index 100% rename from modules/Installer/Resources/views/steps/step2-db.blade.php rename to modules/Installer/Resources/views/install/steps/step2-db.blade.php diff --git a/modules/Installer/Resources/views/steps/step2a-db_output.blade.php b/modules/Installer/Resources/views/install/steps/step2a-db_output.blade.php similarity index 100% rename from modules/Installer/Resources/views/steps/step2a-db_output.blade.php rename to modules/Installer/Resources/views/install/steps/step2a-db_output.blade.php diff --git a/modules/Installer/Resources/views/steps/step3-user.blade.php b/modules/Installer/Resources/views/install/steps/step3-user.blade.php similarity index 100% rename from modules/Installer/Resources/views/steps/step3-user.blade.php rename to modules/Installer/Resources/views/install/steps/step3-user.blade.php diff --git a/modules/Installer/Resources/views/steps/step3a-completed.blade.php b/modules/Installer/Resources/views/install/steps/step3a-completed.blade.php similarity index 100% rename from modules/Installer/Resources/views/steps/step3a-completed.blade.php rename to modules/Installer/Resources/views/install/steps/step3a-completed.blade.php diff --git a/modules/Installer/Resources/views/update/index-start.blade.php b/modules/Installer/Resources/views/update/index-start.blade.php new file mode 100644 index 00000000..f99964d4 --- /dev/null +++ b/modules/Installer/Resources/views/update/index-start.blade.php @@ -0,0 +1,12 @@ +@extends('installer::app') +@section('title', 'Update phpVMS') + +@section('content') +

phpvms updater

+

Press continue to check if there are any updates available.

+ {!! Form::open(['route' => 'update.step1', 'method' => 'post']) !!} +

+ {!! Form::submit('Start >>', ['class' => 'btn btn-success']) !!} +

+ {!! Form::close() !!} +@endsection diff --git a/modules/Installer/Resources/views/update/steps/step1-no-update.blade.php b/modules/Installer/Resources/views/update/steps/step1-no-update.blade.php new file mode 100644 index 00000000..64384d60 --- /dev/null +++ b/modules/Installer/Resources/views/update/steps/step1-no-update.blade.php @@ -0,0 +1,13 @@ +@extends('installer::app') +@section('title', 'Update phpVMS') + +@section('content') +

phpvms updater

+

It seems like you're up to date!

+ {!! Form::open(['route' => 'update.complete', 'method' => 'GET']) !!} + +

+ {!! Form::submit('Complete >>', ['class' => 'btn btn-success']) !!} +

+ {!! Form::close() !!} +@endsection diff --git a/modules/Installer/Resources/views/update/steps/step1-update-available.blade.php b/modules/Installer/Resources/views/update/steps/step1-update-available.blade.php new file mode 100644 index 00000000..171e500b --- /dev/null +++ b/modules/Installer/Resources/views/update/steps/step1-update-available.blade.php @@ -0,0 +1,12 @@ +@extends('installer::app') +@section('title', 'Update phpVMS') + +@section('content') +

phpvms updater

+

Updates have been found, click run to complete the update!.

+ {!! Form::open(['route' => 'update.run_migrations', 'method' => 'post']) !!} +

+ {!! Form::submit('Run >>', ['class' => 'btn btn-success']) !!} +

+ {!! Form::close() !!} +@endsection diff --git a/modules/Installer/Resources/views/update/steps/step2-migrations-done.blade.php b/modules/Installer/Resources/views/update/steps/step2-migrations-done.blade.php new file mode 100644 index 00000000..f003c882 --- /dev/null +++ b/modules/Installer/Resources/views/update/steps/step2-migrations-done.blade.php @@ -0,0 +1,18 @@ +@extends('installer::app') +@section('title', 'Update Completed') +@section('content') +
+ {!! Form::open(['route' => 'update.complete', 'method' => 'GET']) !!} + +
+        
+            {!! $console_output !!}
+        
+    
+ +

+ {!! Form::submit('Complete >>', ['class' => 'btn btn-success']) !!} +

+ {!! Form::close() !!} +
+@endsection diff --git a/modules/Installer/Services/DatabaseService.php b/modules/Installer/Services/DatabaseService.php index db575cf5..178eecd0 100644 --- a/modules/Installer/Services/DatabaseService.php +++ b/modules/Installer/Services/DatabaseService.php @@ -51,12 +51,6 @@ class DatabaseService { $output .= \Artisan::output(); } - \Artisan::call('migrate'); - $output .= trim(\Artisan::output()); - - \Artisan::call('db:seed'); - $output .= trim(\Artisan::output()); - return trim($output); } } diff --git a/modules/Installer/Services/MigrationService.php b/modules/Installer/Services/MigrationService.php new file mode 100644 index 00000000..e4d07d52 --- /dev/null +++ b/modules/Installer/Services/MigrationService.php @@ -0,0 +1,77 @@ +setConnection(config('database.default')); + return $m; + } + + /** + * Find all of the possible paths that migrations exist. + * Include looking in all of the modules Database/migrations directories + * @return array + */ + public function getMigrationPaths(): array + { + $paths = [ + 'core' => \App::databasePath() . '/migrations' + ]; + + $modules = Module::enabled(); + foreach ($modules as $module) { + $module_path = $module->getPath() . '/Database/migrations'; + if(file_exists($module_path)) { + $paths[$module->getName()] = $module_path; + } + } + + Log::info('Update - migration paths', $paths); + + return $paths; + } + + /** + * Return what migrations are available + */ + public function migrationsAvailable(): array + { + $migrator = $this->getMigrator(); + $migration_dirs = $this->getMigrationPaths(); + + $files = $migrator->getMigrationFiles(array_values($migration_dirs)); + $availMigrations = array_diff(array_keys($files), $migrator->getRepository()->getRan()); + + Log::info('Migrations available:', $availMigrations); + + return $availMigrations; + } + + /** + * Run all of the migrations that are available. Just call artisan since + * it looks into all of the module directories, etc + */ + public function runAllMigrations() + { + $output = ''; + + \Artisan::call('migrate'); + $output .= trim(\Artisan::output()); + + return $output; + } +} diff --git a/resources/stubs/modules/migration/add.stub b/resources/stubs/modules/migration/add.stub index 53ec1b32..b20294af 100644 --- a/resources/stubs/modules/migration/add.stub +++ b/resources/stubs/modules/migration/add.stub @@ -1,8 +1,8 @@