Skip to content

Commit

Permalink
Merge pull request #64 from sasa-b/migration-diff-fix
Browse files Browse the repository at this point in the history
#62 Switch to platform options from deprecated custom schema options
  • Loading branch information
jsor authored Feb 20, 2024
2 parents be9bf2b + 7fa08bb commit cabb595
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
3 changes: 0 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
</DeprecatedClass>
<DeprecatedMethod>
<errorLevel type="suppress">
<referencedMethod name="Doctrine\DBAL\Schema\Column::getCustomSchemaOption"/>
<referencedMethod name="Doctrine\DBAL\Schema\Column::getCustomSchemaOptions"/>
<referencedMethod name="Doctrine\DBAL\Schema\Column::setCustomSchemaOption"/>
<referencedMethod name="Doctrine\DBAL\Schema\ColumnDiff::hasChanged"/>
<referencedMethod name="Doctrine\DBAL\Types\Type::getName"/>
</errorLevel>
Expand Down
8 changes: 4 additions & 4 deletions src/Event/DBALSchemaEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ public function onSchemaAlterTableChangeColumn(SchemaAlterTableChangeColumnEvent
}

if ($columnDiff->hasChanged('geometry_type')) {
throw new RuntimeException('The geometry_type of a spatial column cannot be changed (Requested changing type from "' . strtoupper((string) ($columnDiff->fromColumn?->getCustomSchemaOption('geometry_type') ?? 'N/A')) . '" to "' . strtoupper((string) $column->getCustomSchemaOption('geometry_type')) . '" for column "' . $column->getName() . '" in table "' . $table->getName() . '")');
throw new RuntimeException('The geometry_type of a spatial column cannot be changed (Requested changing type from "' . strtoupper((string) ($columnDiff->fromColumn?->getPlatformOption('geometry_type') ?? 'N/A')) . '" to "' . strtoupper((string) $column->getPlatformOption('geometry_type')) . '" for column "' . $column->getName() . '" in table "' . $table->getName() . '")');
}

if ($columnDiff->hasChanged('srid')) {
$args->addSql(sprintf(
"SELECT UpdateGeometrySRID('%s', '%s', %d)",
$table->getName(),
$column->getName(),
(int) $column->getCustomSchemaOption('srid')
(int) $column->getPlatformOption('srid')
));
}
}
Expand Down Expand Up @@ -201,8 +201,8 @@ public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $args):
$column = new Column($tableColumn['field'], PostGISType::getType($tableColumn['type']), $options);

$column
->setCustomSchemaOption('geometry_type', $info['type'])
->setCustomSchemaOption('srid', $info['srid'])
->setPlatformOption('geometry_type', $info['type'])
->setPlatformOption('srid', $info['srid'])
;

$args
Expand Down
4 changes: 2 additions & 2 deletions src/Event/ORMSchemaEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $args): voi
}

/** @var array{geometry_type?: string|null, srid?: int|string|null} $options */
$options = $column->getCustomSchemaOptions();
$options = $column->getPlatformOptions();

$normalized = $type->getNormalizedPostGISColumnOptions($options);

foreach ($normalized as $name => $value) {
$column->setCustomSchemaOption($name, $value);
$column->setPlatformOption($name, $value);
}
}
}
Expand Down
68 changes: 34 additions & 34 deletions tests/Event/DBALSchemaEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,68 +47,68 @@ private function createTableSchema(): Table
$table->addColumn('tsvector', 'tsvector', ['notnull' => true]);

$table->addColumn('geometry', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'GEOMETRY',
'srid' => 0,
]);

$table->addColumn('point', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 0,
])
->setComment('This is a comment for column point');

$table->addColumn('point_2d', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 3785,
]);

$table->addColumn('point_3dz', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINTZ',
'srid' => 3785,
]);

$table->addColumn('point_3dm', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINTM',
'srid' => 3785,
]);

$table->addColumn('point_4d', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINTZM',
'srid' => 3785,
]);

$table->addColumn('point_2d_nullable', 'geometry', ['notnull' => false])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 3785,
]);

$table->addColumn('point_2d_nosrid', 'geometry', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 0,
]);

$table->addColumn('geography', 'geography', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'GEOMETRY',
'srid' => 4326,
]);

$table->addColumn('point_geography_2d', 'geography', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 4326,
]);

