-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commands for exporting current and expected database schema
Signed-off-by: Robin Appelman <robin@icewind.nl>
- Loading branch information
1 parent
8ec5360
commit 6c467c3
Showing
8 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\Db; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use OC\Core\Command\Base; | ||
use OC\DB\Connection; | ||
use OC\DB\MigrationService; | ||
use OC\DB\SchemaWrapper; | ||
use OC\Migration\NullOutput; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ExpectedSchema extends Base { | ||
public function __construct( | ||
protected Connection $connection, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this | ||
->setName('db:schema:expected') | ||
->setDescription('Export the expected database schema for a fresh installation') | ||
->setHelp("Note that the expected schema might not exactly match the exported live schema as the expected schema doesn't take into account any database wide settings or defaults.") | ||
->addOption('sql', null, InputOption::VALUE_NONE, 'Dump the SQL statements for creating the expected schema'); | ||
parent::configure(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$schema = new Schema(); | ||
|
||
$this->applyMigrations('core', $schema); | ||
|
||
$apps = \OC_App::getEnabledApps(); | ||
foreach ($apps as $app) { | ||
$this->applyMigrations($app, $schema); | ||
} | ||
|
||
$sql = $input->getOption('sql'); | ||
if ($sql) { | ||
$output->writeln($schema->toSql($this->connection->getDatabasePlatform())); | ||
} else { | ||
$encoder = new SchemaEncoder(); | ||
$this->writeArrayInOutputFormat($input, $output, $encoder->encodeSchema($schema, $this->connection->getDatabasePlatform())); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function applyMigrations(string $app, Schema $schema): void { | ||
$output = new NullOutput(); | ||
$ms = new MigrationService($app, $this->connection, $output); | ||
foreach ($ms->getAvailableVersions() as $version) { | ||
$migration = $ms->createInstance($version); | ||
$migration->changeSchema($output, function() use (&$schema) { | ||
return new SchemaWrapper($this->connection, $schema); | ||
}, []); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\Db; | ||
|
||
use OC\Core\Command\Base; | ||
use OCP\IDBConnection; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ExportSchema extends Base { | ||
public function __construct( | ||
protected IDBConnection $connection, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this | ||
->setName('db:schema:export') | ||
->setDescription('Export the current database schema') | ||
->addOption('sql', null, InputOption::VALUE_NONE, 'Dump the SQL statements for creating a copy of the schema'); | ||
parent::configure(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$schema = $this->connection->createSchema(); | ||
$sql = $input->getOption('sql'); | ||
if ($sql) { | ||
$output->writeln($schema->toSql($this->connection->getDatabasePlatform())); | ||
} else { | ||
$encoder = new SchemaEncoder(); | ||
$this->writeArrayInOutputFormat($input, $output, $encoder->encodeSchema($schema, $this->connection->getDatabasePlatform())); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\Db; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Types\PhpIntegerMappingType; | ||
|
||
class SchemaEncoder { | ||
/** | ||
* Encode a DBAL schema to json, performing some normalization based on the database platform | ||
* | ||
* @param Schema $schema | ||
* @param AbstractPlatform $platform | ||
* @return array | ||
*/ | ||
public function encodeSchema(Schema $schema, AbstractPlatform $platform): array { | ||
$encoded = ['tables' => [], 'sequences' => []]; | ||
foreach ($schema->getTables() as $table) { | ||
$encoded[$table->getName()] = $this->encodeTable($table, $platform); | ||
} | ||
ksort($encoded); | ||
return $encoded; | ||
} | ||
|
||
private function encodeTable(Table $table, AbstractPlatform $platform): array { | ||
$encoded = ['columns' => [], 'indexes' => []]; | ||
if ($table->getName() === 'oc_filecache_extended') { | ||
$a = 1; | ||
} | ||
foreach ($table->getColumns() as $column) { | ||
$data = $column->toArray(); | ||
$data['type'] = $column->getType()->getName(); | ||
Check notice Code scanning / Psalm DeprecatedMethod Note
The method Doctrine\DBAL\Types\Type::getName has been marked as deprecated
|
||
$data['default'] = $column->getType()->convertToPHPValue($column->getDefault(), $platform); | ||
if ($platform instanceof PostgreSQLPlatform) { | ||
$data['unsigned'] = false; | ||
if ($column->getType() instanceof PhpIntegerMappingType) { | ||
$data['length'] = null; | ||
} | ||
unset($data['jsonb']); | ||
} elseif ($platform instanceof AbstractMySqlPlatform) { | ||
if ($column->getType() instanceof PhpIntegerMappingType) { | ||
$data['length'] = null; | ||
} elseif (in_array($data['type'], ['text', 'blob', 'datetime', 'float', 'json'])) { | ||
$data['length'] = 0; | ||
} | ||
unset($data['collation']); | ||
unset($data['charset']); | ||
} | ||
if ($data['type'] === 'string' && $data['length'] === null) { | ||
Check notice Code scanning / Psalm PossiblyUndefinedArrayOffset Note
Possibly undefined array key $data['length'] on array{default: mixed, length?: 0|mixed|null, type: string, unsigned?: false|mixed, ...<array-key, mixed>}
|
||
$data['length'] = 255; | ||
} | ||
$encoded['columns'][$column->getName()] = $data; | ||
} | ||
ksort($encoded['columns']); | ||
foreach ($table->getIndexes() as $index) { | ||
$options = $index->getOptions(); | ||
if (isset($options['lengths']) && count(array_filter($options['lengths'])) === 0) { | ||
unset($options['lengths']); | ||
} | ||
if ($index->isPrimary()) { | ||
if ($platform instanceof PostgreSqlPlatform) { | ||
$name = $table->getName() . '_pkey'; | ||
} elseif ($platform instanceof AbstractMySQLPlatform) { | ||
$name = "PRIMARY"; | ||
} else { | ||
$name = $index->getName(); | ||
} | ||
} else { | ||
$name = $index->getName(); | ||
} | ||
if ($platform instanceof PostgreSqlPlatform) { | ||
$name = strtolower($name); | ||
} | ||
$encoded['indexes'][$name] = [ | ||
'name' => $name, | ||
'columns' => $index->getColumns(), | ||
'unique' => $index->isUnique(), | ||
'primary' => $index->isPrimary(), | ||
'flags' => $index->getFlags(), | ||
'options' => $options, | ||
]; | ||
} | ||
ksort($encoded['indexes']); | ||
return $encoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters