From ba57cef0a7fc7afa12d9e5f7c8e6a729697c65d2 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 24 Jun 2017 13:20:24 -0500 Subject: [PATCH] #34 Generate UUID on seeder/models --- .../Controllers/Admin/AircraftController.php | 7 --- app/Models/Flight.php | 3 ++ app/Models/User.php | 1 + app/Models/UuidTrait.php | 21 ++++++++ app/Services/DatabaseService.php | 28 +++++++++-- composer.json | 3 +- composer.lock | 49 ++++++++++++++++++- database/seeds/DatabaseSeeder.php | 2 +- phpvms.iml | 3 ++ 9 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 app/Models/UuidTrait.php diff --git a/app/Http/Controllers/Admin/AircraftController.php b/app/Http/Controllers/Admin/AircraftController.php index 31113fb4..87b0e305 100644 --- a/app/Http/Controllers/Admin/AircraftController.php +++ b/app/Http/Controllers/Admin/AircraftController.php @@ -53,11 +53,9 @@ class AircraftController extends BaseController public function store(CreateAircraftRequest $request) { $input = $request->all(); - $aircraft = $this->aircraftRepository->create($input); Flash::success('Aircraft saved successfully.'); - return redirect(route('admin.aircraft.index')); } @@ -87,7 +85,6 @@ class AircraftController extends BaseController if (empty($aircraft)) { Flash::error('Aircraft not found'); - return redirect(route('admin.aircraft.index')); } @@ -106,14 +103,12 @@ class AircraftController extends BaseController if (empty($aircraft)) { Flash::error('Aircraft not found'); - return redirect(route('admin.aircraft.index')); } $aircraft = $this->aircraftRepository->update($request->all(), $id); Flash::success('Aircraft updated successfully.'); - return redirect(route('admin.aircraft.index')); } @@ -126,14 +121,12 @@ class AircraftController extends BaseController if (empty($aircraft)) { Flash::error('Aircraft not found'); - return redirect(route('admin.aircraft.index')); } $this->aircraftRepository->delete($id); Flash::success('Aircraft deleted successfully.'); - return redirect(route('admin.aircraft.index')); } diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 2fd2bfa5..3bd643a8 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -11,7 +11,10 @@ use Eloquent as Model; */ class Flight extends Model { + use Uuids; + public $table = 'flights'; + public $incrementing = false; protected $dates = ['deleted_at']; diff --git a/app/Models/User.php b/app/Models/User.php index e91d5286..ea8681fa 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -31,6 +31,7 @@ use Illuminate\Contracts\Auth\CanResetPassword; */ class User extends Authenticatable { + use Uuids; use Notifiable; use EntrustUserTrait; diff --git a/app/Models/UuidTrait.php b/app/Models/UuidTrait.php new file mode 100644 index 00000000..e1be4515 --- /dev/null +++ b/app/Models/UuidTrait.php @@ -0,0 +1,21 @@ +getKeyName(); + if (empty($model->{$key})) { + $model->{$key} = Uuid::generate()->string; + } + #$model->{$model->getKeyName()} = Uuid::generate()->string; + }); + } +} diff --git a/app/Services/DatabaseService.php b/app/Services/DatabaseService.php index d76bbf87..85f73a19 100644 --- a/app/Services/DatabaseService.php +++ b/app/Services/DatabaseService.php @@ -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']); diff --git a/composer.json b/composer.json index a97630f2..02c516bf 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "symfony/yaml": "^3.3", "league/geotools": "@stable", "toin0u/geotools-laravel": "^1.0", - "anlutro/l4-settings": "^0.5.0" + "anlutro/l4-settings": "^0.5.0", + "webpatser/laravel-uuid": "^2.0" }, "require-dev": { "fzaninotto/faker": "~1.4", diff --git a/composer.lock b/composer.lock index 2a886bfe..4e9e5587 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "abd8dd8b12ea7f677c5bf93da12e0948", + "content-hash": "7525e3933b2ad8a1f88d01bb12263d86", "packages": [ { "name": "anlutro/l4-settings", @@ -3944,6 +3944,53 @@ ], "time": "2016-09-01T10:05:43+00:00" }, + { + "name": "webpatser/laravel-uuid", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/webpatser/laravel-uuid.git", + "reference": "6ed2705775e3edf066b90e1292f76f157ec00507" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webpatser/laravel-uuid/zipball/6ed2705775e3edf066b90e1292f76f157ec00507", + "reference": "6ed2705775e3edf066b90e1292f76f157ec00507", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "fzaninotto/faker": "1.5.*", + "phpunit/phpunit": "4.7.*" + }, + "suggest": { + "paragonie/random_compat": "A random_bytes Php 5.x polyfill." + }, + "type": "library", + "autoload": { + "psr-0": { + "Webpatser\\Uuid": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christoph Kempen", + "email": "christoph@downsized.nl" + } + ], + "description": "Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.", + "homepage": "https://github.com/webpatser/uuid", + "keywords": [ + "UUID RFC4122" + ], + "time": "2016-05-09T09:22:18+00:00" + }, { "name": "willdurand/geocoder", "version": "v3.3.2", diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 63655e56..a2a974d5 100755 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -18,7 +18,7 @@ class DatabaseSeeder extends Seeder } $svc = app('App\Services\DatabaseService'); - $svc->seed_from_yaml($path); + $svc->seed_from_yaml_file($path); } } diff --git a/phpvms.iml b/phpvms.iml index eaeb0baf..c752e417 100644 --- a/phpvms.iml +++ b/phpvms.iml @@ -13,6 +13,7 @@ + @@ -41,6 +42,7 @@ + @@ -56,6 +58,7 @@ +