Issue/329 refactor seeding (#337)
* Fix Contracts class names * Refactoring of the file seeds so it's not a mess * StyleCI fixes
This commit is contained in:
@@ -12,9 +12,9 @@ use App\Repositories\AcarsRepository;
|
||||
use App\Repositories\NavdataRepository;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use League\Geotools\Coordinate\Coordinate;
|
||||
use League\Geotools\Geotools;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class GeoService
|
||||
@@ -109,6 +109,7 @@ class GeoService extends Service
|
||||
continue;
|
||||
}
|
||||
|
||||
$point = null;
|
||||
$size = \count($points);
|
||||
|
||||
if ($size === 0) {
|
||||
@@ -150,6 +151,10 @@ class GeoService extends Service
|
||||
}
|
||||
}
|
||||
|
||||
if ($point === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$coords[] = $point;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,17 +3,13 @@
|
||||
namespace App\Services\Installer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Models\Setting;
|
||||
use DB;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class MigrationService extends Service
|
||||
{
|
||||
private $counters = [];
|
||||
private $offsets = [];
|
||||
|
||||
protected function getMigrator()
|
||||
{
|
||||
$m = app('migrator');
|
||||
@@ -21,144 +17,6 @@ class MigrationService extends Service
|
||||
return $m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncronize all of the seed files, run this after the migrations
|
||||
* and on first install.
|
||||
*/
|
||||
public function syncAllSeeds(): void
|
||||
{
|
||||
$this->syncAllSettings();
|
||||
$this->syncAllPermissions();
|
||||
}
|
||||
|
||||
public function syncAllSettings(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $setting) {
|
||||
if (\trim($setting['key']) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addSetting($setting['key'], $setting);
|
||||
}
|
||||
}
|
||||
|
||||
public function syncAllPermissions(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/permissions.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $perm) {
|
||||
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
|
||||
if ($count === 0) {
|
||||
DB::table('permissions')->insert($perm);
|
||||
} else {
|
||||
DB::table('settings')
|
||||
->where('name', $perm['name'])
|
||||
->update($perm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
@@ -168,7 +26,7 @@ class MigrationService extends Service
|
||||
public function getMigrationPaths(): array
|
||||
{
|
||||
$paths = [
|
||||
'core' => \App::databasePath().'/migrations',
|
||||
'core' => App::databasePath().'/migrations',
|
||||
];
|
||||
|
||||
$modules = Module::allEnabled();
|
||||
@@ -208,8 +66,8 @@ class MigrationService extends Service
|
||||
{
|
||||
$output = '';
|
||||
|
||||
\Artisan::call('migrate');
|
||||
$output .= trim(\Artisan::output());
|
||||
Artisan::call('migrate');
|
||||
$output .= trim(Artisan::output());
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
197
app/Services/Installer/SeederService.php
Normal file
197
app/Services/Installer/SeederService.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Installer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Models\Setting;
|
||||
use App\Services\DatabaseService;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class SeederService extends Service
|
||||
{
|
||||
private $databaseSvc;
|
||||
|
||||
private $counters = [];
|
||||
private $offsets = [];
|
||||
|
||||
private static $seed_mapper = [
|
||||
'local' => 'dev',
|
||||
'qa' => 'dev',
|
||||
'staging' => 'dev',
|
||||
];
|
||||
|
||||
public function __construct(DatabaseService $databaseSvc)
|
||||
{
|
||||
$this->databaseSvc = $databaseSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncronize all of the seed files, run this after the migrations
|
||||
* and on first install.
|
||||
*/
|
||||
public function syncAllSeeds(): void
|
||||
{
|
||||
$this->syncAllYamlFileSeeds();
|
||||
$this->syncAllSettings();
|
||||
$this->syncAllPermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all of the YAML files from disk and seed them
|
||||
*/
|
||||
public function syncAllYamlFileSeeds(): void
|
||||
{
|
||||
Log::info('Running seeder');
|
||||
$env = App::environment();
|
||||
if (array_key_exists($env, self::$seed_mapper)) {
|
||||
$env = self::$seed_mapper[$env];
|
||||
}
|
||||
|
||||
// Gather all of the files to seed
|
||||
collect()
|
||||
->concat(Storage::disk('seeds')->files($env))
|
||||
->map(function ($file) {
|
||||
return database_path('seeds/'.$file);
|
||||
})
|
||||
->filter(function ($file) {
|
||||
$info = pathinfo($file);
|
||||
return $info['extension'] === 'yml';
|
||||
})
|
||||
->each(function ($file) {
|
||||
Log::info('Seeding .'.$file);
|
||||
$this->databaseSvc->seed_from_yaml_file($file);
|
||||
});
|
||||
}
|
||||
|
||||
public function syncAllSettings(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $setting) {
|
||||
if (\trim($setting['key']) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addSetting($setting['key'], $setting);
|
||||
}
|
||||
}
|
||||
|
||||
public function syncAllPermissions(): void
|
||||
{
|
||||
$data = file_get_contents(database_path('/seeds/permissions.yml'));
|
||||
$yml = Yaml::parse($data);
|
||||
foreach ($yml as $perm) {
|
||||
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
|
||||
if ($count === 0) {
|
||||
DB::table('permissions')->insert($perm);
|
||||
} else {
|
||||
DB::table('permissions')
|
||||
->where('name', $perm['name'])
|
||||
->update($perm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use App\Events\PirepRejected;
|
||||
use App\Events\UserStateChanged;
|
||||
use App\Events\UserStatsChanged;
|
||||
use App\Models\Acars;
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Bid;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\PirepSource;
|
||||
@@ -338,7 +339,7 @@ class PirepService extends Service
|
||||
Log::info('PIREP '.$pirep->id.' state change to ACCEPTED');
|
||||
|
||||
// Update the aircraft
|
||||
$pirep->aircraft->flight_time += $pirep->flight_time;
|
||||
$pirep->aircraft->flight_time = $pirep->aircraft->flight_time + $pirep->flight_time;
|
||||
$pirep->aircraft->airport_id = $pirep->arr_airport_id;
|
||||
$pirep->aircraft->landing_time = $pirep->updated_at;
|
||||
$pirep->aircraft->save();
|
||||
|
||||
Reference in New Issue
Block a user