2018-03-20 09:50:40 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Support\Database;
|
2020-10-22 01:28:54 +08:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-02-09 02:29:34 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-10-12 04:06:09 +08:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-04-07 06:21:10 +08:00
|
|
|
|
2018-03-20 10:01:32 +08:00
|
|
|
/**
|
|
|
|
* Class Migration
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
abstract class Migration extends \Illuminate\Database\Migrations\Migration
|
|
|
|
{
|
2018-04-07 06:21:10 +08:00
|
|
|
/**
|
|
|
|
* At a minimum, this function needs to be implemented
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function up();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A method to reverse a migration doesn't need to be made
|
|
|
|
*/
|
|
|
|
public function down()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:28:54 +08:00
|
|
|
/**
|
|
|
|
* Add a module and enable it
|
|
|
|
*
|
|
|
|
* @param array $attrs
|
|
|
|
*/
|
|
|
|
public function addModule(array $attrs)
|
|
|
|
{
|
|
|
|
$module = array_merge([
|
|
|
|
'enabled' => true,
|
|
|
|
'created_at' => DB::raw('NOW()'),
|
|
|
|
'updated_at' => DB::raw('NOW()'),
|
|
|
|
], $attrs);
|
|
|
|
|
|
|
|
try {
|
|
|
|
DB::table('modules')->insert($module);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// setting already exists, just ignore it
|
|
|
|
if ($e->getCode() === 23000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
/**
|
|
|
|
* Seed a YAML file into the database
|
|
|
|
*
|
|
|
|
* @param string $file Full path to yml file to seed
|
|
|
|
*/
|
|
|
|
public function seedFile($file): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$path = base_path($file);
|
|
|
|
Database::seed_from_yaml_file($path, false);
|
2020-10-22 01:28:54 +08:00
|
|
|
} catch (Exception $e) {
|
2020-02-09 02:29:34 +08:00
|
|
|
Log::error('Unable to load '.$file.' file');
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 06:21:10 +08:00
|
|
|
/**
|
|
|
|
* Add rows to a table
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-07 06:21:10 +08:00
|
|
|
* @param $table
|
|
|
|
* @param $rows
|
|
|
|
*/
|
|
|
|
public function addData($table, $rows)
|
|
|
|
{
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
try {
|
|
|
|
DB::table($table)->insert($row);
|
2020-10-22 01:28:54 +08:00
|
|
|
} catch (Exception $e) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// setting already exists, just ignore it
|
2018-04-07 06:21:10 +08:00
|
|
|
if ($e->getCode() === 23000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 04:06:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an award from the migrations (for example, if you're adding an award module)
|
|
|
|
*
|
|
|
|
* @param array $award See \App\Models\Awardv
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
*/
|
|
|
|
public function addAward(array $award)
|
|
|
|
{
|
|
|
|
$validator = Validator::make($award, \App\Models\Award::$rules);
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
$awardModel = new \App\Models\Award($award);
|
|
|
|
$awardModel->save();
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
}
|