From d5a06d957908785b6d4877b852270d6060601fbb Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 00:17:04 +0800 Subject: [PATCH 01/11] Add test --- tests/Feature/MySQL57/CommandTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Feature/MySQL57/CommandTest.php b/tests/Feature/MySQL57/CommandTest.php index ccfed9a..61487bd 100644 --- a/tests/Feature/MySQL57/CommandTest.php +++ b/tests/Feature/MySQL57/CommandTest.php @@ -425,6 +425,30 @@ public function testSkipLog(): void ); } + public function testSkipLogWithSquash(): void + { + $this->migrateGeneral(); + $this->truncateMigrationsTable(); + $this->dumpSchemaAs($this->getStorageSqlPath('expected.sql')); + + $this->artisan( + 'migrate:generate', + [ + '--path' => $this->getStorageMigrationsPath(), + '--skip-log' => true, + '--squash' => true, + ], + ); + + $this->assertSame(0, DB::table('migrations')->count()); + $this->dumpSchemaAs($this->getStorageSqlPath('actual.sql')); + + $this->assertFileEqualsIgnoringOrder( + $this->getStorageSqlPath('expected.sql'), + $this->getStorageSqlPath('actual.sql'), + ); + } + public function testLogWithBatch0(): void { $this->migrateGeneral(); From b890ed583cb3d2287636013d43a9c2ab4c2337b2 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 00:17:25 +0800 Subject: [PATCH 02/11] Add SRS_ID remark --- src/Repositories/MySQLRepository.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Repositories/MySQLRepository.php b/src/Repositories/MySQLRepository.php index a05bec7..32796c2 100644 --- a/src/Repositories/MySQLRepository.php +++ b/src/Repositories/MySQLRepository.php @@ -110,6 +110,8 @@ public function getSrID(string $table, string $column): ?int ); } catch (QueryException $exception) { if ( + // `SRS_ID` available since MySQL 8.0.3. + // https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-3.html Str::contains( $exception->getMessage(), "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SRS_ID'", From 1011f8c8d8560daa8303581d4900c1f6ee5b8ac8 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 00:18:15 +0800 Subject: [PATCH 03/11] Remove full qualified table name check --- src/Repositories/SQLSrvRepository.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Repositories/SQLSrvRepository.php b/src/Repositories/SQLSrvRepository.php index 591a0d4..957e7b9 100644 --- a/src/Repositories/SQLSrvRepository.php +++ b/src/Repositories/SQLSrvRepository.php @@ -135,7 +135,7 @@ public function getUserDefinedTypes(): Collection /** * Returns the where clause to filter schema and table name in a query. * - * @param string $table The full qualified name of the table. + * @param string $table The name of the table. * @param string $schemaColumn The name of the column to compare the schema to in the where clause. * @param string $tableColumn The name of the column to compare the table to in the where clause. */ @@ -143,11 +143,6 @@ private function getTableWhereClause(string $table, string $schemaColumn, string { $schema = 'SCHEMA_NAME()'; - if (str_contains($table, '.')) { - [$schema, $table] = explode('.', $table); - $schema = $this->quoteStringLiteral($schema); - } - $table = $this->quoteStringLiteral($table); return sprintf('(%s = %s AND %s = %s)', $tableColumn, $table, $schemaColumn, $schema); From 62364e2a81725c8c6a281e84850a6ca735c406be Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 00:21:12 +0800 Subject: [PATCH 04/11] Remove @noinspection tag --- .../2020_03_21_000000_expected_create_collations_table.php | 4 ---- ...020_03_21_000000_expected_create_user_collations_table.php | 4 ---- .../foreign/2020_03_21_000000_expected_create_users_table.php | 4 ---- .../2020_03_21_000001_expected_create_user_profile_table.php | 4 ---- .../2020_03_21_000000_expected_create_all_columns_table.php | 4 ---- .../2020_03_21_000000_expected_create_increments_table.php | 4 ---- .../2020_03_21_000000_expected_create_primary_table.php | 4 ---- .../2020_03_21_000000_expected_create_quoted_name_table.php | 4 ---- ...21_000000_expected_create_reserved_name_modifier_table.php | 4 ---- ...000_expected_create_reserved_name_with_precision_table.php | 4 ---- .../2020_03_21_000000_expected_create_test_index_table.php | 4 ---- .../2020_03_21_000000_expected_create_timestamps_table.php | 4 ---- .../general/2020_03_21_000000_expected_create_users_table.php | 4 ---- ...03_21_000001_expected_create_quoted_name_foreign_table.php | 4 ---- .../2020_03_21_000001_expected_create_quoted_name_proc.php | 4 ---- .../2020_03_21_000001_expected_create_quoted_name_view.php | 4 ---- .../2020_03_21_000001_expected_create_user_profile_table.php | 4 ---- .../general/2020_03_21_000001_expected_create_users_view.php | 4 ---- .../vendors/2018_08_08_100000_create_telescope_table.php | 3 --- .../2019_12_14_000001_create_personal_access_tokens_table.php | 4 ---- 20 files changed, 79 deletions(-) diff --git a/tests/resources/database/migrations/collation/2020_03_21_000000_expected_create_collations_table.php b/tests/resources/database/migrations/collation/2020_03_21_000000_expected_create_collations_table.php index bd3530c..2a4ce1a 100644 --- a/tests/resources/database/migrations/collation/2020_03_21_000000_expected_create_collations_table.php +++ b/tests/resources/database/migrations/collation/2020_03_21_000000_expected_create_collations_table.php @@ -1,9 +1,5 @@ Date: Sun, 4 Aug 2024 00:33:07 +0800 Subject: [PATCH 05/11] Disable healthcheck --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 133a1e1..2550067 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,8 +59,8 @@ jobs: ACCEPT_EULA: "Y" ports: - 1433:1433 - options: >- - --health-cmd "echo quit | /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -l 1 -U sa -P '!QAZ2wsx'" +# options: >- +# --health-cmd "echo quit | /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -l 1 -U sa -P '!QAZ2wsx'" mariadb: image: mariadb:10 From 431f9bd6568d70d3417e78e9b12c0e0cd09c8807 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 01:33:53 +0800 Subject: [PATCH 06/11] Update TooManyMethods rule --- .phpmd.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.phpmd.xml b/.phpmd.xml index 51b4b28..c1b641b 100644 --- a/.phpmd.xml +++ b/.phpmd.xml @@ -20,6 +20,7 @@ + @@ -47,6 +48,11 @@ + + + + + From fcc0cdfc709a7b86366d5c1d64e95fb17fde4b7d Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 01:35:57 +0800 Subject: [PATCH 07/11] Fix #222 Support PostGIS extensions on PgSQL --- src/Database/Models/PgSQL/PgSQLColumn.php | 6 +++ .../Database/Models/PgSQL/PgSQLColumnTest.php | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php diff --git a/src/Database/Models/PgSQL/PgSQLColumn.php b/src/Database/Models/PgSQL/PgSQLColumn.php index 0cbcb79..f34a71d 100644 --- a/src/Database/Models/PgSQL/PgSQLColumn.php +++ b/src/Database/Models/PgSQL/PgSQLColumn.php @@ -154,6 +154,12 @@ private function setRealSpatialColumn(string $fullDefinitionType): void return; } + $dotPosition = Str::position($dataType, '.'); + + if ($dotPosition !== false) { + $dataType = Str::substr($dataType, $dotPosition + 1); + } + if ($dataType === 'geography' || $dataType === 'geometry') { return; } diff --git a/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php new file mode 100644 index 0000000..21513d3 --- /dev/null +++ b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php @@ -0,0 +1,52 @@ +mock(PgSQLRepository::class, static function (MockInterface $mock): void { + $mock->shouldReceive('getStoredDefinition'); + }); + + $column = new PgSQLColumn('table', [ + 'name' => 'column', + 'type_name' => 'geography', + 'type' => 'extensions.geography(Point,4326)', + 'collation' => null, + 'nullable' => false, + 'default' => null, + 'auto_increment' => false, + 'comment' => null, + ]); + + $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + } + + public function testSpatialTypeNameWithoutDot(): void + { + $this->mock(PgSQLRepository::class, static function (MockInterface $mock): void { + $mock->shouldReceive('getStoredDefinition'); + }); + + $column = new PgSQLColumn('table', [ + 'name' => 'column', + 'type_name' => 'geography', + 'type' => 'geography(Point,4326)', + 'collation' => null, + 'nullable' => false, + 'default' => null, + 'auto_increment' => false, + 'comment' => null, + ]); + + $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + } +} From 4e912c6cc2b34c7c53474c40e9cc45c8ad328e84 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 10:10:41 +0800 Subject: [PATCH 08/11] Fix test --- .../Database/Models/PgSQL/PgSQLColumnTest.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php index 21513d3..dba7c3d 100644 --- a/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php +++ b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php @@ -5,11 +5,14 @@ use KitLoong\MigrationsGenerator\Database\Models\PgSQL\PgSQLColumn; use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType; use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository; +use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion; use KitLoong\MigrationsGenerator\Tests\TestCase; use Mockery\MockInterface; class PgSQLColumnTest extends TestCase { + use CheckLaravelVersion; + public function testSpatialTypeNameWithDot(): void { $this->mock(PgSQLRepository::class, static function (MockInterface $mock): void { @@ -27,7 +30,14 @@ public function testSpatialTypeNameWithDot(): void 'comment' => null, ]); - $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + if ($this->atLeastLaravel11()) { + $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + $this->assertSame('point', $column->getSpatialSubType()); + $this->assertSame(4326, $column->getSpatialSrID()); + return; + } + + $this->assertSame(ColumnType::POINT, $column->getType()); } public function testSpatialTypeNameWithoutDot(): void @@ -47,6 +57,13 @@ public function testSpatialTypeNameWithoutDot(): void 'comment' => null, ]); - $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + if ($this->atLeastLaravel11()) { + $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); + $this->assertSame('point', $column->getSpatialSubType()); + $this->assertSame(4326, $column->getSpatialSrID()); + return; + } + + $this->assertSame(ColumnType::POINT, $column->getType()); } } From 5e2ca00054dc2771b9ac3ed86281d466baa51b49 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 11:04:00 +0800 Subject: [PATCH 09/11] Disable AttributesOrder --- phpcs.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpcs.xml b/phpcs.xml index 314de42..9494849 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -28,7 +28,7 @@ - + From 2c29c99aa938670635ea19569a568cb4f82378ad Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 11:04:22 +0800 Subject: [PATCH 10/11] Merge tests --- .../Database/Models/PgSQL/PgSQLColumnTest.php | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php index dba7c3d..029c9c9 100644 --- a/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php +++ b/tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php @@ -8,12 +8,14 @@ use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion; use KitLoong\MigrationsGenerator\Tests\TestCase; use Mockery\MockInterface; +use PHPUnit\Framework\Attributes\DataProvider; class PgSQLColumnTest extends TestCase { use CheckLaravelVersion; - public function testSpatialTypeNameWithDot(): void + #[DataProvider('spatialTypeNameProvider')] + public function testSpatialTypeName(string $type): void { $this->mock(PgSQLRepository::class, static function (MockInterface $mock): void { $mock->shouldReceive('getStoredDefinition'); @@ -22,7 +24,7 @@ public function testSpatialTypeNameWithDot(): void $column = new PgSQLColumn('table', [ 'name' => 'column', 'type_name' => 'geography', - 'type' => 'extensions.geography(Point,4326)', + 'type' => $type, 'collation' => null, 'nullable' => false, 'default' => null, @@ -40,30 +42,14 @@ public function testSpatialTypeNameWithDot(): void $this->assertSame(ColumnType::POINT, $column->getType()); } - public function testSpatialTypeNameWithoutDot(): void + /** + * @return array + */ + public static function spatialTypeNameProvider(): array { - $this->mock(PgSQLRepository::class, static function (MockInterface $mock): void { - $mock->shouldReceive('getStoredDefinition'); - }); - - $column = new PgSQLColumn('table', [ - 'name' => 'column', - 'type_name' => 'geography', - 'type' => 'geography(Point,4326)', - 'collation' => null, - 'nullable' => false, - 'default' => null, - 'auto_increment' => false, - 'comment' => null, - ]); - - if ($this->atLeastLaravel11()) { - $this->assertSame(ColumnType::GEOGRAPHY, $column->getType()); - $this->assertSame('point', $column->getSpatialSubType()); - $this->assertSame(4326, $column->getSpatialSrID()); - return; - } - - $this->assertSame(ColumnType::POINT, $column->getType()); + return [ + 'with dot' => ['extensions.geography(Point,4326)'], + 'without dot' => ['geography(Point,4326)'], + ]; } } From 9acdf1972f36530545137482740a0169d2e1b0f6 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Sun, 4 Aug 2024 19:46:58 +0800 Subject: [PATCH 11/11] Fix #223 Move dependency injection to `handle` --- src/MigrateGenerateCommand.php | 34 ++++++++++++-------- tests/Feature/MySQL57/StackedCommandTest.php | 11 +++++-- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/MigrateGenerateCommand.php b/src/MigrateGenerateCommand.php index 7e88d23..db3e9f4 100644 --- a/src/MigrateGenerateCommand.php +++ b/src/MigrateGenerateCommand.php @@ -64,25 +64,33 @@ class MigrateGenerateCommand extends Command protected bool $shouldLog = false; protected int $nextBatchNumber = 0; - - public function __construct( - protected MigrationRepositoryInterface $repository, - protected Squash $squash, - protected ForeignKeyMigration $foreignKeyMigration, - protected ProcedureMigration $procedureMigration, - protected TableMigration $tableMigration, - protected ViewMigration $viewMigration, - ) { - parent::__construct(); - } + protected MigrationRepositoryInterface $repository; + protected Squash $squash; + protected ForeignKeyMigration $foreignKeyMigration; + protected ProcedureMigration $procedureMigration; + protected ViewMigration $viewMigration; + protected TableMigration $tableMigration; /** * Execute the console command. * * @throws \Exception */ - public function handle(): void - { + public function handle( + MigrationRepositoryInterface $repository, + Squash $squash, + ForeignKeyMigration $foreignKeyMigration, + ProcedureMigration $procedureMigration, + TableMigration $tableMigration, + ViewMigration $viewMigration, + ): void { + $this->tableMigration = $tableMigration; + $this->viewMigration = $viewMigration; + $this->procedureMigration = $procedureMigration; + $this->foreignKeyMigration = $foreignKeyMigration; + $this->squash = $squash; + $this->repository = $repository; + $previousConnection = DB::getDefaultConnection(); try { diff --git a/tests/Feature/MySQL57/StackedCommandTest.php b/tests/Feature/MySQL57/StackedCommandTest.php index 8530f8e..9aa0df3 100644 --- a/tests/Feature/MySQL57/StackedCommandTest.php +++ b/tests/Feature/MySQL57/StackedCommandTest.php @@ -24,12 +24,19 @@ public function testRunAsCall(): void $this->assertTrue(Schema::hasTable('migration_table')); $this->assertTrue(Schema::connection('migration2')->hasTable('migration2_table')); - $this->generateMigrations(); + $this->generateMigrations([ + '--table-filename' => 'create_migration_tables.php', + '--squash' => true, + ]); // Setting should reset. $this->assertEquals(app(Setting::class), new Setting()); - $this->generateMigrations(['--connection' => 'migration2']); + $this->generateMigrations([ + '--connection' => 'migration2', + '--table-filename' => 'create_migration2_tables.php', + '--squash' => true, + ]); $files = File::files($this->getStorageMigrationsPath()); $this->assertCount(2, $files);