diff --git a/app/Services/DatabaseService.php b/app/Services/DatabaseService.php index c3c4ae3e..c5b3d5db 100644 --- a/app/Services/DatabaseService.php +++ b/app/Services/DatabaseService.php @@ -3,9 +3,11 @@ namespace App\Services; use App\Interfaces\Service; +use App\Support\Database; use Carbon\Carbon; use Illuminate\Database\QueryException; use Illuminate\Support\Facades\DB; +use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\Yaml\Yaml; use Webpatser\Uuid\Uuid; @@ -42,8 +44,7 @@ class DatabaseService extends Service */ public function seed_from_yaml_file($yaml_file, $ignore_errors = false): array { - $yml = file_get_contents($yaml_file); - return $this->seed_from_yaml($yml, $ignore_errors); + return Database::seed_from_yaml_file($yaml_file, $ignore_errors); } /** @@ -54,27 +55,7 @@ class DatabaseService extends Service */ public function seed_from_yaml($yml, $ignore_errors = false): array { - $imported = []; - $yml = Yaml::parse($yml); - foreach ($yml as $table => $rows) { - $imported[$table] = 0; - - foreach ($rows as $row) { - try { - $row = $this->insert_row($table, $row); - } catch(QueryException $e) { - if ($ignore_errors) { - continue; - } - - throw $e; - } - - ++$imported[$table]; - } - } - - return $imported; + return Database::seed_from_yaml($yml, $ignore_errors); } /** @@ -92,24 +73,6 @@ class DatabaseService extends Service } } - # 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($row as $column => $value) { - if(strtolower($value) === 'now') { - $row[$column] = $this->time(); - } - } - - try { - DB::table($table)->insert($row); - } catch (QueryException $e) { - throw $e; - } - - return $row; + return Database::insert_row($table, $row); } } diff --git a/app/Support/Database.php b/app/Support/Database.php new file mode 100644 index 00000000..d23b7d79 --- /dev/null +++ b/app/Support/Database.php @@ -0,0 +1,95 @@ + $rows) { + $imported[$table] = 0; + + foreach ($rows as $row) { + try { + static::insert_row($table, $row); + } catch (QueryException $e) { + if ($ignore_errors) { + continue; + } + + throw $e; + } + + ++$imported[$table]; + } + } + + return $imported; + } + + /** + * @param $table + * @param $row + * @return mixed + * @throws \Exception + */ + public static function insert_row($table, $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 ($row as $column => $value) { + if (strtolower($value) === 'now') { + $row[$column] = static::time(); + } + } + + try { + DB::table($table)->insert($row); + } catch (QueryException $e) { + throw $e; + } + + return $row; + } +} diff --git a/config/modules.php b/config/modules.php index 1929a665..b44531a4 100644 --- a/config/modules.php +++ b/config/modules.php @@ -53,7 +53,7 @@ return [ 'config' => ['path' => 'Config', 'generate' => true], 'command' => ['path' => 'Console', 'generate' => true], 'migration' => ['path' => 'Database/migrations', 'generate' => true], - 'seeder' => ['path' => 'Database/seeders', 'generate' => true], + 'seeds' => ['path' => 'Database/seeds', 'generate' => true], 'factory' => ['path' => 'Database/factories', 'generate' => true], 'model' => ['path' => 'Models', 'generate' => true], 'controller' => ['path' => 'Http/Controllers', 'generate' => true],