Skip to content

Commit

Permalink
Merge pull request #257 from kitloong/7.x
Browse files Browse the repository at this point in the history
Merge 7.x
  • Loading branch information
kitloong authored Feb 23, 2025
2 parents 1aa33f0 + 542c416 commit d52cbab
Show file tree
Hide file tree
Showing 40 changed files with 151 additions and 298 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:^11.0" --no-interaction --no-update
composer require "laravel/framework:^12.0" --no-interaction --no-update
composer update --prefer-stable --prefer-dist --no-interaction --no-progress
- name: phpcs
Expand Down
10 changes: 2 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ jobs:
strategy:
matrix:
php: [ 8.2, 8.3, 8.4 ]
laravel: [ 11.* ]
sqlsrv_extension: [ pdo_sqlsrv ]
include:
- php: 8.1
laravel: 10.43.*
- php: 8.1
laravel: 10.*
laravel: [ 11.*, 12.* ]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand All @@ -107,7 +101,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, memcached, ${{matrix.sqlsrv_extension}}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, memcached, pdo_sqlsrv
tools: composer:v2
coverage: pcov

Expand Down
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ Laravel Migrations Generator supports all five Laravel first-party support datab
- [x] SQL Server
- [x] SQLite

## Version Compatibility

| Laravel | Version |
|--------------------|-------------------------------------------------|
| 11.x | 7.x |
| \>= 10.43.x | 7.x |
| 10.x \| <= 10.42.x | 6.x |
| 9.x | 6.x |
| 8.x | 6.x |
| 7.x | 6.x |
| 6.x | 6.x |
| 5.8.x | 6.x |
| 5.7.x | 6.x |
| 5.6.x | 6.x |
| <= 5.5.x | https://github.com/Xethron/migrations-generator |

## Install

