Skip to content

Commit

Permalink
Convert Unique to PK for compatibility with Percona XtraDB
Browse files Browse the repository at this point in the history
This PR makes two changes:
1. Modifies an old migration changing a unique ID on the `collres_accesscache` table to a Primary key
2. Creates a new migration for installations which have already run the previous migration to sync them up

I realize that it is an unusual step to modify an old migration, but in this case it is necessary. [NextCloud upgrades are failing with Percona XtraDB](#16311) due to a missing primary key on this table. Because the breakage occurs in an existing migration, the fix must also occur in the existing migration. To ensure that we do not cause any existing installations to diverge, this commit also introduces a new migration that looks for the non-PK version of the table and updates those as well. See #16311 for additional details.

Signed-off-by: Ben Klang <bklang@powerhrg.com>
  • Loading branch information
bklang committed May 11, 2020
1 parent 8910a1f commit eaf0d1d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/Migrations/Version16000Date20190207141427.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'length' => 64,
]);

$table->addUniqueIndex(['collection_id', 'resource_type', 'resource_id'], 'collres_unique_res');
$table->setPrimaryKey(['collection_id', 'resource_type', 'resource_id'], 'collres_unique_res');
}

if (!$schema->hasTable('collres_accesscache')) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'default' => 0,
]);

$table->addUniqueIndex(['user_id', 'collection_id', 'resource_type', 'resource_id'], 'collres_unique_user');
$table->setPrimaryKey(['user_id', 'collection_id', 'resource_type', 'resource_id'], 'collres_unique_user');
$table->addIndex(['user_id', 'resource_type', 'resource_id'], 'collres_user_res');
$table->addIndex(['user_id', 'collection_id'], 'collres_user_coll');
}
Expand Down
64 changes: 64 additions & 0 deletions core/Migrations/Version20000Date20200511105313.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Ben Klang <bklang@powerhrg.com>
*
* @author Ben Klang <bklang@powerhrg.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Core\Migrations;

use Closure;
use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version20000Date20200511105313 extends SimpleMigrationStep {


/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if ($schema->hasTable('collres_accesscache') && !$schema->getTable('collres_accesscache')->hasPrimaryKey()) {
// Converts a unique key to a primary key for compatibility with Percona XtraDB
// See https://github.com/nextcloud/server/issues/16311
// Step 1: Drop the old index
$schema->getTable('collres_accesscache')->dropIndex('collres_unique_user');
}

if ($schema->hasTable('collres_resources') && !$schema->getTable('collres_resources')->hasPrimaryKey()) {
// Converts a unique key to a primary key for compatibility with Percona XtraDB
// See https://github.com/nextcloud/server/issues/16311
// Step 1: Drop the old index
$schema->getTable('collres_resources')->dropIndex('collres_unique_res');
}

return $schema;
}
}
64 changes: 64 additions & 0 deletions core/Migrations/Version20000Date20200511105314.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Ben Klang <bklang@powerhrg.com>
*
* @author Ben Klang <bklang@powerhrg.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Core\Migrations;

use Closure;
use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version20000Date20200511105314 extends SimpleMigrationStep {


/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if ($schema->hasTable('collres_accesscache') && !$schema->getTable('collres_accesscache')->hasPrimaryKey()) {
// Converts a unique key to a primary key for compatibility with Percona XtraDB
// See https://github.com/nextcloud/server/issues/16311
// Step 2: Re-create the index as a Primary Key
$schema->getTable('collres_accesscache')->setPrimaryKey(['user_id', 'collection_id', 'resource_type', 'resource_id'], 'collres_unique_user');
}

if ($schema->hasTable('collres_resources') && !$schema->getTable('collres_resources')->hasPrimaryKey()) {
// Converts a unique key to a primary key for compatibility with Percona XtraDB
// See https://github.com/nextcloud/server/issues/16311
// Step 2: Re-create the index as a Primary Key
$schema->getTable('collres_resources')->setPrimaryKey(['collection_id', 'resource_type', 'resource_id'], 'collres_unique_res');
}

return $schema;
}
}

0 comments on commit eaf0d1d

Please sign in to comment.