Move the MigrationService to the main package
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Interfaces\Controller;
|
||||
use App\Models\User;
|
||||
use App\Repositories\AirlineRepository;
|
||||
use App\Services\AnalyticsService;
|
||||
use App\Services\Installer\MigrationService;
|
||||
use App\Services\UserService;
|
||||
use App\Support\Countries;
|
||||
use Illuminate\Database\QueryException;
|
||||
@@ -16,7 +17,6 @@ use Illuminate\Support\Facades\Validator;
|
||||
use Log;
|
||||
use Modules\Installer\Services\ConfigService;
|
||||
use Modules\Installer\Services\DatabaseService;
|
||||
use Modules\Installer\Services\MigrationService;
|
||||
use Modules\Installer\Services\RequirementsService;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
namespace Modules\Installer\Http\Controllers;
|
||||
|
||||
use App\Interfaces\Controller;
|
||||
use App\Services\Installer\MigrationService;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Modules\Installer\Services\MigrationService;
|
||||
|
||||
|
||||
/**
|
||||
* Class UpdaterController
|
||||
@@ -36,6 +37,10 @@ class UpdaterController extends Controller
|
||||
/**
|
||||
* Step 1. Check if there's an update available. Check if there
|
||||
* are any unrun migrations
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function step1(Request $request)
|
||||
{
|
||||
@@ -49,22 +54,23 @@ class UpdaterController extends Controller
|
||||
|
||||
/**
|
||||
* 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());
|
||||
|
||||
// Resync all of the settings
|
||||
$this->migrationSvc->updateAllSettings();
|
||||
|
||||
$migrations = $this->migrationSvc->migrationsAvailable();
|
||||
if(\count($migrations) === 0) {
|
||||
$this->migrationSvc->syncAllSeeds();
|
||||
return view('installer::update/steps/step3-update-complete');
|
||||
}
|
||||
|
||||
$output = $this->migrationSvc->runAllMigrations();
|
||||
$this->migrationSvc->syncAllSeeds();
|
||||
|
||||
return view('installer::update/steps/step2-migrations-done', [
|
||||
'console_output' => $output,
|
||||
@@ -73,7 +79,9 @@ class UpdaterController extends Controller
|
||||
|
||||
/**
|
||||
* Final step
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function complete(Request $request)
|
||||
|
||||
@@ -1,196 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services;
|
||||
|
||||
use App\Interfaces\Service;
|
||||
use App\Models\Setting;
|
||||
use DB;
|
||||
use Log;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* Class MigrationsService
|
||||
* @package Modules\Installer\Services
|
||||
*/
|
||||
class MigrationService extends Service
|
||||
{
|
||||
private $counters = [];
|
||||
private $offsets = [];
|
||||
|
||||
protected function getMigrator()
|
||||
{
|
||||
$m = app('migrator');
|
||||
$m->setConnection(config('database.default'));
|
||||
return $m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all of the settings and sync them with the settings.yml file
|
||||
*/
|
||||
public function updateAllSettings(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $setting) {
|
||||
if ($setting['key'] === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addSetting($setting['key'], $setting);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $attrs
|
||||
*/
|
||||
public function addSetting($key, $attrs): void
|
||||
{
|
||||
$id = Setting::formatKey($key);
|
||||
$group = $attrs['group'];
|
||||
$order = $this->getNextOrderNumber($group);
|
||||
|
||||
$attrs = array_merge(
|
||||
[
|
||||
'id' => $id,
|
||||
'key' => $key,
|
||||
'offset' => $this->offsets[$group],
|
||||
'order' => $order,
|
||||
'name' => '',
|
||||
'group' => $group,
|
||||
'value' => '',
|
||||
'default' => $attrs['value'],
|
||||
'options' => '',
|
||||
'type' => 'hidden',
|
||||
'description' => '',
|
||||
],
|
||||
$attrs
|
||||
);
|
||||
|
||||
$count = DB::table('settings')->where('id', $id)->count('id');
|
||||
if ($count === 0) {
|
||||
DB::table('settings')->insert($attrs);
|
||||
} else {
|
||||
unset($attrs['value']); // Don't overwrite this
|
||||
DB::table('settings')
|
||||
->where('id', $id)
|
||||
->update($attrs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically figure out the offset and the start number for a group.
|
||||
* This way we don't need to mess with how to order things
|
||||
* When calling getNextOrderNumber(users) 31, will be returned, then 32, and so on
|
||||
*
|
||||
* @param $name
|
||||
* @param null $offset
|
||||
* @param int $start_offset
|
||||
*/
|
||||
private function addCounterGroup($name, $offset = null, $start_offset = 0): void
|
||||
{
|
||||
if ($offset === null) {
|
||||
$group = DB::table('settings')
|
||||
->where('group', $name)
|
||||
->first();
|
||||
|
||||
if ($group === null) {
|
||||
$offset = (int)DB::table('settings')->max('offset');
|
||||
if ($offset === null) {
|
||||
$offset = 0;
|
||||
$start_offset = 1;
|
||||
} else {
|
||||
$offset += 100;
|
||||
$start_offset = $offset + 1;
|
||||
}
|
||||
} else {
|
||||
// Now find the number to start from
|
||||
$start_offset = (int)DB::table('settings')->where('group', $name)->max('order');
|
||||
if ($start_offset === null) {
|
||||
$start_offset = $offset + 1;
|
||||
} else {
|
||||
$start_offset++;
|
||||
}
|
||||
|
||||
$offset = $group->offset;
|
||||
}
|
||||
}
|
||||
|
||||
$this->counters[$name] = $start_offset;
|
||||
$this->offsets[$name] = $offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next increment number from a group
|
||||
*
|
||||
* @param $group
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function getNextOrderNumber($group): int
|
||||
{
|
||||
if (!\in_array($group, $this->counters, true)) {
|
||||
$this->addCounterGroup($group);
|
||||
}
|
||||
|
||||
$idx = $this->counters[$group];
|
||||
$this->counters[$group]++;
|
||||
|
||||
return $idx;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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::allEnabled();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user