Skip to content

Commit 22d65a0

Browse files
committed
Use model connection for load data query
1 parent fd2753d commit 22d65a0

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/Builder/Builder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function load(): bool
4444
{
4545
$query = $this->compile();
4646

47-
$connection = $this->databaseManager->connection($this->connection);
47+
$connection = $this->databaseManager->connection($this->getConnectionName());
4848
return $connection->statement($query->getSql(), $query->getBindings());
4949
}
5050

@@ -53,4 +53,9 @@ public function connection(?string $name = null): self
5353
$this->connection = $name;
5454
return $this;
5555
}
56+
57+
public function getConnectionName(): ?string
58+
{
59+
return $this->connection;
60+
}
5661
}

src/Laravel/Traits/LoadsFiles.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public static function loadFileBuilder(string $file, ?bool $local = null): Build
2121
$builder = app(Builder::class);
2222

2323
$model = app(static::class);
24-
$builder->file($file, $local)->into($model->getTable());
24+
25+
$builder->connection($model->getConnectionName())
26+
->file($file, $local)
27+
->into($model->getTable());
2528

2629
$model->loadFileOptions($builder);
2730

@@ -33,5 +36,7 @@ public function loadFileOptions(Builder $builder): void
3336
//
3437
}
3538

39+
abstract public function getConnectionName();
40+
3641
abstract public function getTable();
3742
}

tests/Feature/LoadsFilesTraitTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ public function testGetBuilderWithOptions(): void
2222
$this->assertTrue($builder->isReplace());
2323
}
2424

25-
public function testDefaultLoadFileOptions()
25+
public function testDefaultLoadFileOptions(): void
2626
{
2727
$builder = TestUserNoOptions::loadFileBuilder('/path/to/test_users.csv', true);
2828

2929
$this->assertFalse($builder->isReplace());
3030
}
3131

32+
public function testLoadFileIsUsingModelConnection(): void
33+
{
34+
$builder = TestUser::loadFileBuilder('/path/to/test_users.csv', true);
35+
36+
$this->assertSame('test-connection', $builder->getConnectionName());
37+
}
38+
39+
public function testLoadFileIsUsingModelConnectionNull(): void
40+
{
41+
$builder = TestUserNoOptions::loadFileBuilder('/path/to/test_users.csv', true);
42+
43+
$this->assertNull($builder->getConnectionName());
44+
}
45+
3246
public function testLoadFile(): void
3347
{
3448
$this->instance(Builder::class, $builder = $this->createPartialMock(Builder::class, [

tests/Feature/Models/TestUser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class TestUser extends Model
1010
{
1111
use LoadsFiles;
1212

13+
protected $connection = 'test-connection';
14+
1315
public function loadFileOptions(Builder $builder): void
1416
{
1517
$builder->replace();

0 commit comments

Comments
 (0)