The recommended way to install this is through composer:
Expand Down
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://getcomposer.org/schema.json",
"name": "kitloong/laravel-migrations-generator",
"description": "Generates Laravel Migrations from an existing database",
"keywords": [
Expand All @@ -17,19 +18,20 @@
}
],
"require": {
"php": "^8.1",
"illuminate/support": "^10.43|^11.0",
"php": "^8.2",
"illuminate/support": "^11.0|^12.0",
"ext-pdo": "*"
},
"require-dev": {
"orchestra/testbench": "^8.0|^9.0",
"squizlabs/php_codesniffer": "^3.5",
"mockery/mockery": "^1.0",
"barryvdh/laravel-ide-helper": "^3.0",
"friendsofphp/php-cs-fixer": "^3.1",
"larastan/larastan": "^1.0|^2.0",
"slevomat/coding-standard": "^8.0",
"larastan/larastan": "^2.0|^3.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^9.0|^10.0",
"phpmd/phpmd": "^2.10",
"barryvdh/laravel-ide-helper": "^2.0|^3.0"
"phpstan/phpstan-mockery": "^2.0",
"slevomat/coding-standard": "^8.0",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -65,6 +67,7 @@
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
Expand Down
12 changes: 3 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:

Expand All @@ -10,15 +11,8 @@ parameters:
# The level 9 is the highest level
level: 8

ignoreErrors:
- '#Method KitLoong\\MigrationsGenerator\\Database\\(.*)Schema::getViews\(\) should return Illuminate\\Support\\Collection<int, KitLoong\\MigrationsGenerator\\Schema\\Models\\View> but returns Illuminate\\Support\\Collection<(int|\(int\|string\)), KitLoong\\MigrationsGenerator\\Database\\Models\\(.*)\\(.*)View>.#'
- '#Method KitLoong\\MigrationsGenerator\\Database\\(.*)Schema::getProcedures\(\) should return Illuminate\\Support\\Collection<int, KitLoong\\MigrationsGenerator\\Schema\\Models\\Procedure> but returns Illuminate\\Support\\Collection<(int|\(int\|string\)), KitLoong\\MigrationsGenerator\\Database\\Models\\(.*)\\(.*)Procedure>.#'
- '#Method KitLoong\\MigrationsGenerator\\Database\\(.*)Schema::getForeignKeys\(\) should return Illuminate\\Support\\Collection<int, KitLoong\\MigrationsGenerator\\Schema\\Models\\ForeignKey> but returns Illuminate\\Support\\Collection<(int|\(int\|string\)), KitLoong\\MigrationsGenerator\\Database\\Models\\(.*)\\(.*)ForeignKey>.#'
- '#(.*)expects Illuminate\\Support\\Collection<int, KitLoong\\MigrationsGenerator\\Migration\\Blueprint\\WritableBlueprint>, Illuminate\\Support\\Collection<int, KitLoong\\MigrationsGenerator\\Migration\\Blueprint\\(.*)Blueprint> given.#'
- '#Call to an undefined method Mockery\\ExpectationInterface\|Mockery\\HigherOrderMessage::(.*)#'
- '#Cannot call method expectsQuestion\(\) on Illuminate\\Testing\\PendingCommand\|int\.#'
# `env` calls from `tests` directory are allowed
noEnvCallsOutsideOfConfig: false

excludePaths:
- ./tests/resources/**

checkMissingIterableValueType: true
2 changes: 1 addition & 1 deletion src/Database/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class DatabaseSchema implements Schema
*/
public function getTableNames(): Collection
{
return new Collection(SchemaFacade::getTableListing());
return new Collection(array_column(SchemaFacade::getTables(), 'name'));
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Database/Models/Blueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace KitLoong\MigrationsGenerator\Database\Models;

use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;

class Blueprint extends BaseBlueprint
{
use CheckLaravelVersion;

public function __construct(string $table)
{
if ($this->atLeastLaravel12()) {
parent::__construct(Schema::getConnection(), $table);

return;
}

parent::__construct($table); // @phpstan-ignore-line
}

/**
* @return string[]
*/
public function toSqlWithCompatible(): array
{
if ($this->atLeastLaravel12()) {
return parent::toSql();
}

return parent::toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar()); // @phpstan-ignore-line
}
}
3 changes: 0 additions & 3 deletions src/Database/Models/DatabaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
use KitLoong\MigrationsGenerator\Enum\Migrations\ColumnName;
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType;
use KitLoong\MigrationsGenerator\Schema\Models\Column;
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;

/**
* @phpstan-import-type SchemaColumn from \KitLoong\MigrationsGenerator\Database\DatabaseSchema
*/
abstract class DatabaseColumn implements Column
{
use CheckLaravelVersion;

private const REMEMBER_TOKEN_LENGTH = 100;

protected bool $autoincrement;
Expand Down
4 changes: 0 additions & 4 deletions src/Database/Models/MySQL/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ private function setStoredDefinition(): void
*/
private function setRealSpatialColumn(): void
{
if (!$this->atLeastLaravel11()) {
return;
}

switch ($this->type) {
case ColumnType::GEOMETRY_COLLECTION:
$this->spatialSubType = 'geometryCollection';
Expand Down
30 changes: 0 additions & 30 deletions src/Database/Models/PgSQL/PgSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,6 @@ private function setRawDefault(): void
$this->rawDefault = true;
}

/**
* Get geometry mapping.
*
* @return array<string, \KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType>
*/
private function getGeometryMap(): array
{
return [
'geometry' => ColumnType::GEOMETRY,
'geometrycollection' => ColumnType::GEOMETRY_COLLECTION,
'linestring' => ColumnType::LINE_STRING,
'multilinestring' => ColumnType::MULTI_LINE_STRING,
'multipoint' => ColumnType::MULTI_POINT,
'multipolygon' => ColumnType::MULTI_POLYGON,
'point' => ColumnType::POINT,
'polygon' => ColumnType::POLYGON,
];
}

/**
* Set to geometry type base on geography map.
*/
Expand Down Expand Up @@ -171,17 +152,6 @@ private function setRealSpatialColumn(string $fullDefinitionType): void
$spatialSubType = $matches[2];
$spatialSrID = isset($matches[3]) ? (int) $matches[3] : null;

if (!$this->atLeastLaravel11()) {
$map = $this->getGeometryMap();

if (!isset($map[$spatialSubType])) {
return;
}

$this->type = $map[$spatialSubType];
return;
}

$this->spatialSubType = $spatialSubType;
$this->spatialSrID = $spatialSrID;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Models/PgSQL/PgSQLIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace KitLoong\MigrationsGenerator\Database\Models\PgSQL;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Database\Models\Blueprint;
use KitLoong\MigrationsGenerator\Database\Models\DatabaseIndex;
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\IndexType;
use KitLoong\MigrationsGenerator\Support\TableName;
Expand Down Expand Up @@ -37,6 +36,6 @@ public function __construct(string $table, array $index, bool $hasUDTColumn)
// Generate the alter index statement.
$blueprint->{$this->type->value}($this->columns, $this->name);

$this->udtColumnSqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
$this->udtColumnSqls = $blueprint->toSqlWithCompatible();
}
}
5 changes: 2 additions & 3 deletions src/Database/Models/PgSQL/PgSQLUDTColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace KitLoong\MigrationsGenerator\Database\Models\PgSQL;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use KitLoong\MigrationsGenerator\Database\Models\Blueprint;
use KitLoong\MigrationsGenerator\Database\Models\DatabaseUDTColumn;
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType;
use KitLoong\MigrationsGenerator\Support\TableName;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function __construct(string $table, array $column)
'nullable' => $column['nullable'],
]);

