Skip to content

Commit

Permalink
Merge pull request #226 from kitloong/feature/update
Browse files Browse the repository at this point in the history
Fix update
  • Loading branch information
kitloong authored Aug 4, 2024
2 parents ca7d318 + 6dc429c commit f6900e8
Show file tree
Hide file tree
Showing 30 changed files with 127 additions and 103 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<exclude name="ExcessiveParameterList"/>
<exclude name="NPathComplexity"/>
<exclude name="TooManyFields"/>
<exclude name="TooManyMethods"/>
<exclude name="TooManyPublicMethods"/>
</rule>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
Expand Down Expand Up @@ -47,6 +48,11 @@
<property name="maxfields" value="25"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="ignorepattern" value="(^(set|get|is|has|with|test))i"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="ignorepattern" value="(^(set|get|is|has|with|test))i"/>
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<rule ref="SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace"/>
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
<rule ref="SlevomatCodingStandard.Attributes.AttributesOrder"/>
<!--<rule ref="SlevomatCodingStandard.Attributes.AttributesOrder"/>-->
<rule ref="SlevomatCodingStandard.Attributes.DisallowAttributesJoining"/>
<rule ref="SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine"/>
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>
Expand Down
6 changes: 6 additions & 0 deletions src/Database/Models/PgSQL/PgSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
34 changes: 21 additions & 13 deletions src/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/Repositories/MySQLRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
7 changes: 1 addition & 6 deletions src/Repositories/SQLSrvRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,14 @@ 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.
*/
private function getTableWhereClause(string $table, string $schemaColumn, string $tableColumn): 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);
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/MySQL57/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 9 additions & 2 deletions tests/Feature/MySQL57/StackedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace KitLoong\MigrationsGenerator\Tests\Unit\Database\Models\PgSQL;

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;
use PHPUnit\Framework\Attributes\DataProvider;

class PgSQLColumnTest extends TestCase
{
use CheckLaravelVersion;

#[DataProvider('spatialTypeNameProvider')]
public function testSpatialTypeName(string $type): void
{
$this->mock(PgSQLRepository::class, static function (MockInterface $mock): void {
$mock->shouldReceive('getStoredDefinition');
});

$column = new PgSQLColumn('table', [
'name' => 'column',
'type_name' => 'geography',
'type' => $type,
'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 array<string, string[]>
*/
public static function spatialTypeNameProvider(): array
{
return [
'with dot' => ['extensions.geography(Point,4326)'],
'without dot' => ['geography(Point,4326)'],
];
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Support\Facades\DB;
use KitLoong\MigrationsGenerator\Enum\Driver;
use KitLoong\MigrationsGenerator\Tests\TestMigration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/** @noinspection PhpIllegalPsrClassPathInspection */

/** @noinspection PhpUnused */

use Illuminate\Support\Facades\DB;
use KitLoong\MigrationsGenerator\Tests\TestMigration;

Expand Down
Loading

0 comments on commit f6900e8

Please sign in to comment.