Skip to content

Commit d1c82a7

Browse files
committed
update default doctrine methods
1 parent 8668cdf commit d1c82a7

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/DBAL/Platform/PostgreSQLPlatform.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public function columnsEqual(Column $column1, Column $column2): bool
153153
return is_subclass_of($type1, $type2::class) || is_subclass_of($type2, $type1::class);
154154
}
155155

156+
public function getDefaultColumnValueSQLSnippet(): string
157+
{
158+
return <<<'SQL'
159+
SELECT pg_get_expr(adbin, adrelid)
160+
FROM pg_attrdef
161+
WHERE c.oid = pg_attrdef.adrelid
162+
AND pg_attrdef.adnum=a.attnum
163+
SQL;
164+
}
165+
156166
protected function initializeDoctrineTypeMappings(): void
157167
{
158168
parent::initializeDoctrineTypeMappings();

src/DBAL/Schema/PostgreSQLSchemaManager.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
6969
$sql .= ' c.relname AS table_name, n.nspname AS schema_name,';
7070
}
7171

72-
$sql .= <<<'SQL'
72+
$sql .= sprintf(<<<'SQL'
7373
a.attnum,
7474
quote_ident(a.attname) AS field,
7575
t.typname AS type,
@@ -85,11 +85,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
8585
AND pg_index.indkey[0] = a.attnum
8686
AND pg_index.indisprimary = 't'
8787
) AS pri,
88-
(SELECT pg_get_expr(adbin, adrelid)
89-
FROM pg_attrdef
90-
WHERE c.oid = pg_attrdef.adrelid
91-
AND pg_attrdef.adnum=a.attnum
92-
) AS default,
88+
(%s) AS default,
9389
(SELECT pg_description.description
9490
FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid
9591
) AS comment,
@@ -105,7 +101,8 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
105101
LEFT JOIN pg_depend d
106102
ON d.objid = c.oid
107103
AND d.deptype = 'e'
108-
SQL;
104+
AND d.classid = (SELECT oid FROM pg_class WHERE relname = 'pg_class')
105+
SQL, $this->_platform->getDefaultColumnValueSQLSnippet());
109106

110107
$conditions = array_merge([
111108
'a.attnum > 0',
@@ -213,6 +210,7 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
213210

214211
break;
215212

213+
case 'json':
216214
case 'text':
217215
case '_varchar':
218216
case 'varchar':
@@ -302,7 +300,7 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
302300
}
303301
}
304302

305-
if (isset($tableColumn['collation']) && !empty($tableColumn['collation'])) {
303+
if (!empty($tableColumn['collation'])) {
306304
$column->setPlatformOption('collation', $tableColumn['collation']);
307305
}
308306

src/Tools/SchemaTool.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1010
use Doctrine\DBAL\Schema\Index;
1111
use Doctrine\DBAL\Schema\SchemaException;
12+
use Doctrine\DBAL\Schema\Sequence;
1213
use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets;
1314
use Doctrine\ORM\EntityManagerInterface;
1415
use Doctrine\ORM\Mapping\ClassMetadata;
@@ -226,10 +227,11 @@ static function (ClassMetadata $class) use ($idMapping): bool {
226227

227228
if (isset($class->table['uniqueConstraints'])) {
228229
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
229-
$uniqIndex = new Index($indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
230+
$uniqIndex = new Index('tmp__' . $indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
230231

231232
foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
232-
if ($tableIndex->isFullfilledBy($uniqIndex)) {
233+
$method = method_exists($tableIndex, 'isFulfilledBy') ? 'isFulfilledBy' : 'isFullfilledBy';
234+
if ($tableIndex->$method($uniqIndex)) {
233235
$table->dropIndex($tableIndexName);
234236

235237
break;
@@ -270,16 +272,23 @@ static function (ClassMetadata $class) use ($idMapping): bool {
270272
$this->gatherEnumTypes($class, $schema);
271273
}
272274

275+
if (!$this->platform->supportsSchemas()) {
276+
$filter = /** @param Sequence|Table $asset */ static function ($asset) use ($schema): bool {
277+
return !$asset->isInDefaultNamespace($schema->getName());
278+
};
279+
280+
if (array_filter($schema->getSequences() + $schema->getTables(), $filter) && !$this->platform->canEmulateSchemas()) {
281+
$schema->visit(new RemoveNamespacedAssets());
282+
}
283+
}
284+
285+
// remove default namespace creation from down migration
273286
foreach ($this->schemaManager->getExistingSchemaSearchPaths() as $namespace) {
274287
if (!$schema->hasNamespace($namespace)) {
275288
$schema->createNamespace($namespace);
276289
}
277290
}
278291

279-
if (!$this->platform->supportsSchemas() && !$this->platform->canEmulateSchemas()) {
280-
$schema->visit(new RemoveNamespacedAssets());
281-
}
282-
283292
if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
284293
$eventManager->dispatchEvent(
285294
ToolEvents::postGenerateSchema,
@@ -412,7 +421,7 @@ private function gatherColumnOptions(array $mapping): array
412421
}
413422

414423
$options = array_intersect_key($mappingOptions, array_flip(self::KNOWN_COLUMN_OPTIONS));
415-
$options['customSchemaOptions'] = array_diff_key($mappingOptions, $options);
424+
$options['platformOptions'] = array_diff_key($mappingOptions, $options);
416425

417426
return $options;
418427
}
@@ -689,6 +698,7 @@ private function addDiscriminatorColumnDefinition(ClassMetadata $class, Table $t
689698
$options['columnDefinition'] = $discrColumn['columnDefinition'];
690699
}
691700

701+
$options = $this->gatherColumnOptions($discrColumn) + $options;
692702
$table->addColumn($discrColumn['name'], $discrColumn['type'], $options);
693703
}
694704

0 commit comments

Comments
 (0)