$table->addColumn('point_geography_2d_srid', 'geography', ['notnull' => true])
->setCustomSchemaOptions([
->setPlatformOptions([
'geometry_type' => 'POINT',
'srid' => 4326,
]);
Expand Down Expand Up @@ -145,8 +145,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point']->getDefault());
$this->assertIsArray($columns['point']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(0, $columns['point']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point']->getPlatformOption('geometry_type'));
$this->assertEquals(0, $columns['point']->getPlatformOption('srid'));

// ---

Expand All @@ -158,8 +158,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_2d']->getDefault());
$this->assertIsArray($columns['point_2d']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point_2d']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(3785, $columns['point_2d']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point_2d']->getPlatformOption('geometry_type'));
$this->assertEquals(3785, $columns['point_2d']->getPlatformOption('srid'));

// ---

Expand All @@ -171,8 +171,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_3dz']->getDefault());
$this->assertIsArray($columns['point_3dz']->getPlatformOptions());

$this->assertEquals('POINTZ', $columns['point_3dz']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(3785, $columns['point_3dz']->getCustomSchemaOption('srid'));
$this->assertEquals('POINTZ', $columns['point_3dz']->getPlatformOption('geometry_type'));
$this->assertEquals(3785, $columns['point_3dz']->getPlatformOption('srid'));

// ---

Expand All @@ -184,8 +184,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_3dm']->getDefault());
$this->assertIsArray($columns['point_3dm']->getPlatformOptions());

$this->assertEquals('POINTM', $columns['point_3dm']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(3785, $columns['point_3dm']->getCustomSchemaOption('srid'));
$this->assertEquals('POINTM', $columns['point_3dm']->getPlatformOption('geometry_type'));
$this->assertEquals(3785, $columns['point_3dm']->getPlatformOption('srid'));

// ---

Expand All @@ -197,8 +197,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_4d']->getDefault());
$this->assertIsArray($columns['point_4d']->getPlatformOptions());

$this->assertEquals('POINTZM', $columns['point_4d']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(3785, $columns['point_4d']->getCustomSchemaOption('srid'));
$this->assertEquals('POINTZM', $columns['point_4d']->getPlatformOption('geometry_type'));
$this->assertEquals(3785, $columns['point_4d']->getPlatformOption('srid'));

// ---

Expand All @@ -210,8 +210,8 @@ public function testListTableColumns(): void
// $this->assertEquals('NULL::geometry', $columns['point_2d_nullable']->getDefault());
$this->assertIsArray($columns['point_2d_nullable']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point_2d_nullable']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(3785, $columns['point_2d_nullable']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point_2d_nullable']->getPlatformOption('geometry_type'));
$this->assertEquals(3785, $columns['point_2d_nullable']->getPlatformOption('srid'));

// ---

Expand All @@ -223,8 +223,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_2d_nosrid']->getDefault());
$this->assertIsArray($columns['point_2d_nosrid']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point_2d_nosrid']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(0, $columns['point_2d_nosrid']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point_2d_nosrid']->getPlatformOption('geometry_type'));
$this->assertEquals(0, $columns['point_2d_nosrid']->getPlatformOption('srid'));

// ---

Expand All @@ -236,8 +236,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_geography_2d']->getDefault());
$this->assertIsArray($columns['point_geography_2d']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point_geography_2d']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(4326, $columns['point_geography_2d']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point_geography_2d']->getPlatformOption('geometry_type'));
$this->assertEquals(4326, $columns['point_geography_2d']->getPlatformOption('srid'));

// ---

Expand All @@ -249,8 +249,8 @@ public function testListTableColumns(): void
$this->assertNull($columns['point_geography_2d_srid']->getDefault());
$this->assertIsArray($columns['point_geography_2d_srid']->getPlatformOptions());

$this->assertEquals('POINT', $columns['point_geography_2d_srid']->getCustomSchemaOption('geometry_type'));
$this->assertEquals(4326, $columns['point_geography_2d_srid']->getCustomSchemaOption('srid'));
$this->assertEquals('POINT', $columns['point_geography_2d_srid']->getPlatformOption('geometry_type'));
$this->assertEquals(4326, $columns['point_geography_2d_srid']->getPlatformOption('srid'));
}

public function testDiffListTableColumns(): void
Expand Down Expand Up @@ -330,7 +330,7 @@ public function testGetCreateTableSqlSkipsAlreadyAddedTable(): void
{
$schema = new Schema([], [], $this->sm->createSchemaConfig());

$this->_getMessengerConnection()->configureSchema($schema, $this->_getConnection());
$this->_getMessengerConnection()->configureSchema($schema, $this->_getConnection(), static fn () => false);

$sql = $this->_getConnection()->getDatabasePlatform()->getCreateTableSQL($schema->getTable('messenger_messages'));

Expand Down Expand Up @@ -359,16 +359,16 @@ public function testAlterTableScenario(): void

$tableDiff = new TableDiff('points');
$tableDiff->fromTable = $table;
$tableDiff->addedColumns['linestring'] = new Column('linestring', Type::getType('geometry'), ['customSchemaOptions' => ['geometry_type' => 'linestring', 'srid' => 3785]]);
$tableDiff->addedColumns['linestring'] = new Column('linestring', Type::getType('geometry'), ['platformOptions' => ['geometry_type' => 'linestring', 'srid' => 3785]]);
$tableDiff->removedColumns['point'] = $table->getColumn('point');
$tableDiff->changedColumns[] = new ColumnDiff('point_3dm', new Column('point_3dm', Type::getType('geometry'), ['customSchemaOptions' => ['srid' => 4326]]), ['srid'], $table->getColumn('point_3dm'));
$tableDiff->changedColumns[] = new ColumnDiff('point_3dm', new Column('point_3dm', Type::getType('geometry'), ['platformOptions' => ['srid' => 4326]]), ['srid'], $table->getColumn('point_3dm'));

$this->sm->alterTable($tableDiff);

$table = $this->sm->listTableDetails('points');
$this->assertFalse($table->hasColumn('point'));
$this->assertTrue($table->hasColumn('linestring'));
$this->assertEquals(4326, $table->getColumn('point_3dm')->getCustomSchemaOption('srid'));
$this->assertEquals(4326, $table->getColumn('point_3dm')->getPlatformOption('srid'));

$tableDiff = new TableDiff('points');
$tableDiff->fromTable = $table;
Expand Down Expand Up @@ -431,7 +431,7 @@ public function testAlterTableThrowsExceptionForChangedSpatialType(): void

$tableDiff = new TableDiff('points');
$tableDiff->fromTable = $table;
$tableDiff->changedColumns[] = new ColumnDiff('point_2d', new Column('point_2d', Type::getType('geometry'), ['customSchemaOptions' => ['geometry_type' => 'LINESTRING']]), ['geometry_type'], $table->getColumn('point_2d'));
$tableDiff->changedColumns[] = new ColumnDiff('point_2d', new Column('point_2d', Type::getType('geometry'), ['platformOptions' => ['geometry_type' => 'LINESTRING']]), ['geometry_type'], $table->getColumn('point_2d'));

$this->sm->alterTable($tableDiff);
}
Expand Down

0 comments on commit cabb595

Please sign in to comment.