From d5fba47dc62ca08fc014454efbcd44e3ffd23d65 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 6 Apr 2018 17:21:10 -0500 Subject: [PATCH] Move migration base class entirely into the interface --- ...017_06_07_014930_create_settings_table.php | 2 +- .../2017_06_08_0000_create_users_table.php | 2 +- ...17_06_08_0001_roles_permissions_tables.php | 2 +- ...6_08_0005_create_password_resets_table.php | 2 +- .../2017_06_08_0006_create_sessions_table.php | 2 +- ...017_06_08_191703_create_airlines_table.php | 2 +- ...17_06_09_010621_create_aircrafts_table.php | 2 +- .../2017_06_10_040335_create_fares_table.php | 2 +- ...017_06_11_135707_create_airports_table.php | 2 +- ...2017_06_17_214650_create_flight_tables.php | 2 +- .../2017_06_21_165410_create_ranks_table.php | 2 +- ...17_06_23_011011_create_subfleet_tables.php | 2 +- .../2017_06_28_195426_create_pirep_tables.php | 2 +- .../2017_12_12_174519_create_bids_table.php | 2 +- .../2017_12_14_225241_create_jobs_table.php | 2 +- ..._12_14_225337_create_failed_jobs_table.php | 2 +- ...017_12_20_004147_create_navdata_tables.php | 2 +- .../2017_12_20_005147_create_acars_tables.php | 2 +- .../2018_01_03_014930_create_stats_table.php | 2 +- .../2018_01_08_142204_create_news_table.php | 2 +- .../2018_01_28_180522_create_awards_table.php | 2 +- ...018_02_26_185121_create_expenses_table.php | 2 +- ...1807_create_journal_transactions_table.php | 2 +- ...018_02_28_231813_create_journals_table.php | 2 +- ...2018_02_28_232438_create_ledgers_table.php | 2 +- .../2018_04_01_193443_create_files_table.php | 2 +- app/Interfaces/Migration.php | 135 +++++++++++++++++ app/Models/Migrations/Migration.php | 137 ------------------ 28 files changed, 161 insertions(+), 163 deletions(-) delete mode 100644 app/Models/Migrations/Migration.php diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index b873c3e6..83059725 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -1,6 +1,6 @@ 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; + } + } + } + } } diff --git a/app/Models/Migrations/Migration.php b/app/Models/Migrations/Migration.php deleted file mode 100644 index 108a1dac..00000000 --- a/app/Models/Migrations/Migration.php +++ /dev/null @@ -1,137 +0,0 @@ -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; - } - } - } - } -}