Skip to content

Commit

Permalink
Merge pull request #3024 from nextcloud/ref/database-schema
Browse files Browse the repository at this point in the history
Refactor database operations for commands, repair steps and migrations
  • Loading branch information
dartcafe authored Aug 14, 2023
2 parents c8d129c + 9fbfc75 commit b7727a8
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 261 deletions.
24 changes: 18 additions & 6 deletions lib/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,27 @@ protected function setInput(InputInterface $input): void {
$this->input = $input;
}

protected function printInfo(string $message): void {
$this->output->writeln('<info>' . $message . '</info>');
}

protected function printNewLine(): void {
$this->output->writeln('');
}

protected function printComment(string $message): void {
$this->output->writeln('<comment>' . $message. '</comment>');
protected function printInfo(string|array $messages, string $prefix = ''): void {
if (is_array($messages)) {
foreach ($messages as $message) {
$this->output->writeln('<info>' . $prefix . $message . '</info>');
}
return;
}
$this->output->writeln('<info>' . $prefix . $messages . '</info>');
}

protected function printComment(string|array $messages, string $prefix = ''): void {
if (is_array($messages)) {
foreach ($messages as $message) {
$this->output->writeln('<comment>' . $prefix . $message . '</comment>');
}
return;
}
$this->output->writeln('<comment>' . $prefix . $messages . '</comment>');
}
}
18 changes: 12 additions & 6 deletions lib/Command/Db/CleanMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace OCA\Polls\Command\Db;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\TableManager;
use OCP\IDBConnection;

