From 5b4ee89e8b5aaa4510ebbc67224583acc2d4e0b4 Mon Sep 17 00:00:00 2001 From: robinNcode Date: Sat, 1 Nov 2025 23:53:40 +0600 Subject: [PATCH] fix: correct syntax generation for column types with parameters Signed-off-by: robinNcode --- composer.json | 4 +-- src/Services/MigrationGenerator.php | 50 ++++++++++++++++++----------- src/Services/SeederGenerator.php | 4 ++- src/Templates/migration.stub | 32 ++++++++++++++++++ src/Templates/seeder.stub | 20 ++++++++++++ 5 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 src/Templates/migration.stub create mode 100644 src/Templates/seeder.stub diff --git a/composer.json b/composer.json index 4d71513..b0cf4fb 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "robinncode/laravel-db-craft", "description": "Generate Laravel migrations and seeders from existing database connections", - "version": "1.0.2", + "version": "1.0.3", "keywords": [ "laravel", "migration", @@ -9,7 +9,7 @@ "database", "generator" ], - "type": "library", + "type": "package", "license": "MIT", "authors": [ { diff --git a/src/Services/MigrationGenerator.php b/src/Services/MigrationGenerator.php index 96129ac..72cd172 100644 --- a/src/Services/MigrationGenerator.php +++ b/src/Services/MigrationGenerator.php @@ -6,23 +6,17 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Support\Str; -/** - * Service to generate migrations from existing database tables. - * Generates migration files for all tables or specific tables, - * including columns, indexes, and foreign keys. - * - * @author RobinNcode - */ - class MigrationGenerator { protected $connection; protected $excludeTables; + protected $currentTimestamp; public function __construct($connection = null) { $this->connection = $connection ?? config('database.default'); $this->excludeTables = config('db-craft.exclude_tables', []); + $this->currentTimestamp = now(); } /** @@ -217,9 +211,10 @@ protected function buildMigrationContent($tableName, $columns, $indexes, $foreig /** * Migration class for creating the {$tableName} table. * Generated by Laravel DB Craft. - * Generated on: {{ date('Y-m-d H:i:s') }} + * Generated on: {$this->currentTimestamp->toDateTimeString()} */ + return new class extends Migration { /** @@ -283,8 +278,15 @@ protected function getColumnDefinition($column, $driver) } // Map database types to Laravel types - $laravelType = $this->mapToLaravelType($type); - $definition = "\$table->{$laravelType}('{$name}')"; + $typeInfo = $this->mapToLaravelType($type); + $definition = "\$table->{$typeInfo['method']}('{$name}'"; + + // Add length parameter if present + if (isset($typeInfo['length'])) { + $definition .= ", {$typeInfo['length']}"; + } + + $definition .= ")"; if ($nullable) { $definition .= "->nullable()"; @@ -305,16 +307,28 @@ protected function mapToLaravelType($type) { $type = strtolower($type); - // Handle types with parameters like varchar(255) - if (preg_match('/^(\w+)\((\d+)\)/', $type, $matches)) { + // Handle types with parameters like varchar(255), char(10), decimal(8,2) + if (preg_match('/^(\w+)\((.+)\)/', $type, $matches)) { $baseType = $matches[1]; - $length = $matches[2]; + $params = $matches[2]; if ($baseType === 'varchar') { - return "string"; + $length = (int) $params; + return ['method' => 'string', 'length' => $length]; } + if ($baseType === 'char') { - return "char, {$length}"; + $length = (int) $params; + return ['method' => 'char', 'length' => $length]; + } + + if ($baseType === 'decimal') { + // Handle decimal(8,2) format + if (strpos($params, ',') !== false) { + list($precision, $scale) = explode(',', $params); + return ['method' => 'decimal', 'length' => trim($precision) . ', ' . trim($scale)]; + } + return ['method' => 'decimal', 'length' => $params]; } } @@ -346,11 +360,11 @@ protected function mapToLaravelType($type) foreach ($typeMap as $dbType => $laravelType) { if (strpos($type, $dbType) !== false) { - return $laravelType; + return ['method' => $laravelType]; } } - return 'string'; + return ['method' => 'string']; } /** diff --git a/src/Services/SeederGenerator.php b/src/Services/SeederGenerator.php index 0fbcba1..ef821e5 100644 --- a/src/Services/SeederGenerator.php +++ b/src/Services/SeederGenerator.php @@ -16,11 +16,13 @@ class SeederGenerator protected $connection; protected $excludeTables; protected $chunkSize; + protected $currentTimestamp; public function __construct($connection = null) { $this->connection = $connection ?? config('database.default'); $this->excludeTables = config('db-craft.exclude_tables', []); + $this->currentTimestamp = now(); $this->chunkSize = config('db-craft.seeder_chunk_size', 100); } @@ -130,7 +132,7 @@ protected function buildSeederContent($tableName, $data) /** * Seeder class for the {$tableName} table. * Generated by Laravel DB Craft. - * Generated on: {{ date('Y-m-d H:i:s') }} + * Generated on: {$this->currentTimestamp->toDateTimeString()} */ class {$className} extends Seeder { diff --git a/src/Templates/migration.stub b/src/Templates/migration.stub new file mode 100644 index 0000000..f01debf --- /dev/null +++ b/src/Templates/migration.stub @@ -0,0 +1,32 @@ +id(); + {{schema}} + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('{{tableName}}'); + } +}; diff --git a/src/Templates/seeder.stub b/src/Templates/seeder.stub new file mode 100644 index 0000000..47bb3c1 --- /dev/null +++ b/src/Templates/seeder.stub @@ -0,0 +1,20 @@ +