Skip to content

Commit ffcbf30

Browse files
authored
Merge pull request #5 from robinNcode/development
fix: correct syntax generation for column types with parameters
2 parents e475da7 + 5b4ee89 commit ffcbf30

File tree

5 files changed

+89
-21
lines changed

5 files changed

+89
-21
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "robinncode/laravel-db-craft",
33
"description": "Generate Laravel migrations and seeders from existing database connections",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"keywords": [
66
"laravel",
77
"migration",
88
"seeder",
99
"database",
1010
"generator"
1111
],
12-
"type": "library",
12+
"type": "package",
1313
"license": "MIT",
1414
"authors": [
1515
{

src/Services/MigrationGenerator.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@
66
use Illuminate\Support\Facades\Schema;
77
use 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-
179
class 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+
223218
return 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
/**

src/Services/SeederGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class SeederGenerator
1616
protected $connection;
1717
protected $excludeTables;
1818
protected $chunkSize;
19+
protected $currentTimestamp;
1920

2021
public function __construct($connection = null)
2122
{
2223
$this->connection = $connection ?? config('database.default');
2324
$this->excludeTables = config('db-craft.exclude_tables', []);
25+
$this->currentTimestamp = now();
2426
$this->chunkSize = config('db-craft.seeder_chunk_size', 100);
2527
}
2628

@@ -130,7 +132,7 @@ protected function buildSeederContent($tableName, $data)
130132
/**
131133
* Seeder class for the {$tableName} table.
132134
* Generated by Laravel DB Craft.
133-
* Generated on: {{ date('Y-m-d H:i:s') }}
135+
* Generated on: {$this->currentTimestamp->toDateTimeString()}
134136
*/
135137
class {$className} extends Seeder
136138
{

src/Templates/migration.stub

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('{{tableName}}', function (Blueprint $table) {
17+
$table->id();
18+
{{schema}}
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('{{tableName}}');
31+
}
32+
};

src/Templates/seeder.stub

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
6+
use Illuminate\Database\Seeder;
7+
use Illuminate\Support\Facades\DB;
8+
9+
class {{className}} extends Seeder
10+
{
11+
/**
12+
* Run the database seeds.
13+
*
14+
* @return void
15+
*/
16+
public function run()
17+
{
18+
{{data}}
19+
}
20+
}

0 commit comments

Comments
 (0)