@@ -108,22 +108,112 @@ protected function generateMigrationContentForOperations()
108108
109109 protected function handleAlterTable ($ operation )
110110 {
111- // Implementation remains the same
111+ $ tableName = $ operation ['params ' ]['table ' ];
112+ $ alterations = $ operation ['params ' ]['alterations ' ];
113+
114+ $ upOperations = [];
115+ $ downOperations = [];
116+
117+ foreach ($ alterations as $ alteration ) {
118+ switch ($ alteration ['type ' ]) {
119+ case 'AddColumn ' :
120+ $ upOperations [] = $ this ->stubParser ->parse ('add_column ' , [
121+ 'type ' => $ this ->mapPrismaTypeToLaravel ($ alteration ['column ' ]['type ' ]),
122+ 'name ' => $ alteration ['column ' ]['name ' ],
123+ 'nullable ' => $ alteration ['column ' ]['nullable ' ] ? '->nullable() ' : '' ,
124+ 'default ' => isset ($ alteration ['column ' ]['default ' ]) ? "->default(' {$ alteration ['column ' ]['default ' ]}') " : '' ,
125+ ]);
126+ $ downOperations [] = $ this ->stubParser ->parse ('drop_column ' , [
127+ 'name ' => $ alteration ['column ' ]['name ' ],
128+ ]);
129+ break ;
130+ case 'DropColumn ' :
131+ $ upOperations [] = $ this ->stubParser ->parse ('drop_column ' , [
132+ 'name ' => $ alteration ['column ' ],
133+ ]);
134+ // Note: We can't reliably reverse a column drop without knowing its original definition
135+ $ downOperations [] = "// TODO: Manually add column ' {$ alteration ['column ' ]}' definition here " ;
136+ break ;
137+ case 'RenameColumn ' :
138+ $ upOperations [] = $ this ->stubParser ->parse ('rename_column ' , [
139+ 'old_name ' => $ alteration ['from ' ],
140+ 'new_name ' => $ alteration ['to ' ],
141+ ]);
142+ $ downOperations [] = $ this ->stubParser ->parse ('rename_column ' , [
143+ 'old_name ' => $ alteration ['to ' ],
144+ 'new_name ' => $ alteration ['from ' ],
145+ ]);
146+ break ;
147+ }
148+ }
149+
150+ $ up = $ this ->stubParser ->parse ('alter_table ' , [
151+ 'table ' => $ tableName ,
152+ 'operations ' => implode ("\n " , $ upOperations ),
153+ ]);
154+
155+ $ down = $ this ->stubParser ->parse ('alter_table ' , [
156+ 'table ' => $ tableName ,
157+ 'operations ' => implode ("\n " , array_reverse ($ downOperations )),
158+ ]);
159+
160+ return [$ up , $ down ];
112161 }
113162
114163 protected function handleRenameTable ($ operation )
115164 {
116- // Implementation remains the same
165+ $ oldName = $ operation ['params ' ]['from ' ];
166+ $ newName = $ operation ['params ' ]['to ' ];
167+
168+ $ up = $ this ->stubParser ->parse ('rename_table ' , [
169+ 'old_name ' => $ oldName ,
170+ 'new_name ' => $ newName ,
171+ ]);
172+
173+ $ down = $ this ->stubParser ->parse ('rename_table ' , [
174+ 'old_name ' => $ newName ,
175+ 'new_name ' => $ oldName ,
176+ ]);
177+
178+ return [$ up , $ down ];
117179 }
118180
119181 protected function handleAddForeignKey ($ operation )
120182 {
121- // Implementation remains the same
183+ $ table = $ operation ['params ' ]['table ' ];
184+ $ foreignKey = $ operation ['params ' ]['foreignKey ' ];
185+
186+ $ up = $ this ->stubParser ->parse ('add_foreign_key ' , [
187+ 'table ' => $ table ,
188+ 'column ' => $ foreignKey ['column ' ],
189+ 'referenced_table ' => $ foreignKey ['referencedTable ' ],
190+ 'referenced_column ' => $ foreignKey ['referencedColumn ' ],
191+ 'on_delete ' => $ foreignKey ['onDelete ' ] ?? 'cascade ' ,
192+ 'on_update ' => $ foreignKey ['onUpdate ' ] ?? 'cascade ' ,
193+ ]);
194+
195+ $ down = $ this ->stubParser ->parse ('drop_foreign_key ' , [
196+ 'table ' => $ table ,
197+ 'foreign_key_name ' => "{$ table }_ {$ foreignKey ['column ' ]}_foreign " ,
198+ ]);
199+
200+ return [$ up , $ down ];
122201 }
123202
124203 protected function handleDropForeignKey ($ operation )
125204 {
126- // Implementation remains the same
205+ $ table = $ operation ['params ' ]['table ' ];
206+ $ foreignKeyName = $ operation ['params ' ]['foreignKey ' ];
207+
208+ $ up = $ this ->stubParser ->parse ('drop_foreign_key ' , [
209+ 'table ' => $ table ,
210+ 'foreign_key_name ' => $ foreignKeyName ,
211+ ]);
212+
213+ // Note: We can't reliably reverse a foreign key drop without knowing its original definition
214+ $ down = "// TODO: Manually add foreign key ' {$ foreignKeyName }' definition here " ;
215+
216+ return [$ up , $ down ];
127217 }
128218
129219 protected function generateCreateTableMethod ($ tableName , $ fields )
0 commit comments