2017-06-25 00:09:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-12-15 11:59:54 +08:00
|
|
|
use Log;
|
2017-06-25 01:59:07 +08:00
|
|
|
use Carbon\Carbon;
|
2017-12-15 11:59:54 +08:00
|
|
|
use Illuminate\Database\QueryException;
|
2017-06-25 02:20:24 +08:00
|
|
|
use Webpatser\Uuid\Uuid;
|
2017-06-25 01:59:07 +08:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
|
2017-06-25 02:20:24 +08:00
|
|
|
class DatabaseService extends BaseService
|
|
|
|
{
|
|
|
|
|
2017-06-25 02:30:13 +08:00
|
|
|
protected $time_fields = [
|
2017-06-25 02:23:30 +08:00
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
|
|
|
];
|
|
|
|
|
2017-06-25 02:30:13 +08:00
|
|
|
protected $uuid_tables = [
|
2017-06-25 02:20:24 +08:00
|
|
|
'flights',
|
2017-06-25 02:20:39 +08:00
|
|
|
'pireps',
|
2017-06-25 02:20:24 +08:00
|
|
|
];
|
2017-06-25 00:09:27 +08:00
|
|
|
|
2017-06-25 01:59:07 +08:00
|
|
|
protected function time(): string
|
|
|
|
{
|
|
|
|
return Carbon::now('UTC')->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2017-12-30 07:34:09 +08:00
|
|
|
/**
|
|
|
|
* @param $yaml_file
|
|
|
|
* @param bool $ignore_errors
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function seed_from_yaml_file($yaml_file, $ignore_errors=false): array
|
2017-06-25 01:59:07 +08:00
|
|
|
{
|
2017-06-25 02:20:24 +08:00
|
|
|
$yml = file_get_contents($yaml_file);
|
2017-12-30 07:34:09 +08:00
|
|
|
return $this->seed_from_yaml($yml, $ignore_errors);
|
2017-06-25 02:20:24 +08:00
|
|
|
}
|
2017-06-25 01:59:07 +08:00
|
|
|
|
2017-12-30 07:34:09 +08:00
|
|
|
/**
|
|
|
|
* @param $yml
|
|
|
|
* @param bool $ignore_errors
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function seed_from_yaml($yml, $ignore_errors=false): array
|
2017-06-25 02:20:24 +08:00
|
|
|
{
|
2017-12-30 07:34:09 +08:00
|
|
|
$imported = [];
|
2017-06-25 02:20:24 +08:00
|
|
|
$yml = Yaml::parse($yml);
|
2017-06-25 01:59:07 +08:00
|
|
|
foreach ($yml as $table => $rows) {
|
2017-12-30 07:34:09 +08:00
|
|
|
|
|
|
|
$imported[$table] = 0;
|
|
|
|
|
2017-06-25 01:59:07 +08:00
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
2017-06-25 02:20:24 +08:00
|
|
|
# see if this table uses a UUID as the PK
|
|
|
|
# if no ID is specified
|
|
|
|
if(in_array($table, $this->uuid_tables)) {
|
|
|
|
if(!array_key_exists('id', $row)) {
|
|
|
|
$row['id'] = Uuid::generate()->string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 01:59:07 +08:00
|
|
|
# encrypt any password fields
|
|
|
|
if(array_key_exists('password', $row)) {
|
|
|
|
$row['password'] = bcrypt($row['password']);
|
|
|
|
}
|
|
|
|
|
|
|
|
# if any time fields are == to "now", then insert the right time
|
2017-06-25 02:23:30 +08:00
|
|
|
foreach($this->time_fields as $tf) {
|
2017-12-24 01:58:17 +08:00
|
|
|
if(array_key_exists($tf, $row) && strtolower($row[$tf]) === 'now') {
|
2017-06-25 01:59:07 +08:00
|
|
|
$row[$tf] = $this->time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 07:34:09 +08:00
|
|
|
try {
|
2017-12-15 11:59:54 +08:00
|
|
|
DB::table($table)->insert($row);
|
2017-12-30 07:34:09 +08:00
|
|
|
++$imported[$table];
|
|
|
|
} catch(QueryException $e) {
|
|
|
|
if($ignore_errors) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2017-06-25 01:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-30 07:34:09 +08:00
|
|
|
|
|
|
|
return $imported;
|
2017-06-25 01:59:07 +08:00
|
|
|
}
|
2017-06-25 00:09:27 +08:00
|
|
|
}
|