Skip to content

Commit

Permalink
Merge pull request #37 from DanAtFh/import-non-default-connection
Browse files Browse the repository at this point in the history
fix issue where data did not seed on non-default connection
  • Loading branch information
Flynsarmy authored Feb 9, 2021
2 parents b328ae1 + 4f50c9d commit 2803da7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CsvSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function createMappingFromRow(array $row): array

// skip csv columns that don't exist in the database
foreach ($mapping as $index => $fieldname) {
if (!DB::getSchemaBuilder()->hasColumn($this->table, $fieldname)) {
if (!DB::connection($this->connection)->getSchemaBuilder()->hasColumn($this->table, $fieldname)) {
if (isset($mapping[$index])) {
unset($mapping[$index]);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);
$app['config']->set('database.connections.csvSeederTest2', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

/** @test */
Expand Down Expand Up @@ -436,4 +441,34 @@ public function it_offsets()
'age' => 54
]);
}
/** @test */
public function it_imports_with_non_default_connection()
{
$seeder = new \Flynsarmy\CsvSeeder\CsvSeeder();
$seeder->table = 'tests_users2';
$seeder->filename = __DIR__ . '/csvs/users.csv';
$seeder->connection = 'csvSeederTest2';
$seeder->hashable = [];
$seeder->run();

// Make sure the rows imported
$this->assertDatabaseHas('tests_users2', [
'id' => 1,
'first_name' => 'Abe',
'last_name' => 'Abeson',
'email' => '[email protected]',
'age' => 50,
'created_at' => null,
'updated_at' => null,
], 'csvSeederTest2');
$this->assertDatabaseHas('tests_users2', [
'id' => 3,
'first_name' => 'Charly',
'last_name' => 'Charlyson',
'email' => '[email protected]',
'age' => 52,
'created_at' => null,
'updated_at' => null,
], 'csvSeederTest2');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;

class CreateSecondTestsUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('csvSeederTest2')->create('tests_users2', function ($table) {
$table->increments('id');
$table->string('first_name')->default('');
$table->string('last_name')->default('');
$table->string('email')->default('');
$table->string('password')->default('');
$table->string('address')->default('');
$table->integer('age')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('csvSeederTest2')->drop('tests_users2');
}
}

0 comments on commit 2803da7

Please sign in to comment.