Files
investbrain/database/seeders/MarketDataSeeder.php
T

84 lines
2.2 KiB
PHP
Raw Normal View History

2024-08-24 22:19:40 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-24 22:19:40 -05:00
namespace Database\Seeders;
2025-01-28 17:14:49 -06:00
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
2024-08-24 22:19:40 -05:00
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class MarketDataSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Run the database seeds.
*/
public function run(): void
2025-01-28 17:14:49 -06:00
{
2024-08-24 22:19:40 -05:00
$chunkSize = 500;
// Path to the CSV file
$csvFilePath = realpath(__DIR__.'/market_data_seed.csv');
2024-08-24 22:19:40 -05:00
// Open the file in read mode
if (($handle = fopen($csvFilePath, 'r')) !== false) {
$header = null;
$rows = [];
$rowCount = 0;
while (($row = fgetcsv($handle, 0, ',')) !== false) {
2025-01-28 17:14:49 -06:00
if (! $header) {
2024-08-24 22:19:40 -05:00
// header must be the first row
$header = $row;
} else {
try {
$data = array_combine($header, $row);
$rows[] = [
2025-01-28 17:14:49 -06:00
'symbol' => $data['symbol'],
'name' => $data['name'],
2024-08-24 22:19:40 -05:00
'meta_data' => json_encode([
'country' => $data['country'],
'first_trade_year' => $data['first_trade_year'],
'sector' => $data['sector'],
'industry' => $data['industry'],
2025-01-28 17:14:49 -06:00
]),
2024-08-24 22:19:40 -05:00
];
$rowCount++;
if ($rowCount % $chunkSize == 0) {
DB::table('market_data')->insertOrIgnore($rows);
2025-01-28 17:14:49 -06:00
$rows = [];
2024-08-24 22:19:40 -05:00
}
} catch (\Throwable $e) {
2025-01-28 17:14:49 -06:00
2025-03-07 17:27:08 -06:00
throw new \Exception('Error: '.substr($e->getMessage(), 0, 500));
2024-08-24 22:19:40 -05:00
}
}
}
// final clean up
2025-01-28 17:14:49 -06:00
if (! empty($rows)) {
2024-08-24 22:19:40 -05:00
DB::table('market_data')->insertOrIgnore($rows);
}
// Close the CSV file
fclose($handle);
echo "Imported $rowCount market data items successfully!\n";
2024-08-24 22:19:40 -05:00
} else {
echo "Failed to open the CSV.\n";
}
}
}