From 4da6081786693ffe9fba76ccac87b23a4ce63976 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 7 Aug 2025 18:51:56 +0200 Subject: [PATCH] feat: add table filter to schema:export and schema:expected Signed-off-by: Robin Appelman --- core/Command/Db/ExpectedSchema.php | 15 +++++++++++++++ core/Command/Db/ExportSchema.php | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/core/Command/Db/ExpectedSchema.php b/core/Command/Db/ExpectedSchema.php index 1f35daba089..e8635b1117d 100644 --- a/core/Command/Db/ExpectedSchema.php +++ b/core/Command/Db/ExpectedSchema.php @@ -14,6 +14,7 @@ use OC\DB\Connection; use OC\DB\MigrationService; use OC\DB\SchemaWrapper; use OC\Migration\NullOutput; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -30,12 +31,14 @@ class ExpectedSchema extends Base { ->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.") + ->addArgument('table', InputArgument::OPTIONAL, 'Only show the schema for the specified table') ->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(); + $onlyTable = $input->getArgument('table'); $this->applyMigrations('core', $schema); @@ -44,6 +47,18 @@ class ExpectedSchema extends Base { $this->applyMigrations($app, $schema); } + if ($onlyTable) { + $tablesToDrop = []; + foreach ($schema->getTables() as $table) { + if ($table->getName() !== $onlyTable) { + $tablesToDrop[] = $table->getName(); + } + } + foreach ($tablesToDrop as $table) { + $schema->dropTable($table); + } + } + $sql = $input->getOption('sql'); if ($sql) { $output->writeln($schema->toSql($this->connection->getDatabasePlatform())); diff --git a/core/Command/Db/ExportSchema.php b/core/Command/Db/ExportSchema.php index 581824eea5f..1ea0a95d2e6 100644 --- a/core/Command/Db/ExportSchema.php +++ b/core/Command/Db/ExportSchema.php @@ -10,6 +10,7 @@ namespace OC\Core\Command\Db; use OC\Core\Command\Base; use OCP\IDBConnection; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -25,6 +26,7 @@ class ExportSchema extends Base { $this ->setName('db:schema:export') ->setDescription('Export the current database schema') + ->addArgument('table', InputArgument::OPTIONAL, 'Only show the schema for the specified table') ->addOption('sql', null, InputOption::VALUE_NONE, 'Dump the SQL statements for creating a copy of the schema'); parent::configure(); } @@ -32,6 +34,20 @@ class ExportSchema extends Base { protected function execute(InputInterface $input, OutputInterface $output): int { $schema = $this->connection->createSchema(); $sql = $input->getOption('sql'); + $onlyTable = $input->getArgument('table'); + + if ($onlyTable) { + $tablesToDrop = []; + foreach ($schema->getTables() as $table) { + if ($table->getName() !== $onlyTable) { + $tablesToDrop[] = $table->getName(); + } + } + foreach ($tablesToDrop as $table) { + $schema->dropTable($table); + } + } + if ($sql) { $output->writeln($schema->toSql($this->connection->getDatabasePlatform())); } else {