Skip to content

Commit

Permalink
Ability to set model connection (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Mar 13, 2024
1 parent eb6028b commit 09fc6c5
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ protected function populateStub(string $stub, Model $model): string
$stub = $this->disableForeignKeyConstraints($stub);
}

if ($model->usesCustomDatabaseConnection()) {
$property = str_replace(
['{{ name }}', 'by the model.'],
[$model->databaseConnection(), 'by the migration.'],
$this->filesystem->stub('model.connection.stub')
);

$stub = Str::replaceFirst('{', '{' . PHP_EOL . $property, $stub);
}

return $stub;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ protected function buildProperties(Model $model): string
{
$properties = [];

if ($model->usesCustomDatabaseConnection()) {
$properties[] = str_replace('{{ name }}', $model->databaseConnection(), $this->filesystem->stub('model.connection.stub'));
}

if ($model->usesCustomTableName() || $model->isPivot()) {
$properties[] = str_replace('{{ name }}', $model->tableName(), $this->filesystem->stub('model.table.stub'));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private function buildModel(string $name, array $columns): Model
$model = new Model($name);

if (isset($columns['meta']) && is_array($columns['meta'])) {
if (isset($columns['meta']['connection'])) {
$model->setDatabaseConnection($columns['meta']['connection']);
}

if (isset($columns['meta']['table'])) {
$model->setTableName($columns['meta']['table']);
}
Expand Down
17 changes: 17 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Model implements BlueprintModel

private string|bool $softDeletes = false;

private ?string $connection;

private string $table;

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

public function usesCustomDatabaseConnection(): bool
{
return isset($this->connection);
}

public function databaseConnection(): ?string
{
return $this->connection;
}

public function setDatabaseConnection(string $connection)
{
$this->connection = $connection;
}

public function usesCustomTableName(): bool
{
return isset($this->table);
Expand Down
6 changes: 6 additions & 0 deletions stubs/model.connection.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The database connection that should be used by the model.
*
* @var string
*/
protected $connection = '{{ name }}';
6 changes: 6 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function output_generates_migrations($definition, $path, $model): void
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

if ($definition === 'drafts/model-with-meta.yaml') {
$this->filesystem->expects('stub')
->with('model.connection.stub')
->andReturn($this->stub('model.connection.stub'));
}

$now = Carbon::now();
Carbon::setTestNow($now);

Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function output_generates_models($definition, $path, $model): void
}

if ($definition === 'drafts/model-with-meta.yaml') {
$this->filesystem->expects('stub')
->with('model.connection.stub')
->andReturn($this->stub('model.connection.stub'));

$this->filesystem->expects('stub')
->with('model.table.stub')
->andReturn($this->stub('model.table.stub'));
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/drafts/model-with-meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
models:
Post:
meta:
connection: read-only
pivot: true
table: post
title: string:400
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/migrations/model-with-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

return new class extends Migration
{
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'read-only';

/**
* Run the migrations.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/models/model-with-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class Post extends Pivot
{
use HasFactory;

/**
* The database connection that should be used by the model.
*
* @var string
*/
protected $connection = 'read-only';

/**
* The table associated with the model.
*
Expand Down

0 comments on commit 09fc6c5

Please sign in to comment.