diff --git a/Makefile b/Makefile index 1b3fe024..f05bcc01 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ clean: reset: clean @sqlite3 database/testing.sqlite "" - @php artisan migrate:refresh --seed --seeder DevelopmentSeeder + @php artisan migrate:refresh --seed db: sqlite3 database/testing.sqlite "" diff --git a/app/Services/DatabaseService.php b/app/Services/DatabaseService.php index 4fbbee9e..d76bbf87 100644 --- a/app/Services/DatabaseService.php +++ b/app/Services/DatabaseService.php @@ -2,8 +2,40 @@ namespace App\Services; +use Carbon\Carbon; +use Symfony\Component\Yaml\Yaml; +use Illuminate\Support\Facades\DB; + class DatabaseService extends BaseService { - + protected function time(): string + { + return Carbon::now('UTC')->format('Y-m-d H:i:s'); + } + + public function seed_from_yaml($yaml_file) + { + $time_fields = ['created_at', 'updated_at']; + + $yml = Yaml::parse(file_get_contents($yaml_file)); + foreach ($yml as $table => $rows) { + foreach ($rows as $row) { + + # 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 + foreach($time_fields as $tf) { + if(array_key_exists($tf, $row) && $row[$tf] === 'now') { + $row[$tf] = $this->time(); + } + } + + DB::table($table)->insert($row); + } + } + } } diff --git a/config/database.php b/config/database.php index 7e0abf1d..752657d9 100755 --- a/config/database.php +++ b/config/database.php @@ -2,18 +2,7 @@ return [ - /* - |-------------------------------------------------------------------------- - | PDO Fetch Style - |-------------------------------------------------------------------------- - | - | By default, database results will be returned as instances of the PHP - | stdClass object; however, you may desire to retrieve records in an - | array format for simplicity. Here you can tweak the fetch style. - | - */ - - 'fetch' => PDO::FETCH_OBJ, + 'fetch' => PDO::FETCH_ASSOC, 'default' => env('DB_CONNECTION', 'local'), 'connections' => [ diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 297cf6df..63655e56 100755 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -1,6 +1,5 @@ seed_from_yaml(App::environment()); - } - - protected function seed_from_yaml($env) - { + $env = App::environment(); $path = database_path('seeds/'.$env.'.yml'); - - $time_fields = ['created_at', 'updated_at']; - $curr_time = Carbon::now('UTC')->format('Y-m-d H:i:s'); - - $yml = Yaml::parse(file_get_contents($path)); - foreach ($yml as $table => $rows) { - foreach ($rows as $row) { - - # 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 - foreach ($time_fields as $tf) { - if (array_key_exists($tf, $row) && $row[$tf] === 'now') { - $row[$tf] = $curr_time; - } - } - - DB::table($table)->insert($row); - } + if(!file_exists($path)) { + return; } + + $svc = app('App\Services\DatabaseService'); + $svc->seed_from_yaml($path); } } diff --git a/database/seeds/DevelopmentSeeder.php b/database/seeds/DevelopmentSeeder.php deleted file mode 100644 index a7cb09b9..00000000 --- a/database/seeds/DevelopmentSeeder.php +++ /dev/null @@ -1,48 +0,0 @@ -seed_from_yaml(); - } - - protected function time(): string - { - return Carbon::now('UTC')->format('Y-m-d H:i:s'); - } - - protected function seed_from_yaml() - { - $time_fields = ['created_at', 'updated_at']; - - $yml = Yaml::parse(file_get_contents(database_path('seeds/dev.yml'))); - foreach ($yml as $table => $rows) { - foreach ($rows as $row) { - - # 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 - foreach($time_fields as $tf) { - if(array_key_exists($tf, $row) && $row[$tf] === 'now') { - $row[$tf] = $this->time(); - } - } - - DB::table($table)->insert($row); - } - } - } - -}