Skip to content

Commit 09fc6c5

Browse files
Ability to set model connection (#680)
1 parent eb6028b commit 09fc6c5

File tree

10 files changed

+66
-0
lines changed

10 files changed

+66
-0
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ protected function populateStub(string $stub, Model $model): string
9494
$stub = $this->disableForeignKeyConstraints($stub);
9595
}
9696

97+
if ($model->usesCustomDatabaseConnection()) {
98+
$property = str_replace(
99+
['{{ name }}', 'by the model.'],
100+
[$model->databaseConnection(), 'by the migration.'],
101+
$this->filesystem->stub('model.connection.stub')
102+
);
103+
104+
$stub = Str::replaceFirst('{', '{' . PHP_EOL . $property, $stub);
105+
}
106+
97107
return $stub;
98108
}
99109

src/Generators/ModelGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ protected function buildProperties(Model $model): string
171171
{
172172
$properties = [];
173173

174+
if ($model->usesCustomDatabaseConnection()) {
175+
$properties[] = str_replace('{{ name }}', $model->databaseConnection(), $this->filesystem->stub('model.connection.stub'));
176+
}
177+
174178
if ($model->usesCustomTableName() || $model->isPivot()) {
175179
$properties[] = str_replace('{{ name }}', $model->tableName(), $this->filesystem->stub('model.table.stub'));
176180
}

src/Lexers/ModelLexer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ private function buildModel(string $name, array $columns): Model
132132
$model = new Model($name);
133133

134134
if (isset($columns['meta']) && is_array($columns['meta'])) {
135+
if (isset($columns['meta']['connection'])) {
136+
$model->setDatabaseConnection($columns['meta']['connection']);
137+
}
138+
135139
if (isset($columns['meta']['table'])) {
136140
$model->setTableName($columns['meta']['table']);
137141
}

src/Models/Model.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Model implements BlueprintModel
1919

2020
private string|bool $softDeletes = false;
2121

22+
private ?string $connection;
23+
2224
private string $table;
2325

2426
private array $columns = [];
@@ -124,6 +126,21 @@ public function setPivot(): void
124126
$this->pivot = true;
125127
}
126128

129+
public function usesCustomDatabaseConnection(): bool
130+
{
131+
return isset($this->connection);
132+
}
133+
134+
public function databaseConnection(): ?string
135+
{
136+
return $this->connection;
137+
}
138+
139+
public function setDatabaseConnection(string $connection)
140+
{
141+
$this->connection = $connection;
142+
}
143+
127144
public function usesCustomTableName(): bool
128145
{
129146
return isset($this->table);

stubs/model.connection.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The database connection that should be used by the model.
3+
*
4+
* @var string
5+
*/
6+
protected $connection = '{{ name }}';

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public function output_generates_migrations($definition, $path, $model): void
5454
->with('migration.stub')
5555
->andReturn($this->stub('migration.stub'));
5656

57+
if ($definition === 'drafts/model-with-meta.yaml') {
58+
$this->filesystem->expects('stub')
59+
->with('model.connection.stub')
60+
->andReturn($this->stub('model.connection.stub'));
61+
}
62+
5763
$now = Carbon::now();
5864
Carbon::setTestNow($now);
5965

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public function output_generates_models($definition, $path, $model): void
6060
}
6161

6262
if ($definition === 'drafts/model-with-meta.yaml') {
63+
$this->filesystem->expects('stub')
64+
->with('model.connection.stub')
65+
->andReturn($this->stub('model.connection.stub'));
66+
6367
$this->filesystem->expects('stub')
6468
->with('model.table.stub')
6569
->andReturn($this->stub('model.table.stub'));

tests/fixtures/drafts/model-with-meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
models:
22
Post:
33
meta:
4+
connection: read-only
45
pivot: true
56
table: post
67
title: string:400

tests/fixtures/migrations/model-with-meta.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
return new class extends Migration
88
{
9+
/**
10+
* The database connection that should be used by the migration.
11+
*
12+
* @var string
13+
*/
14+
protected $connection = 'read-only';
15+
916
/**
1017
* Run the migrations.
1118
*/

tests/fixtures/models/model-with-meta.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class Post extends Pivot
99
{
1010
use HasFactory;
1111

12+
/**
13+
* The database connection that should be used by the model.
14+
*
15+
* @var string
16+
*/
17+
protected $connection = 'read-only';
18+
1219
/**
1320
* The table associated with the model.
1421
*

0 commit comments

Comments
 (0)