From ff8f29be246bb131b69af53017e474b4f706ef2f Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 3 May 2018 15:06:28 -0500 Subject: [PATCH] Update dev tools to add incremental inserts --- app/Console/Commands/DevCommands.php | 45 +++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index aec2ccb0..2e42cd3f 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -8,7 +8,7 @@ use App\Models\Airline; use App\Models\Pirep; use App\Models\User; use App\Services\AwardService; -use Artisan; +use App\Services\DatabaseService; use DB; use PDO; use Symfony\Component\Yaml\Yaml; @@ -21,6 +21,17 @@ class DevCommands extends Command { protected $signature = 'phpvms {cmd} {param?}'; protected $description = 'Developer commands'; + protected $dbSvc; + + /** + * DevCommands constructor. + * @param DatabaseService $dbSvc + */ + public function __construct(DatabaseService $dbSvc) + { + parent::__construct(); + $this->dbSvc = $dbSvc; + } /** * Run dev related commands @@ -40,6 +51,7 @@ class DevCommands extends Command 'clear-users' => 'clearUsers', 'compile-assets' => 'compileAssets', 'db-attrs' => 'dbAttrs', + 'manual-insert' => 'manualInsert', 'xml-to-yaml' => 'xmlToYaml', ]; @@ -170,4 +182,35 @@ class DevCommands extends Command file_put_contents(storage_path($file_name), Yaml::dump($yaml, 4, 2)); $this->info('Writing yaml to storage: '.$file_name); } + + /** + * Insert the rows from the file, manually advancing each row + */ + protected function manualInsert(): void + { + $file = $this->argument('param'); + $this->info('Reading '.$file); + + if (!file_exists($file)) { + $this->error('File '.$file.' doesn\'t exist'); + exit; + } + + $yml = Yaml::parse(file_get_contents($file)); + foreach ($yml as $table => $rows) { + + $this->info('Importing table ' . $table); + $this->info('Number of rows: ' . \count($rows)); + + foreach ($rows as $row) { + try { + $this->dbSvc->insert_row($table, $row); + } catch (\Exception $e) { + $this->error($e); + } + + $this->confirm('Insert next row?', true); + } + } + } }