-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCsvSeeder.php
318 lines (270 loc) · 8.6 KB
/
CsvSeeder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Flynsarmy\CsvSeeder;
use App;
use Log;
use DB;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Database\Schema;
use Illuminate\Support\Facades\Hash;
/**
* Taken from http://laravelsnippets.com/snippets/seeding-database-with-csv-files-cleanly
* and modified to include insert chunking
*/
class CsvSeeder extends Seeder
{
/**
* DB table name
*
* @var string
*/
public string $table = '';
/**
* CSV filename
*
* @var string
*/
public string $filename = '';
/**
* DB connection to use. Leave empty for default connection
*/
public string $connection = '';
/**
* DB fields to be hashed before import, For example a password field.
*/
public array $hashable = ['password'];
/**
* An SQL INSERT query will execute every time this number of rows
* are read from the CSV. Without this, large INSERTS will silently
* fail.
*/
public int $insert_chunk_size = 50;
/**
* CSV delimiter (defaults to ,)
*/
public string $csv_delimiter = ',';
/**
* Number of rows to skip at the start of the CSV
*/
public int $offset_rows = 0;
/**
* Can be used to tell the import to trim any leading or trailing white space from the column;
*/
public bool $should_trim = false;
/**
* Add created_at and updated_at to rows
*/
public bool $timestamps = false;
/**
* created_at and updated_at values to be added to each row. Only used if
* $this->timestamps is true
*/
public string $created_at = '';
public string $updated_at = '';
/**
* The mapping of CSV to DB column. If not specified manually, the first
* row (after offset_rows) of your CSV will be read as your DB columns.
*
* Mappings take the form of csvColNumber => dbColName.
*
* IE to read the first, third and fourth columns of your CSV only, use:
* array(
* 0 => id,
* 2 => name,
* 3 => description,
* )
*/
public array $mapping = [];
/**
* Run DB seed
*/
public function run()
{
// Cache created_at and updated_at if we need to
if ($this->timestamps) {
if (!$this->created_at) {
$this->created_at = Carbon::now()->toIso8601String();
}
if (!$this->updated_at) {
$this->updated_at = Carbon::now()->toIso8601String();
}
}
$this->seedFromCSV($this->filename, $this->csv_delimiter);
}
/**
* Strip UTF-8 BOM characters from the start of a string
*
* @param string $text
* @return string String with BOM stripped
*/
public function stripUtf8Bom(string $text): string
{
$bom = pack('H*', 'EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
/**
* Opens a CSV file and returns it as a resource
*
* @param string $filename
* @return FALSE|resource
*/
public function openCSV(string $filename)
{
if (!file_exists($filename) || !is_readable($filename)) {
Log::error("CSV insert failed: CSV " . $filename . " does not exist or is not readable.");
return false;
}
// check if file is gzipped
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file_mime_type = finfo_file($finfo, $filename);
finfo_close($finfo);
$gzipped = strcmp($file_mime_type, "application/x-gzip") == 0;
$handle = $gzipped ? gzopen($filename, 'r') : fopen($filename, 'r');
return $handle;
}
/**
* Reads all rows of a given CSV and imports the data.
*
* @param string $filename
* @param string $deliminator
* @return bool Whether or not the import completed successfully.
*/
public function seedFromCSV(string $filename, string $deliminator = ","): bool
{
$handle = $this->openCSV($filename);
// CSV doesn't exist or couldn't be read from.
if ($handle === false) {
return false;
}
$success = true;
$row_count = 0;
$data = [];
$mapping = $this->mapping ?: [];
$offset = $this->offset_rows;
if ($mapping) {
$this->hashable = $this->removeUnusedHashColumns($mapping);
}
while (($row = fgetcsv($handle, 0, $deliminator)) !== false) {
// Offset the specified number of rows
while ($offset-- > 0) {
continue 2;
}
// No mapping specified - the first row will be used as the mapping
// ie it's a CSV title row. This row won't be inserted into the DB.
if (!$mapping) {
$mapping = $this->createMappingFromRow($row);
$this->hashable = $this->removeUnusedHashColumns($mapping);
continue;
}
$row = $this->readRow($row, $mapping);
// insert only non-empty rows from the csv file
if (empty($row)) {
continue;
}
$data[$row_count] = $row;
// Chunk size reached, insert
if (++$row_count == $this->insert_chunk_size) {
$success = $success && $this->insert($data);
$row_count = 0;
// clear the data array explicitly when it was inserted so
// that nothing is left, otherwise a leftover scenario can
// cause duplicate inserts
$data = [];
}
}
// Insert any leftover rows
//check if the data array explicitly if there are any values left to be inserted, if insert them
if (count($data)) {
$success = $success && $this->insert($data);
}
fclose($handle);
return $success;
}
/**
* Creates a CSV->DB column mapping from the given CSV row.
*
* @param array $row
* @return array
*/
public function createMappingFromRow(array $row): array
{
$mapping = $row;
$mapping[0] = $this->stripUtf8Bom($mapping[0]);
// skip csv columns that don't exist in the database
foreach ($mapping as $index => $fieldname) {
if (!DB::getSchemaBuilder()->hasColumn($this->table, $fieldname)) {
if (isset($mapping[$index])) {
unset($mapping[$index]);
}
}
}
return $mapping;
}
/**
* Removes fields from the hashable array that don't exist in our mapping.
*
* This function acts as a performance enhancement - we don't want
* to search for hashable columns on every row imported when we already
* know they don't exist.
*
* @param array $mapping
* @return array
*/
public function removeUnusedHashColumns(array $mapping)
{
$hashables = $this->hashable;
foreach ($hashables as $key => $field) {
if (!in_array($field, $mapping)) {
unset($hashables[$key]);
}
}
return $hashables;
}
/**
* Read a CSV row into a DB insertable array
*
* @param array $row A row of data to read
* @param array $mapping Array of csvCol => dbCol
* @return array
*/
public function readRow(array $row, array $mapping): array
{
$row_values = [];
foreach ($mapping as $csvCol => $dbCol) {
if (!isset($row[$csvCol]) || $row[$csvCol] === '') {
$row_values[$dbCol] = null;
} else {
$row_values[$dbCol] = $this->should_trim ? trim($row[$csvCol]) : $row[$csvCol];
}
}
if (!empty($this->hashable)) {
foreach ($this->hashable as $columnToHash) {
if (isset($row_values[$columnToHash])) {
$row_values[$columnToHash] = Hash::make($row_values[$columnToHash]);
}
}
}
if ($this->timestamps) {
$row_values['created_at'] = $this->created_at;
$row_values['updated_at'] = $this->updated_at;
}
return $row_values;
}
/**
* Seed a given set of data to the DB
*
* @param array $seedData
* @return bool TRUE on success else FALSE
*/
public function insert(array $seedData): bool
{
try {
DB::connection($this->connection)->table($this->table)->insert($seedData);
} catch (\Exception $e) {
Log::error("CSV insert failed: " . $e->getMessage() . " - CSV " . $this->filename);
return false;
}
return true;
}
}