#34 Generate UUID on seeder/models

This commit is contained in:
Nabeel Shahzad
2017-06-24 13:20:24 -05:00
parent 2aa7cc66d5
commit ba57cef0a7
9 changed files with 103 additions and 14 deletions

View File

@@ -3,25 +3,45 @@
namespace App\Services;
use Carbon\Carbon;
use Webpatser\Uuid\Uuid;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Facades\DB;
class DatabaseService extends BaseService {
class DatabaseService extends BaseService
{
protected $uuid_tables = [
'flights',
'users',
];
protected function time(): string
{
return Carbon::now('UTC')->format('Y-m-d H:i:s');
}
public function seed_from_yaml($yaml_file)
public function seed_from_yaml_file($yaml_file)
{
$yml = file_get_contents($yaml_file);
$this->seed_from_yaml($yml);
}
public function seed_from_yaml($yml)
{
$time_fields = ['created_at', 'updated_at'];
$yml = Yaml::parse(file_get_contents($yaml_file));
$yml = Yaml::parse($yml);
foreach ($yml as $table => $rows) {
foreach ($rows as $row) {
# 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;
}
}
# encrypt any password fields
if(array_key_exists('password', $row)) {
$row['password'] = bcrypt($row['password']);