Move migration base class entirely into the interface

This commit is contained in:
Nabeel Shahzad 2018-04-06 17:21:10 -05:00
parent 3e89a53fc1
commit d5fba47dc6
28 changed files with 161 additions and 163 deletions

View File

@ -1,6 +1,6 @@
<?php
use App\Models\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateSettingsTable extends Migration

View File

@ -1,6 +1,6 @@
<?php
use App\Models\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use App\Models\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class RolesPermissionsTables extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAirlinesTable extends Migration

View File

@ -1,8 +1,8 @@
<?php
use App\Interfaces\Migration;
use App\Models\Enums\AircraftState;
use App\Models\Enums\AircraftStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAircraftsTable extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateFaresTable extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAirportsTable extends Migration

View File

@ -1,7 +1,7 @@
<?php
use App\Interfaces\Migration;
use App\Models\Enums\FlightType;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateFlightTables extends Migration

View File

@ -1,6 +1,6 @@
<?php
use App\Models\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateRanksTable extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
/**

View File

@ -1,9 +1,9 @@
<?php
use App\Interfaces\Migration;
use App\Models\Enums\FlightType;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePirepTables extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use App\Models\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateStatsTable extends Migration

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,6 +1,6 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Interfaces\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@ -1,8 +1,8 @@
<?php
use App\Interfaces\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFilesTable extends Migration
{

View File

@ -2,11 +2,146 @@
namespace App\Interfaces;
use App\Models\Setting;
use DB;
/**
* Class Migration
* @package App\Interfaces
*/
abstract class Migration extends \Illuminate\Database\Migrations\Migration
{
protected $counters = [];
protected $offsets = [];
/**
* At a minimum, this function needs to be implemented
* @return mixed
*/
abstract public function up();
/**
* A method to reverse a migration doesn't need to be made
*/
public function down()
{
}
/**
* 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
*/
protected function addCounterGroup($name, $offset = null, $start_offset = 0)
{
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
*/
public function getNextOrderNumber($group): int
{
if (!\in_array($group, $this->counters, true)) {
$this->addCounterGroup($group);
}
$idx = $this->counters[$group];
++$this->counters[$group];
return $idx;
}
/**
* @param $key
* @param $attrs
*/
public function addSetting($key, $attrs)
{
$group = $attrs['group'];
$order = $this->getNextOrderNumber($group);
$attrs = array_merge([
'id' => Setting::formatKey($key),
'key' => $key,
'offset' => $this->offsets[$group],
'order' => $order,
'name' => '',
'group' => $group,
'value' => '',
'default' => '',
'options' => '',
'type' => 'hidden',
'description' => '',
], $attrs);
return $this->addData('settings', [$attrs]);
}
/**
* Update a setting
* @param $key
* @param $value
* @param array $attrs
*/
public function updateSetting($key, $value, array $attrs = [])
{
$attrs['value'] = $value;
DB::table('settings')
->where('id', Setting::formatKey($key))
->update($attrs);
}
/**
* Add rows to a table
* @param $table
* @param $rows
*/
public function addData($table, $rows)
{
foreach ($rows as $row) {
try {
DB::table($table)->insert($row);
} catch (\Exception $e) {
# setting already exists, just ignore it
if ($e->getCode() === 23000) {
continue;
}
}
}
}
}

View File

@ -1,137 +0,0 @@
<?php
/**
* Migration base class with some extra functionality
*/
namespace App\Models\Migrations;
use App\Interfaces\Migration as MigrationInterface;
use App\Models\Setting;
use DB;
/**
* Class Migration
* @package App\Models\Migrations
*/
class Migration extends MigrationInterface
{
protected $counters = [];
protected $offsets = [];
/**
* 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
*/
protected function addCounterGroup($name, $offset = null, $start_offset = 0)
{
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
*/
public function getNextOrderNumber($group): int
{
if (!\in_array($group, $this->counters, true)) {
$this->addCounterGroup($group);
}
$idx = $this->counters[$group];
++$this->counters[$group];
return $idx;
}
/**
* @param $key
* @param $attrs
*/
public function addSetting($key, $attrs)
{
$group = $attrs['group'];
$order = $this->getNextOrderNumber($group);
$attrs = array_merge([
'id' => Setting::formatKey($key),
'key' => $key,
'offset' => $this->offsets[$group],
'order' => $order,
'name' => '',
'group' => $group,
'value' => '',
'default' => '',
'options' => '',
'type' => 'hidden',
'description' => '',
], $attrs);
return $this->addData('settings', [$attrs]);
}
/**
* Update a setting
* @param $key
* @param $value
* @param array $attrs
*/
public function updateSetting($key, $value, array $attrs = [])
{
$attrs['value'] = $value;
DB::table('settings')
->where('id', Setting::formatKey($key))
->update($attrs);
}
/**
* Add rows to a table
* @param $table
* @param $rows
*/
public function addData($table, $rows)
{
foreach ($rows as $row) {
try {
DB::table($table)->insert($row);
} catch (\Exception $e) {
# setting already exists, just ignore it
if ($e->getCode() === 23000) {
continue;
}
}
}
}
}