$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
$sqls = $blueprint->toSqlWithCompatible();

// Replace the string column type with the user-defined type.
$sqls[0] = Str::replaceFirst(' varchar ', ' ' . $column['type'] . ' ', $sqls[0]);
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Models/SQLSrv/SQLSrvIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace KitLoong\MigrationsGenerator\Database\Models\SQLSrv;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use KitLoong\MigrationsGenerator\Database\Models\Blueprint;
use KitLoong\MigrationsGenerator\Database\Models\DatabaseIndex;
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\IndexType;
use KitLoong\MigrationsGenerator\Support\TableName;
Expand Down Expand Up @@ -37,7 +36,7 @@ public function __construct(string $table, array $index, bool $hasUDTColumn)
// Generate the alter index statement.
$blueprint->{$this->type->value}($this->columns, $this->name);

$this->udtColumnSqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
$this->udtColumnSqls = $blueprint->toSqlWithCompatible();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Models/SQLSrv/SQLSrvUDTColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace KitLoong\MigrationsGenerator\Database\Models\SQLSrv;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use KitLoong\MigrationsGenerator\Database\Models\Blueprint;
use KitLoong\MigrationsGenerator\Database\Models\DatabaseUDTColumn;
use KitLoong\MigrationsGenerator\Support\TableName;

Expand All @@ -30,7 +29,7 @@ public function __construct(string $table, array $column)
'nullable' => $column['nullable'],
]);

$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
$sqls = $blueprint->toSqlWithCompatible();

// Replace the string column type with the user-defined type.
$sqls[0] = Str::replaceFirst(' nvarchar() ', ' ' . $column['type'] . ' ', $sqls[0]);
Expand Down
3 changes: 1 addition & 2 deletions src/Database/MySQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function getViewNames(): Collection
*/
public function getViews(): Collection
{
return $this->getSchemaViews()
->map(static fn (array $view) => new MySQLView($view));
return $this->getSchemaViews()->map(static fn (array $view) => new MySQLView($view));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Database/PgSQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public function getTable(string $name): Table
*/
public function getViewNames(): Collection
{
return $this->getViews()
->map(static fn (View $view) => $view->getName());
return $this->getViews()->map(static fn (View $view) => $view->getName());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Database/SQLiteSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function getViewNames(): Collection
*/
public function getViews(): Collection
{
return $this->getSchemaViews()
->map(static fn (array $view) => new SQLiteView($view));
return $this->getSchemaViews()->map(static fn (array $view) => new SQLiteView($view));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
class MigrateGenerateCommand extends Command
{
/**
* The name and signature of the console command.
* @inheritDoc
*/
// phpcs:ignore
protected $signature = 'migrate:generate
{tables? : A list of tables or views you wish to generate migrations for separated by a comma: users,posts,comments}
{--c|connection= : The database connection to use}
Expand All @@ -54,9 +53,8 @@ class MigrateGenerateCommand extends Command
{--with-has-table : Check for the existence of a table using `hasTable`}';

/**
* The console command description.
* @inheritDoc
*/
// phpcs:ignore
protected $description = 'Generate migrations from an existing table structure.';

protected Schema $schema;
Expand Down
Loading

0 comments on commit d52cbab

Please sign in to comment.