Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"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",
"seeder",
"database",
"generator"
],
"type": "library",
"type": "package",
"license": "MIT",
"authors": [
{
Expand Down
50 changes: 32 additions & 18 deletions src/Services/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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()";
Expand All @@ -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];
}
}

Expand Down Expand Up @@ -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'];
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Services/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
{
Expand Down
32 changes: 32 additions & 0 deletions src/Templates/migration.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('{{tableName}}', function (Blueprint $table) {
$table->id();
{{schema}}
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('{{tableName}}');
}
};
20 changes: 20 additions & 0 deletions src/Templates/seeder.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class {{className}} extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
{{data}}
}
}