2018-06-21 00:18:30 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-08-05 20:27:53 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-08-27 00:40:04 +08:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2018-06-21 00:18:30 +08:00
|
|
|
|
|
|
|
class Database
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function time(): string
|
|
|
|
{
|
|
|
|
return Carbon::now('UTC');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $yaml_file
|
|
|
|
* @param bool $ignore_errors
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-06-21 00:18:30 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2018-06-21 00:18:30 +08:00
|
|
|
*/
|
|
|
|
public static function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
|
|
|
|
{
|
|
|
|
$yml = file_get_contents($yaml_file);
|
|
|
|
return static::seed_from_yaml($yml, $ignore_errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $yml
|
|
|
|
* @param bool $ignore_errors
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-06-21 00:18:30 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2018-06-21 00:18:30 +08:00
|
|
|
*/
|
|
|
|
public static function seed_from_yaml($yml, $ignore_errors = false): array
|
|
|
|
{
|
|
|
|
$imported = [];
|
|
|
|
$yml = Yaml::parse($yml);
|
2019-07-23 20:41:20 +08:00
|
|
|
if (empty($yml)) {
|
|
|
|
return $imported;
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
foreach ($yml as $table => $data) {
|
2018-06-21 00:18:30 +08:00
|
|
|
$imported[$table] = 0;
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
$id_column = 'id';
|
|
|
|
if (array_key_exists('id_column', $data)) {
|
|
|
|
$id_column = $data['id_column'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$ignore_on_update = [];
|
|
|
|
if (array_key_exists('ignore_on_update', $data)) {
|
|
|
|
$ignore_on_update = $data['ignore_on_update'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('data', $data)) {
|
|
|
|
$rows = $data['data'];
|
|
|
|
} else {
|
|
|
|
$rows = $data;
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:30 +08:00
|
|
|
foreach ($rows as $row) {
|
|
|
|
try {
|
2019-08-05 20:27:53 +08:00
|
|
|
static::insert_row($table, $row, $id_column, $ignore_on_update);
|
2018-06-21 00:18:30 +08:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
if ($ignore_errors) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$imported[$table]++;
|
2018-06-21 00:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $imported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-05 20:27:53 +08:00
|
|
|
* @param string $table
|
|
|
|
* @param array $row
|
|
|
|
* @param string $id_col The ID column to use for update/insert
|
|
|
|
* @param array $ignore_on_updates
|
|
|
|
* @param bool $ignore_errors
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-06-21 00:18:30 +08:00
|
|
|
*/
|
2019-08-05 20:27:53 +08:00
|
|
|
public static function insert_row(
|
|
|
|
$table,
|
|
|
|
$row,
|
|
|
|
$id_col = 'id',
|
|
|
|
$ignore_on_updates = [],
|
|
|
|
$ignore_errors = true
|
|
|
|
) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// encrypt any password fields
|
2018-06-21 00:18:30 +08:00
|
|
|
if (array_key_exists('password', $row)) {
|
|
|
|
$row['password'] = bcrypt($row['password']);
|
|
|
|
}
|
|
|
|
|
2019-07-23 20:41:20 +08:00
|
|
|
if (empty($row)) {
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// if any time fields are == to "now", then insert the right time
|
2018-06-21 00:18:30 +08:00
|
|
|
foreach ($row as $column => $value) {
|
|
|
|
if (strtolower($value) === 'now') {
|
|
|
|
$row[$column] = static::time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
$count = 0;
|
|
|
|
if (array_key_exists($id_col, $row)) {
|
|
|
|
$count = DB::table($table)->where($id_col, $row[$id_col])->count($id_col);
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:30 +08:00
|
|
|
try {
|
2019-08-05 20:27:53 +08:00
|
|
|
if ($count > 0) {
|
|
|
|
foreach ($ignore_on_updates as $ignore_column) {
|
|
|
|
if (array_key_exists($ignore_column, $row)) {
|
|
|
|
unset($row[$ignore_column]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table($table)
|
|
|
|
->where($id_col, $row[$id_col])
|
|
|
|
->update($row);
|
|
|
|
} else {
|
|
|
|
DB::table($table)->insert($row);
|
|
|
|
}
|
2018-06-21 00:18:30 +08:00
|
|
|
} catch (QueryException $e) {
|
2018-07-16 04:06:25 +08:00
|
|
|
Log::error($e);
|
2019-08-05 20:27:53 +08:00
|
|
|
if (!$ignore_errors) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2018-06-21 00:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
}
|