66use Illuminate \Support \Facades \Schema ;
77use Illuminate \Support \Str ;
88
9- /**
10- * Service to generate migrations from existing database tables.
11- * Generates migration files for all tables or specific tables,
12- * including columns, indexes, and foreign keys.
13- *
14- * @author RobinNcode
15- */
16-
179class MigrationGenerator
1810{
1911 protected $ connection ;
2012 protected $ excludeTables ;
13+ protected $ currentTimestamp ;
2114
2215 public function __construct ($ connection = null )
2316 {
2417 $ this ->connection = $ connection ?? config ('database.default ' );
2518 $ this ->excludeTables = config ('db-craft.exclude_tables ' , []);
19+ $ this ->currentTimestamp = now ();
2620 }
2721
2822 /**
@@ -217,9 +211,10 @@ protected function buildMigrationContent($tableName, $columns, $indexes, $foreig
217211/**
218212 * Migration class for creating the {$ tableName } table.
219213 * Generated by Laravel DB Craft.
220- * Generated on: {{ date('Y-m-d H:i:s') } }
214+ * Generated on: {$ this -> currentTimestamp -> toDateTimeString () }
221215 */
222216
217+
223218return new class extends Migration
224219{
225220 /**
@@ -283,8 +278,15 @@ protected function getColumnDefinition($column, $driver)
283278 }
284279
285280 // Map database types to Laravel types
286- $ laravelType = $ this ->mapToLaravelType ($ type );
287- $ definition = "\$table-> {$ laravelType }(' {$ name }') " ;
281+ $ typeInfo = $ this ->mapToLaravelType ($ type );
282+ $ definition = "\$table-> {$ typeInfo ['method ' ]}(' {$ name }' " ;
283+
284+ // Add length parameter if present
285+ if (isset ($ typeInfo ['length ' ])) {
286+ $ definition .= ", {$ typeInfo ['length ' ]}" ;
287+ }
288+
289+ $ definition .= ") " ;
288290
289291 if ($ nullable ) {
290292 $ definition .= "->nullable() " ;
@@ -305,16 +307,28 @@ protected function mapToLaravelType($type)
305307 {
306308 $ type = strtolower ($ type );
307309
308- // Handle types with parameters like varchar(255)
309- if (preg_match ('/^(\w+)\((\d +)\)/ ' , $ type , $ matches )) {
310+ // Handle types with parameters like varchar(255), char(10), decimal(8,2)
311+ if (preg_match ('/^(\w+)\((. +)\)/ ' , $ type , $ matches )) {
310312 $ baseType = $ matches [1 ];
311- $ length = $ matches [2 ];
313+ $ params = $ matches [2 ];
312314
313315 if ($ baseType === 'varchar ' ) {
314- return "string " ;
316+ $ length = (int ) $ params ;
317+ return ['method ' => 'string ' , 'length ' => $ length ];
315318 }
319+
316320 if ($ baseType === 'char ' ) {
317- return "char, {$ length }" ;
321+ $ length = (int ) $ params ;
322+ return ['method ' => 'char ' , 'length ' => $ length ];
323+ }
324+
325+ if ($ baseType === 'decimal ' ) {
326+ // Handle decimal(8,2) format
327+ if (strpos ($ params , ', ' ) !== false ) {
328+ list ($ precision , $ scale ) = explode (', ' , $ params );
329+ return ['method ' => 'decimal ' , 'length ' => trim ($ precision ) . ', ' . trim ($ scale )];
330+ }
331+ return ['method ' => 'decimal ' , 'length ' => $ params ];
318332 }
319333 }
320334
@@ -346,11 +360,11 @@ protected function mapToLaravelType($type)
346360
347361 foreach ($ typeMap as $ dbType => $ laravelType ) {
348362 if (strpos ($ type , $ dbType ) !== false ) {
349- return $ laravelType ;
363+ return [ ' method ' => $ laravelType] ;
350364 }
351365 }
352366
353- return ' string ' ;
367+ return [ ' method ' => ' string '] ;
354368 }
355369
356370 /**
0 commit comments