class CleanMigrations extends Command {
protected string $name = parent::NAME_PREFIX . 'db:clean-migrations';
Expand All @@ -34,17 +36,21 @@ class CleanMigrations extends Command {
'NO data migration will be executed, so make sure you have a backup of your database.',
];

public function __construct(protected TableManager $tableManager) {
public function __construct(
private TableManager $tableManager,
private IDBConnection $connection,
private Schema $schema,
) {
parent::__construct();
}

protected function runCommands(): int {
// remove constraints and indices
$this->printComment('Remove migration entries from migration table');
// secure, that the schema is updated to the current status
$this->tableManager->refreshSchema();
$this->schema = $this->connection->createSchema();
$this->tableManager->setSchema($this->schema);
$this->tableManager->removeObsoleteMigrations();
$this->tableManager->migrate();

$this->printComment('Remove migration entries from migration table');
$this->connection->migrateToSchema($this->schema);

return 0;
}
Expand Down
22 changes: 12 additions & 10 deletions lib/Command/Db/CreateIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace OCA\Polls\Command\Db;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\IndexManager;
use OCP\IDBConnection;

class CreateIndices extends Command {
protected string $name = parent::NAME_PREFIX . 'index:create';
Expand All @@ -34,17 +36,22 @@ class CreateIndices extends Command {
'NO data migration will be executed, so make sure you have a backup of your database.',
];

public function __construct(private IndexManager $indexManager) {
public function __construct(
private IndexManager $indexManager,
private IDBConnection $connection,
private Schema $schema,
) {
parent::__construct();
}

protected function runCommands(): int {
// create indices and constraints
// secure, that the schema is updated to the current status
$this->indexManager->refreshSchema();
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$this->addForeignKeyConstraints();
$this->addIndices();
$this->indexManager->migrate();
$this->connection->migrateToSchema($this->schema);

return 0;
}
Expand All @@ -55,10 +62,7 @@ protected function runCommands(): int {
private function addForeignKeyConstraints(): void {
$this->printComment('Add foreign key constraints');
$messages = $this->indexManager->createForeignKeyConstraints();

foreach ($messages as $message) {
$this->printInfo(' ' . $message);
}
$this->printInfo($messages, ' - ');
}

/**
Expand All @@ -67,8 +71,6 @@ private function addForeignKeyConstraints(): void {
private function addIndices(): void {
$this->printComment('Add indices');
$messages = $this->indexManager->createIndices();
foreach ($messages as $message) {
$this->printInfo(' ' . $message);
}
$this->printInfo($messages, ' - ');
}
}
8 changes: 5 additions & 3 deletions lib/Command/Db/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
namespace OCA\Polls\Command\Db;

use OCA\Polls\Command\Command;
use OCA\Polls\Db\IndexManager;
use OCA\Polls\Db\TableManager;
use OCP\IDBConnection;

class Purge extends Command {
protected string $name = parent::NAME_PREFIX . 'db:purge';
Expand All @@ -40,14 +40,16 @@ class Purge extends Command {
];

public function __construct(
private IndexManager $indexManager,
private IDBConnection $connection,
private TableManager $tableManager
) {
parent::__construct();
}

protected function runCommands(): int {
$this->tableManager->purgeTables();
$this->tableManager->setConnection($this->connection);
$messages = $this->tableManager->purgeTables();
$this->printInfo($messages, ' - ');
return 0;
}
}
103 changes: 36 additions & 67 deletions lib/Command/Db/Rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

namespace OCA\Polls\Command\Db;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\TableManager;
use OCA\Polls\Db\IndexManager;
use OCA\Polls\Command\Command;
use OCP\IDBConnection;

class Rebuild extends Command {
protected string $name = parent::NAME_PREFIX . 'db:rebuild';
Expand All @@ -38,48 +40,44 @@ class Rebuild extends Command {
public function __construct(
private TableManager $tableManager,
private IndexManager $indexManager,
private IDBConnection $connection,
private Schema $schema,
) {
parent::__construct();
}

protected function runCommands(): int {
// remove constraints and indices
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$this->tableManager->setSchema($this->schema);

$this->printComment('Step 1. Remove all indices and foreign key constraints');
// secure, that the schema is updated to the current status
$this->indexManager->refreshSchema();
$this->deleteForeignKeyConstraints();
$this->deleteGenericIndices();
$this->deleteUniqueIndices();
$this->indexManager->migrate();

// remove old tables and columns
$this->printComment('Step 2. Remove all orphaned tables and columns');
// secure, that the schema is updated to the current status
$this->tableManager->refreshSchema();
$this->removeObsoleteTables();
$this->removeObsoleteColumns();
$this->tableManager->migrate();

// validate and fix/create current table layout
$this->connection->migrateToSchema($this->schema);

$this->printComment('Step 3. Create or update tables to current shema');
$this->createOrUpdateSchema();
$this->tableManager->migrate();

// validate and fix/create current table layout
$this->connection->migrateToSchema($this->schema);

$this->printComment('Step 4. set hashes for votes and options');
$this->migrateOptionsToHash();

// Remove orphaned records and duplicates
$this->printComment('Step 5. Remove invalid records (orphaned and duplicates)');
$this->cleanTables();

// recreate indices and constraints
$this->printComment('Step 6. Recreate indices and foreign key constraints');
// secure, that the schema is updated to the current status
$this->indexManager->refreshSchema();
$this->addForeignKeyConstraints();
$this->addIndices();
$this->indexManager->migrate();

$this->connection->migrateToSchema($this->schema);

return 0;
}
Expand All @@ -88,124 +86,95 @@ protected function runCommands(): int {
* add an on delete fk contraint to all tables referencing the main polls table
*/
private function addForeignKeyConstraints(): void {
$this->printComment('- Add foreign key constraints');
$this->printComment(' - Add foreign key constraints');
$messages = $this->indexManager->createForeignKeyConstraints();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Create index for $table
*/
private function addIndices(): void {
$this->printComment('- Add indices');
$this->printComment(' - Add indices');
$messages = $this->indexManager->createIndices();
foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Iterate over tables and make sure, the are created or updated
* according to the schema
*/
private function createOrUpdateSchema(): void {
$this->printComment('- Set db structure');
$this->printComment(' - Set db structure');
$messages = $this->tableManager->createTables();
foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Add or update hash for votes and options
*/
private function migrateOptionsToHash(): void {
$this->printComment('- add or update hashes');
$this->printComment(' - Add or update hashes');
$messages = $this->tableManager->migrateOptionsToHash();
foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

private function removeObsoleteColumns(): void {
$this->printComment('- Drop orphaned columns');
$this->printComment(' - Drop orphaned columns');
$messages = $this->tableManager->removeObsoleteColumns();
foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Remove obsolete tables if they still exist
*/
private function removeObsoleteTables(): void {
$this->printComment(' Drop orphaned tables');
$this->printComment(' - Drop orphaned tables');
$messages = $this->tableManager->removeObsoleteTables();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Initialize last poll interactions timestamps
*/
public function resetLastInteraction(): void {
$messages = $this->tableManager->resetLastInteraction();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* Remove obsolete tables if they still exist
*/
private function cleanTables(): void {
$this->printComment(' Remove orphaned records');
$this->printComment(' - Remove orphaned records');
$this->tableManager->removeOrphaned();

$this->printComment(' Remove duplicates');
$this->printComment(' - Remove duplicates');
$messages = $this->tableManager->deleteAllDuplicates();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

private function deleteForeignKeyConstraints(): void {
$this->printComment('- Remove foreign key constraints');
$this->printComment(' - Remove foreign key constraints');
$messages = $this->indexManager->removeAllForeignKeyConstraints();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* add an on delete fk contraint to all tables referencing the main polls table
*/
private function deleteGenericIndices(): void {
$this->printComment('- Remove generic indices');
$this->printComment(' - Remove generic indices');
$messages = $this->indexManager->removeAllGenericIndices();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}

/**
* add an on delete fk contraint to all tables referencing the main polls table
*/
private function deleteUniqueIndices(): void {
$this->printComment('- Remove unique indices');
$this->printComment(' - Remove unique indices');
$messages = $this->indexManager->removeAllUniqueIndices();

foreach ($messages as $message) {
$this->printInfo(' - ' . $message);
}
$this->printInfo($messages, ' ');
}
}
Loading

0 comments on commit b7727a8

Please sign in to comment.