22
33namespace Toramanlis \ImplicitMigrations \Blueprint \Exporters ;
44
5+ use Illuminate \Database \Eloquent \Model ;
56use Illuminate \Database \Schema \Builder ;
67use Illuminate \Database \Schema \ColumnDefinition ;
8+ use Illuminate \Support \Fluent ;
79use Toramanlis \ImplicitMigrations \Attributes \Column ;
810use Toramanlis \ImplicitMigrations \Attributes \IndexType ;
911
@@ -17,10 +19,34 @@ class ColumnExporter extends Exporter
1719
1820 protected ?string $ collapsedType = null ;
1921
22+ public ?Fluent $ foreignKey = null ;
23+
2024 public const SUPPORTED_MODIFIERS = Column::SUPPORTED_MODIFIERS ;
2125
22- public function __construct (protected ColumnDefinition $ definition )
26+ public function __construct (public readonly ColumnDefinition $ definition )
2327 {
28+ $ this ->attributes = array_filter ($ this ->definition ->getAttributes ());
29+ unset(
30+ $ this ->attributes ['type ' ],
31+ $ this ->attributes ['name ' ],
32+ $ this ->attributes ['change ' ],
33+ $ this ->attributes [IndexType::Primary->value ],
34+ $ this ->attributes [IndexType::Unique->value ],
35+ $ this ->attributes [IndexType::Index->value ]
36+ );
37+
38+ $ this ->runCollapsables ();
39+ }
40+
41+ public function setForeignKey (Fluent $ foreignKey )
42+ {
43+ if ('unsignedBigInteger ' !== $ this ->collapsedType || !empty ($ this ->collapsedAttributes )) {
44+ return false ;
45+ }
46+
47+ $ foreignKey ->references = 'id ' === $ foreignKey ->references ? null : $ foreignKey ->references ;
48+ $ this ->foreignKey = $ foreignKey ;
49+ return true ;
2450 }
2551
2652 protected function buildIndexModifiers (bool $ up = true )
@@ -41,17 +67,6 @@ protected function buildIndexModifiers(bool $up = true)
4167
4268 protected function exportUp (): string
4369 {
44- $ this ->attributes = array_filter ($ this ->definition ->getAttributes ());
45- unset(
46- $ this ->attributes ['type ' ],
47- $ this ->attributes ['name ' ],
48- $ this ->attributes ['change ' ],
49- $ this ->attributes [IndexType::Primary->value ],
50- $ this ->attributes [IndexType::Unique->value ],
51- $ this ->attributes [IndexType::Index->value ]
52- );
53-
54- $ this ->runCollapsables ();
5570 $ modifiers = $ this ->extractModifiers ($ this ->attributes );
5671 $ indexModifiers = $ this ->buildIndexModifiers ();
5772 $ collapsedModifiers = $ this ->extractModifiers ($ this ->collapsedAttributes );
@@ -73,12 +88,23 @@ protected function exportUp(): string
7388 $ type = $ this ->definition ->type ;
7489 }
7590
91+ $ modifiers = array_merge ($ modifiers , $ indexModifiers );
92+
7693 $ parameters = in_array ($ type , ['id ' , 'rememberToken ' , 'softDeletes ' ]) ? [] : [$ this ->definition ->name ];
7794 if ('id ' === $ type && 'id ' !== $ this ->definition ->name ) {
7895 $ parameters [] = $ this ->definition ->name ;
7996 }
8097
81- return $ this ->exportMethodCall ($ type , $ parameters , array_merge ($ modifiers , $ indexModifiers ));
98+ if ($ this ->foreignKey && 'unsignedBigInteger ' === $ type ) {
99+ $ modifiers = array_merge ([static ::makeModifier ('constrained ' , array_filter ([
100+ $ this ->foreignKey ->on ,
101+ 'id ' == $ this ->foreignKey ->references ? null : $ this ->foreignKey ->references ,
102+ $ this ->foreignKey ->index
103+ ]))], $ modifiers );
104+ return $ this ->exportMethodCall ('foreignId ' , $ parameters , $ modifiers );
105+ }
106+
107+ return $ this ->exportMethodCall ($ type , $ parameters , $ modifiers );
82108 } else {
83109 return $ this ->exportMethodCall ('addColumn ' , [
84110 $ this ->definition ->type ,
0 commit comments