#30 #19 change PDO mode and the seeder from yaml to a service for mass importing

This commit is contained in:
Nabeel Shahzad 2017-06-24 12:59:07 -05:00
parent 3736e530bf
commit 2aa7cc66d5
5 changed files with 41 additions and 90 deletions

View File

@ -25,7 +25,7 @@ clean:
reset: clean reset: clean
@sqlite3 database/testing.sqlite "" @sqlite3 database/testing.sqlite ""
@php artisan migrate:refresh --seed --seeder DevelopmentSeeder @php artisan migrate:refresh --seed
db: db:
sqlite3 database/testing.sqlite "" sqlite3 database/testing.sqlite ""

View File

@ -2,8 +2,40 @@
namespace App\Services; namespace App\Services;
use Carbon\Carbon;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Facades\DB;
class DatabaseService extends BaseService { 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);
}
}
}
} }

View File

@ -2,18 +2,7 @@
return [ return [
/* 'fetch' => PDO::FETCH_ASSOC,
|--------------------------------------------------------------------------
| 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,
'default' => env('DB_CONNECTION', 'local'), 'default' => env('DB_CONNECTION', 'local'),
'connections' => [ 'connections' => [

View File

@ -1,6 +1,5 @@
<?php <?php
use Carbon\Carbon;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
@ -12,35 +11,14 @@ class DatabaseSeeder extends Seeder
*/ */
public function run() public function run()
{ {
$this->seed_from_yaml(App::environment()); $env = App::environment();
}
protected function seed_from_yaml($env)
{
$path = database_path('seeds/'.$env.'.yml'); $path = database_path('seeds/'.$env.'.yml');
if(!file_exists($path)) {
$time_fields = ['created_at', 'updated_at']; return;
$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);
}
} }
$svc = app('App\Services\DatabaseService');
$svc->seed_from_yaml($path);
} }
} }

View File

@ -1,48 +0,0 @@
<?php
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class DevelopmentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->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);
}
}
}
}