Skip to content

Commit

Permalink
Merge pull request #55 from ConductionNL/fix/migrations-for-merge
Browse files Browse the repository at this point in the history
Fix migrations for merge
  • Loading branch information
WilcoLouwerse authored Nov 12, 2024
2 parents 06a1aff + 693fa3d commit ec9854b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/Migration/Version0Date20240826193657.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->addColumn('version', Types::STRING, ['notnull' => true, 'length' => 255, 'default' => '0.0.1']);
$table->addColumn('synchronization_id', Types::STRING, ['notnull' => true, 'length' => 255]);
// Source
$table->addColumn('origin_id', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('origin_hash', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('source_id', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('source_hash', Types::STRING, ['notnull' => false, 'length' => 255]);
$table->addColumn('source_last_changed', Types::DATETIME, ['notnull' => false]);
$table->addColumn('source_last_checked', Types::DATETIME, ['notnull' => false]);
$table->addColumn('source_last_synced', Types::DATETIME, ['notnull' => false]);
Expand All @@ -212,9 +212,9 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->setPrimaryKey(['id']);
$table->addIndex(['uuid'], 'openconnector_sync_contracts_uuid_index');
$table->addIndex(['synchronization_id'], 'openconnector_sync_contracts_sync_index');
$table->addIndex(['origin_id'], 'openconnector_sync_contracts_origin_id_index');
$table->addIndex(['source_id'], 'openconnector_sync_contracts_source_id_index');
$table->addIndex(['target_id'], 'openconnector_sync_contracts_target_id_index');
$table->addIndex(['synchronization_id', 'origin_id'], 'openconnector_sync_contracts_sync_origin_index');
$table->addIndex(['synchronization_id', 'source_id'], 'openconnector_sync_contracts_sync_origin_index');
$table->addIndex(['synchronization_id', 'target_id'], 'openconnector_sync_contracts_sync_target_index');
}

Expand Down
117 changes: 117 additions & 0 deletions lib/Migration/Version1Date20241111144800.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenConnector\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use OCP\IDBConnection;

/**
* This migration changes the following:
* - Renaming of SynchronizationContract sourceId & sourceHash to originId and originHash,
* creating the new columns and transferring old data to the new fields.
* - Removal of old indexes related to sourceId and sourceHash
* - Addition of new indexes for originId and synchronization_id fields
*/
class Version1Date20241111144800 extends SimpleMigrationStep {

private IDBConnection $connection;

public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/**
* @var ISchemaWrapper $schema
*/
$schema = $schemaClosure();
$table = $schema->getTable('openconnector_synchronization_contracts');

// Step 1: Add new columns for 'origin_id' and 'origin_hash'
if ($table->hasColumn('origin_id') === false) {
$table->addColumn('origin_id', Types::STRING, [
'length' => 255,
'notnull' => true,
]);
}
if ($table->hasColumn('origin_hash') === false) {
$table->addColumn('origin_hash', Types::STRING, [
'length' => 255,
'notnull' => false,
]);
}

// Step 4: Adjust indexes in preparation for data migration
if ($table->hasIndex('openconnector_sync_contracts_source_id_index') === true) {
$table->dropIndex('openconnector_sync_contracts_source_id_index');
}
if ($table->hasIndex('openconnector_sync_contracts_origin_id_index') === false) {
$table->addIndex(['origin_id'], 'openconnector_sync_contracts_origin_id_index');
}

if ($table->hasIndex('openconnector_sync_contracts_sync_source_index') === true) {
$table->dropIndex('openconnector_sync_contracts_sync_source_index');
}
if ($table->hasIndex('openconnector_sync_contracts_sync_origin_index') === false) {
$table->addIndex(['synchronization_id', 'origin_id'], 'openconnector_sync_contracts_sync_origin_index');
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
/**
* @var ISchemaWrapper $schema
*/
$schema = $schemaClosure();
$table = $schema->getTable('openconnector_synchronization_contracts');

// Step 2: Copy data from old columns to new columns
if ($table->hasColumn('origin_id') === true && $table->hasColumn('origin_hash') === true
&& $table->hasColumn('source_id') === true && $table->hasColumn('source_hash') === true
) {
$this->connection->executeQuery("
UPDATE openconnector_synchronization_contracts
SET origin_id = source_id, origin_hash = source_hash
WHERE source_id IS NOT NULL
");
}

if ($table->hasColumn('source_id') === true) {
$table->dropColumn('source_id');
}
if ($table->hasColumn('source_hash') === true) {
$table->dropColumn('source_hash');
}
}
}

0 comments on commit ec9854b

Please sign in to comment.