|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OC\Core\Command\Db; |
| 10 | + |
| 11 | +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; |
| 12 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 13 | +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; |
| 14 | +use Doctrine\DBAL\Schema\Schema; |
| 15 | +use Doctrine\DBAL\Schema\Table; |
| 16 | +use Doctrine\DBAL\Types\PhpIntegerMappingType; |
| 17 | +use Doctrine\DBAL\Types\Type; |
| 18 | + |
| 19 | +class SchemaEncoder { |
| 20 | + /** |
| 21 | + * Encode a DBAL schema to json, performing some normalization based on the database platform |
| 22 | + * |
| 23 | + * @param Schema $schema |
| 24 | + * @param AbstractPlatform $platform |
| 25 | + * @return array |
| 26 | + */ |
| 27 | + public function encodeSchema(Schema $schema, AbstractPlatform $platform): array { |
| 28 | + $encoded = ['tables' => [], 'sequences' => []]; |
| 29 | + foreach ($schema->getTables() as $table) { |
| 30 | + $encoded[$table->getName()] = $this->encodeTable($table, $platform); |
| 31 | + } |
| 32 | + ksort($encoded); |
| 33 | + return $encoded; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @psalm-type ColumnArrayType = |
| 38 | + */ |
| 39 | + private function encodeTable(Table $table, AbstractPlatform $platform): array { |
| 40 | + $encoded = ['columns' => [], 'indexes' => []]; |
| 41 | + foreach ($table->getColumns() as $column) { |
| 42 | + /** |
| 43 | + * @var array{ |
| 44 | + * name: string, |
| 45 | + * default: mixed, |
| 46 | + * notnull: bool, |
| 47 | + * length: ?int, |
| 48 | + * precision: int, |
| 49 | + * scale: int, |
| 50 | + * unsigned: bool, |
| 51 | + * fixed: bool, |
| 52 | + * autoincrement: bool, |
| 53 | + * comment: string, |
| 54 | + * columnDefinition: ?string, |
| 55 | + * collation?: string, |
| 56 | + * charset?: string, |
| 57 | + * jsonb?: bool, |
| 58 | + * } $data |
| 59 | + **/ |
| 60 | + $data = $column->toArray(); |
| 61 | + $data['type'] = Type::getTypeRegistry()->lookupName($column->getType()); |
| 62 | + $data['default'] = $column->getType()->convertToPHPValue($column->getDefault(), $platform); |
| 63 | + if ($platform instanceof PostgreSQLPlatform) { |
| 64 | + $data['unsigned'] = false; |
| 65 | + if ($column->getType() instanceof PhpIntegerMappingType) { |
| 66 | + $data['length'] = null; |
| 67 | + } |
| 68 | + unset($data['jsonb']); |
| 69 | + } elseif ($platform instanceof AbstractMySqlPlatform) { |
| 70 | + if ($column->getType() instanceof PhpIntegerMappingType) { |
| 71 | + $data['length'] = null; |
| 72 | + } elseif (in_array($data['type'], ['text', 'blob', 'datetime', 'float', 'json'])) { |
| 73 | + $data['length'] = 0; |
| 74 | + } |
| 75 | + unset($data['collation']); |
| 76 | + unset($data['charset']); |
| 77 | + } |
| 78 | + if ($data['type'] === 'string' && $data['length'] === null) { |
| 79 | + $data['length'] = 255; |
| 80 | + } |
| 81 | + $encoded['columns'][$column->getName()] = $data; |
| 82 | + } |
| 83 | + ksort($encoded['columns']); |
| 84 | + foreach ($table->getIndexes() as $index) { |
| 85 | + $options = $index->getOptions(); |
| 86 | + if (isset($options['lengths']) && count(array_filter($options['lengths'])) === 0) { |
| 87 | + unset($options['lengths']); |
| 88 | + } |
| 89 | + if ($index->isPrimary()) { |
| 90 | + if ($platform instanceof PostgreSqlPlatform) { |
| 91 | + $name = $table->getName() . '_pkey'; |
| 92 | + } elseif ($platform instanceof AbstractMySQLPlatform) { |
| 93 | + $name = "PRIMARY"; |
| 94 | + } else { |
| 95 | + $name = $index->getName(); |
| 96 | + } |
| 97 | + } else { |
| 98 | + $name = $index->getName(); |
| 99 | + } |
| 100 | + if ($platform instanceof PostgreSqlPlatform) { |
| 101 | + $name = strtolower($name); |
| 102 | + } |
| 103 | + $encoded['indexes'][$name] = [ |
| 104 | + 'name' => $name, |
| 105 | + 'columns' => $index->getColumns(), |
| 106 | + 'unique' => $index->isUnique(), |
| 107 | + 'primary' => $index->isPrimary(), |
| 108 | + 'flags' => $index->getFlags(), |
| 109 | + 'options' => $options, |
| 110 | + ]; |
| 111 | + } |
| 112 | + ksort($encoded['indexes']); |
| 113 | + return $encoded; |
| 114 | + } |
| 115 | +} |
0 commit comments