Skip to content

Commit 2803da7

Browse files
authoredFeb 9, 2021
Merge pull request #37 from DanAtFh/import-non-default-connection
fix issue where data did not seed on non-default connection
2 parents b328ae1 + 4f50c9d commit 2803da7

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
 

Diff for: ‎src/CsvSeeder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function createMappingFromRow(array $row): array
232232

233233
// skip csv columns that don't exist in the database
234234
foreach ($mapping as $index => $fieldname) {
235-
if (!DB::getSchemaBuilder()->hasColumn($this->table, $fieldname)) {
235+
if (!DB::connection($this->connection)->getSchemaBuilder()->hasColumn($this->table, $fieldname)) {
236236
if (isset($mapping[$index])) {
237237
unset($mapping[$index]);
238238
}

Diff for: ‎tests/CsvTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ protected function getEnvironmentSetUp($app)
3636
'database' => ':memory:',
3737
'prefix' => '',
3838
]);
39+
$app['config']->set('database.connections.csvSeederTest2', [
40+
'driver' => 'sqlite',
41+
'database' => ':memory:',
42+
'prefix' => '',
43+
]);
3944
}
4045

4146
/** @test */
@@ -436,4 +441,34 @@ public function it_offsets()
436441
'age' => 54
437442
]);
438443
}
444+
/** @test */
445+
public function it_imports_with_non_default_connection()
446+
{
447+
$seeder = new \Flynsarmy\CsvSeeder\CsvSeeder();
448+
$seeder->table = 'tests_users2';
449+
$seeder->filename = __DIR__ . '/csvs/users.csv';
450+
$seeder->connection = 'csvSeederTest2';
451+
$seeder->hashable = [];
452+
$seeder->run();
453+
454+
// Make sure the rows imported
455+
$this->assertDatabaseHas('tests_users2', [
456+
'id' => 1,
457+
'first_name' => 'Abe',
458+
'last_name' => 'Abeson',
459+
'email' => 'abe.abeson@foo.com',
460+
'age' => 50,
461+
'created_at' => null,
462+
'updated_at' => null,
463+
], 'csvSeederTest2');
464+
$this->assertDatabaseHas('tests_users2', [
465+
'id' => 3,
466+
'first_name' => 'Charly',
467+
'last_name' => 'Charlyson',
468+
'email' => 'charly.charlyson@foo.com',
469+
'age' => 52,
470+
'created_at' => null,
471+
'updated_at' => null,
472+
], 'csvSeederTest2');
473+
}
439474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class CreateSecondTestsUsersTable extends Migration
6+
{
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
Schema::connection('csvSeederTest2')->create('tests_users2', function ($table) {
15+
$table->increments('id');
16+
$table->string('first_name')->default('');
17+
$table->string('last_name')->default('');
18+
$table->string('email')->default('');
19+
$table->string('password')->default('');
20+
$table->string('address')->default('');
21+
$table->integer('age')->default(0);
22+
$table->timestamps();
23+
});
24+
}
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::connection('csvSeederTest2')->drop('tests_users2');
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.