Repository: laravel-shift/blueprint (php)

Issue:
Ability to add connection/table name in model 
- Laravel Version: 10.4.0
- PHP Version: 8.2.0
- Blueprint Version: 2.7.0
- Platform: Windows

### Issue:
Ability to add connection/table name in model 

### draft.yaml:
```yaml
Models:
    SystemInformationConversion:
         range: string:100
         rubric: string:200
         specified_value: string:50
         softDeletes: true
         timestamps: false

         table: system_information_conversion
         connection: db_connection_name
```


Pull request:
Ability to set model connection
This adds a `connection` option to the `meta` section of a model, which will generate a model and any migration with the `$connection` property set to that value.

For example:

```yaml
models:
  Example:
    meta:
      connection: readonly
    # ...
```

---
Closes #639

Patch:
diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php
index d4075db..895fdf1 100644
--- a/src/Generators/MigrationGenerator.php
+++ b/src/Generators/MigrationGenerator.php
@@ -94,6 +94,16 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
             $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;
     }
 
diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php
index 21c7829..9f1e38e 100644
--- a/src/Generators/ModelGenerator.php
+++ b/src/Generators/ModelGenerator.php
@@ -171,6 +171,10 @@ class ModelGenerator extends AbstractClassGenerator implements Generator
     {
         $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'));
         }
diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php
index f4d2f26..ffab3b0 100644
--- a/src/Lexers/ModelLexer.php
+++ b/src/Lexers/ModelLexer.php
@@ -132,6 +132,10 @@ class ModelLexer implements Lexer
         $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']);
             }
diff --git a/src/Models/Model.php b/src/Models/Model.php
index d4703b8..6ffb29e 100644
--- a/src/Models/Model.php
+++ b/src/Models/Model.php
@@ -19,6 +19,8 @@ class Model implements BlueprintModel
 
     private string|bool $softDeletes = false;
 
+    private ?string $connection;
+
     private string $table;
 
     private array $columns = [];
@@ -124,6 +126,21 @@ class Model implements BlueprintModel
         $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);
diff --git a/stubs/model.connection.stub b/stubs/model.connection.stub
new file mode 100644
index 0000000..5e3adb1
--- /dev/null
+++ b/stubs/model.connection.stub
@@ -0,0 +1,6 @@
+    /**
+     * The database connection that should be used by the model.
+     *
+     * @var string
+     */
+    protected $connection = '{{ name }}';


Test patch:
diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php
index c433047..807036a 100644
--- a/tests/Feature/Generators/MigrationGeneratorTest.php
+++ b/tests/Feature/Generators/MigrationGeneratorTest.php
@@ -54,6 +54,12 @@ final class MigrationGeneratorTest extends TestCase
             ->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);
 
diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php
index 52f88a1..2a4feda 100644
--- a/tests/Feature/Generators/ModelGeneratorTest.php
+++ b/tests/Feature/Generators/ModelGeneratorTest.php
@@ -60,6 +60,10 @@ final class ModelGeneratorTest extends TestCase
         }
 
         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'));
diff --git a/tests/fixtures/drafts/model-with-meta.yaml b/tests/fixtures/drafts/model-with-meta.yaml
index a615cf2..5b6c33b 100644
--- a/tests/fixtures/drafts/model-with-meta.yaml
+++ b/tests/fixtures/drafts/model-with-meta.yaml
@@ -1,6 +1,7 @@
 models:
   Post:
     meta:
+      connection: read-only
       pivot: true
       table: post
     title: string:400
diff --git a/tests/fixtures/migrations/model-with-meta.php b/tests/fixtures/migrations/model-with-meta.php
index 421baff..2a67bf6 100644
--- a/tests/fixtures/migrations/model-with-meta.php
+++ b/tests/fixtures/migrations/model-with-meta.php
@@ -6,6 +6,13 @@ use Illuminate\Support\Facades\Schema;
 
 return new class extends Migration
 {
+    /**
+     * The database connection that should be used by the migration.
+     *
+     * @var string
+     */
+    protected $connection = 'read-only';
+
     /**
      * Run the migrations.
      */
diff --git a/tests/fixtures/models/model-with-meta.php b/tests/fixtures/models/model-with-meta.php
index 70276c9..4697e3e 100644
--- a/tests/fixtures/models/model-with-meta.php
+++ b/tests/fixtures/models/model-with-meta.php
@@ -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.
      *