From d90e9088d0ca7ffea0a9d5b1595f0547d69b4189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 10 Jan 2022 12:07:41 +0100 Subject: [PATCH 1/6] Fix primary key change in user_ldap migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use a backup table to copy the data, drop table and recreate it with correct primary key, then copy the data back and drop the backup table. Signed-off-by: Côme Chilliet --- .../Version1130Date20211102154716.php | 31 +++- .../Version1130Date20220110154717.php | 159 ++++++++++++++++++ .../Version1130Date20220110154718.php | 56 ++++++ 3 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 apps/user_ldap/lib/Migration/Version1130Date20220110154717.php create mode 100644 apps/user_ldap/lib/Migration/Version1130Date20220110154718.php diff --git a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php index 82afb4965deea..7ecf02d8f6fa3 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php @@ -103,11 +103,38 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); $changeSchema = true; } - if (!$table->hasPrimaryKey() || ($table->getPrimaryKeyColumns() !== ['owncloud_name'])) { - $table->dropPrimaryKey(); + if (!$table->hasPrimaryKey()) { $table->setPrimaryKey(['owncloud_name']); $changeSchema = true; } + + if ($table->getPrimaryKeyColumns() !== ['owncloud_name']) { + // We need to copy the table twice to be able to change primary key, prepare the backup table + $table2 = $schema->createTable('ldap_group_mapping_backup'); + $table2->addColumn('ldap_dn', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table2->addColumn('owncloud_name', Types::STRING, [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table2->addColumn('directory_uuid', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table2->addColumn('ldap_dn_hash', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table2->setPrimaryKey(['owncloud_name']); + $table2->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); + $table2->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); + $changeSchema = true; + } } } diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php new file mode 100644 index 0000000000000..e940747c81925 --- /dev/null +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php @@ -0,0 +1,159 @@ + + * + * @author Côme Chilliet + * + * @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 . + * + */ + +namespace OCA\User_LDAP\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1130Date20220110154717 extends SimpleMigrationStep { + + /** @var IDBConnection */ + private $dbc; + + public function __construct(IDBConnection $dbc) { + $this->dbc = $dbc; + } + + public function getName() { + return 'Copy ldap_group_mapping data to backup table and back if needed'; + } + + protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { + $insert = $this->dbc->getQueryBuilder(); + $insert->insert($destinationTable) + ->values([ + 'ldap_dn' => $insert->createParameter('ldap_dn'), + 'owncloud_name' => $insert->createParameter('owncloud_name'), + 'directory_uuid' => $insert->createParameter('directory_uuid'), + 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), + ]); + + $query = $this->dbc->getQueryBuilder(); + $query->select('*') + ->from($sourceTable); + + + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + $insert + ->setParameter('ldap_dn', $row['ldap_dn']) + ->setParameter('owncloud_name', $row['owncloud_name']) + ->setParameter('directory_uuid', $row['directory_uuid']) + ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) + ; + + $insert->executeStatement(); + } + $result->closeCursor(); + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @since 13.0.0 + */ + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('ldap_group_mapping_backup')) { + // Backup table does not exist + return; + } + + $output->startProgress(); + $this->copyGroupMappingData('ldap_group_mapping', 'ldap_group_mapping_backup'); + $output->finishProgress(); + } + + /** + * @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): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('ldap_group_mapping_backup')) { + // Backup table does not exist + return null; + } + + $schema->dropTable('ldap_group_mapping'); + $table = $schema->createTable('ldap_group_mapping'); + $table->addColumn('ldap_dn', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('owncloud_name', Types::STRING, [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('directory_uuid', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('ldap_dn_hash', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table->setPrimaryKey(['owncloud_name']); + $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); + $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('ldap_group_mapping_backup')) { + // Backup table does not exist + return; + } + + $output->startProgress(); + $this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping'); + $output->finishProgress(); + } +} diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php new file mode 100644 index 0000000000000..972a655387752 --- /dev/null +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php @@ -0,0 +1,56 @@ + + * + * @author Côme Chilliet + * + * @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 . + * + */ + +namespace OCA\User_LDAP\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1130Date20220110154718 extends SimpleMigrationStep { + public function getName() { + return 'Drop ldap_group_mapping_backup'; + } + + /** + * @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): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if ($schema->hasTable('ldap_group_mapping_backup')) { + $schema->dropTable('ldap_group_mapping_backup'); + return $schema; + } + + return null; + } +} From 03a570a5002d93015fcda028d190b1cd09eef438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 11 Jan 2022 16:16:02 +0100 Subject: [PATCH 2/6] Split dropTable and createTable in two migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is not possible to drop and create the same table in one migration Signed-off-by: Côme Chilliet --- .../Version1130Date20220110154717.php | 44 +-------- .../Version1130Date20220110154718.php | 89 +++++++++++++++++-- .../Version1130Date20220110154719.php | 56 ++++++++++++ 3 files changed, 141 insertions(+), 48 deletions(-) create mode 100644 apps/user_ldap/lib/Migration/Version1130Date20220110154719.php diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php index e940747c81925..492081a454346 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php @@ -43,7 +43,7 @@ public function __construct(IDBConnection $dbc) { } public function getName() { - return 'Copy ldap_group_mapping data to backup table and back if needed'; + return 'Copy ldap_group_mapping data to backup table if needed'; } protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { @@ -111,49 +111,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt } $schema->dropTable('ldap_group_mapping'); - $table = $schema->createTable('ldap_group_mapping'); - $table->addColumn('ldap_dn', Types::STRING, [ - 'notnull' => true, - 'length' => 255, - 'default' => '', - ]); - $table->addColumn('owncloud_name', Types::STRING, [ - 'notnull' => true, - 'length' => 64, - 'default' => '', - ]); - $table->addColumn('directory_uuid', Types::STRING, [ - 'notnull' => true, - 'length' => 255, - 'default' => '', - ]); - $table->addColumn('ldap_dn_hash', Types::STRING, [ - 'notnull' => false, - 'length' => 64, - ]); - $table->setPrimaryKey(['owncloud_name']); - $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); - $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); return $schema; } - - /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options - */ - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { - /** @var ISchemaWrapper $schema */ - $schema = $schemaClosure(); - - if (!$schema->hasTable('ldap_group_mapping_backup')) { - // Backup table does not exist - return; - } - - $output->startProgress(); - $this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping'); - $output->finishProgress(); - } } diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php index 972a655387752..6132d9060cb03 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php @@ -32,8 +32,44 @@ use OCP\Migration\SimpleMigrationStep; class Version1130Date20220110154718 extends SimpleMigrationStep { + /** @var IDBConnection */ + private $dbc; + + public function __construct(IDBConnection $dbc) { + $this->dbc = $dbc; + } + public function getName() { - return 'Drop ldap_group_mapping_backup'; + return 'Copy ldap_group_mapping data from backup table and if needed'; + } + + protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { + $insert = $this->dbc->getQueryBuilder(); + $insert->insert($destinationTable) + ->values([ + 'ldap_dn' => $insert->createParameter('ldap_dn'), + 'owncloud_name' => $insert->createParameter('owncloud_name'), + 'directory_uuid' => $insert->createParameter('directory_uuid'), + 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), + ]); + + $query = $this->dbc->getQueryBuilder(); + $query->select('*') + ->from($sourceTable); + + + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + $insert + ->setParameter('ldap_dn', $row['ldap_dn']) + ->setParameter('owncloud_name', $row['owncloud_name']) + ->setParameter('directory_uuid', $row['directory_uuid']) + ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) + ; + + $insert->executeStatement(); + } + $result->closeCursor(); } /** @@ -46,11 +82,54 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); - if ($schema->hasTable('ldap_group_mapping_backup')) { - $schema->dropTable('ldap_group_mapping_backup'); - return $schema; + if (!$schema->hasTable('ldap_group_mapping_backup')) { + // Backup table does not exist + return null; + } + + $table = $schema->createTable('ldap_group_mapping'); + $table->addColumn('ldap_dn', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('owncloud_name', Types::STRING, [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('directory_uuid', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('ldap_dn_hash', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table->setPrimaryKey(['owncloud_name']); + $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); + $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('ldap_group_mapping_backup')) { + // Backup table does not exist + return; } - return null; + $output->startProgress(); + $this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping'); + $output->finishProgress(); } } diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154719.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154719.php new file mode 100644 index 0000000000000..9e9ed38cb7052 --- /dev/null +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154719.php @@ -0,0 +1,56 @@ + + * + * @author Côme Chilliet + * + * @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 . + * + */ + +namespace OCA\User_LDAP\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1130Date20220110154719 extends SimpleMigrationStep { + public function getName() { + return 'Drop ldap_group_mapping_backup'; + } + + /** + * @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): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if ($schema->hasTable('ldap_group_mapping_backup')) { + $schema->dropTable('ldap_group_mapping_backup'); + return $schema; + } + + return null; + } +} From 66ca2925e7985bed851d940da4236f9470be202c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 11 Jan 2022 16:36:27 +0100 Subject: [PATCH 3/6] Move duplicated code to a base class for group_mapping migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet Co-authored-by: Joas Schilling <213943+nickvergessen@users.noreply.github.com> --- .../lib/Migration/GroupMappingMigration.php | 69 +++++++++++++++++++ .../Version1130Date20220110154717.php | 42 +---------- .../Version1130Date20220110154718.php | 40 +---------- 3 files changed, 72 insertions(+), 79 deletions(-) create mode 100644 apps/user_ldap/lib/Migration/GroupMappingMigration.php diff --git a/apps/user_ldap/lib/Migration/GroupMappingMigration.php b/apps/user_ldap/lib/Migration/GroupMappingMigration.php new file mode 100644 index 0000000000000..f89bebe57d65e --- /dev/null +++ b/apps/user_ldap/lib/Migration/GroupMappingMigration.php @@ -0,0 +1,69 @@ + + * + * @author Côme Chilliet + * + * @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 . + * + */ + +namespace OCA\User_LDAP\Migration; + +use OCP\IDBConnection; +use OCP\Migration\SimpleMigrationStep; + +abstract class GroupMappingMigration extends SimpleMigrationStep { + + /** @var IDBConnection */ + private $dbc; + + public function __construct(IDBConnection $dbc) { + $this->dbc = $dbc; + } + + protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { + $insert = $this->dbc->getQueryBuilder(); + $insert->insert($destinationTable) + ->values([ + 'ldap_dn' => $insert->createParameter('ldap_dn'), + 'owncloud_name' => $insert->createParameter('owncloud_name'), + 'directory_uuid' => $insert->createParameter('directory_uuid'), + 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), + ]); + + $query = $this->dbc->getQueryBuilder(); + $query->select('*') + ->from($sourceTable); + + + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + $insert + ->setParameter('ldap_dn', $row['ldap_dn']) + ->setParameter('owncloud_name', $row['owncloud_name']) + ->setParameter('directory_uuid', $row['directory_uuid']) + ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) + ; + + $insert->executeStatement(); + } + $result->closeCursor(); + } +} diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php index 492081a454346..2ffda4198c1e4 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154717.php @@ -28,53 +28,13 @@ use Closure; use OCP\DB\ISchemaWrapper; -use OCP\DB\Types; -use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\SimpleMigrationStep; - -class Version1130Date20220110154717 extends SimpleMigrationStep { - - /** @var IDBConnection */ - private $dbc; - - public function __construct(IDBConnection $dbc) { - $this->dbc = $dbc; - } +class Version1130Date20220110154717 extends GroupMappingMigration { public function getName() { return 'Copy ldap_group_mapping data to backup table if needed'; } - protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { - $insert = $this->dbc->getQueryBuilder(); - $insert->insert($destinationTable) - ->values([ - 'ldap_dn' => $insert->createParameter('ldap_dn'), - 'owncloud_name' => $insert->createParameter('owncloud_name'), - 'directory_uuid' => $insert->createParameter('directory_uuid'), - 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), - ]); - - $query = $this->dbc->getQueryBuilder(); - $query->select('*') - ->from($sourceTable); - - - $result = $query->executeQuery(); - while ($row = $result->fetch()) { - $insert - ->setParameter('ldap_dn', $row['ldap_dn']) - ->setParameter('owncloud_name', $row['owncloud_name']) - ->setParameter('directory_uuid', $row['directory_uuid']) - ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) - ; - - $insert->executeStatement(); - } - $result->closeCursor(); - } - /** * @param IOutput $output * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php index 6132d9060cb03..caadd9d9be238 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php @@ -28,50 +28,14 @@ use Closure; use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; use OCP\Migration\IOutput; -use OCP\Migration\SimpleMigrationStep; - -class Version1130Date20220110154718 extends SimpleMigrationStep { - /** @var IDBConnection */ - private $dbc; - - public function __construct(IDBConnection $dbc) { - $this->dbc = $dbc; - } +class Version1130Date20220110154718 extends GroupMappingMigration { public function getName() { return 'Copy ldap_group_mapping data from backup table and if needed'; } - protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { - $insert = $this->dbc->getQueryBuilder(); - $insert->insert($destinationTable) - ->values([ - 'ldap_dn' => $insert->createParameter('ldap_dn'), - 'owncloud_name' => $insert->createParameter('owncloud_name'), - 'directory_uuid' => $insert->createParameter('directory_uuid'), - 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), - ]); - - $query = $this->dbc->getQueryBuilder(); - $query->select('*') - ->from($sourceTable); - - - $result = $query->executeQuery(); - while ($row = $result->fetch()) { - $insert - ->setParameter('ldap_dn', $row['ldap_dn']) - ->setParameter('owncloud_name', $row['owncloud_name']) - ->setParameter('directory_uuid', $row['directory_uuid']) - ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) - ; - - $insert->executeStatement(); - } - $result->closeCursor(); - } - /** * @param IOutput $output * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` From e4235bdebaf623d6ba16abdb04f89a267ad8682d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 13 Jan 2022 12:20:57 +0100 Subject: [PATCH 4/6] Fix user_ldap migration for long DNs support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../Version1130Date20211102154716.php | 78 +++++++------------ .../Version1130Date20220110154718.php | 2 +- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php index 7ecf02d8f6fa3..b359f9fd3571b 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php @@ -73,11 +73,12 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $changeSchema = true; } $column = $table->getColumn('ldap_dn'); - if ($column->getLength() < 4096) { - $column->setLength(4096); - $changeSchema = true; - } if ($tableName === 'ldap_user_mapping') { + if ($column->getLength() < 4096) { + $column->setLength(4096); + $changeSchema = true; + } + if ($table->hasIndex('ldap_dn_users')) { $table->dropIndex('ldap_dn_users'); $changeSchema = true; @@ -91,50 +92,31 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $changeSchema = true; } } else { - if ($table->hasIndex('owncloud_name_groups')) { - $table->dropIndex('owncloud_name_groups'); - $changeSchema = true; - } - if (!$table->hasIndex('ldap_group_dn_hashes')) { - $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); - $changeSchema = true; - } - if (!$table->hasIndex('ldap_group_directory_uuid')) { - $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); - $changeSchema = true; - } - if (!$table->hasPrimaryKey()) { - $table->setPrimaryKey(['owncloud_name']); - $changeSchema = true; - } - - if ($table->getPrimaryKeyColumns() !== ['owncloud_name']) { - // We need to copy the table twice to be able to change primary key, prepare the backup table - $table2 = $schema->createTable('ldap_group_mapping_backup'); - $table2->addColumn('ldap_dn', Types::STRING, [ - 'notnull' => true, - 'length' => 255, - 'default' => '', - ]); - $table2->addColumn('owncloud_name', Types::STRING, [ - 'notnull' => true, - 'length' => 64, - 'default' => '', - ]); - $table2->addColumn('directory_uuid', Types::STRING, [ - 'notnull' => true, - 'length' => 255, - 'default' => '', - ]); - $table2->addColumn('ldap_dn_hash', Types::STRING, [ - 'notnull' => false, - 'length' => 64, - ]); - $table2->setPrimaryKey(['owncloud_name']); - $table2->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); - $table2->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); - $changeSchema = true; - } + // We need to copy the table twice to be able to change primary key, prepare the backup table + $table2 = $schema->createTable('ldap_group_mapping_backup'); + $table2->addColumn('ldap_dn', Types::STRING, [ + 'notnull' => true, + 'length' => 4096, + 'default' => '', + ]); + $table2->addColumn('owncloud_name', Types::STRING, [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table2->addColumn('directory_uuid', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table2->addColumn('ldap_dn_hash', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); + $table2->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); + $table2->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); + $changeSchema = true; } } diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php index caadd9d9be238..5f146ab06a5f2 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php @@ -54,7 +54,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->createTable('ldap_group_mapping'); $table->addColumn('ldap_dn', Types::STRING, [ 'notnull' => true, - 'length' => 255, + 'length' => 4096, 'default' => '', ]); $table->addColumn('owncloud_name', Types::STRING, [ From 9a9c8602e43789d63ba92d588ef77d87856c7f23 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 13 Jan 2022 14:22:30 +0100 Subject: [PATCH 5/6] Update autoloader Signed-off-by: Joas Schilling --- apps/user_ldap/composer/composer/autoload_classmap.php | 4 ++++ apps/user_ldap/composer/composer/autoload_static.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/apps/user_ldap/composer/composer/autoload_classmap.php b/apps/user_ldap/composer/composer/autoload_classmap.php index d670dab1ef773..f66bc5c0e76b2 100644 --- a/apps/user_ldap/composer/composer/autoload_classmap.php +++ b/apps/user_ldap/composer/composer/autoload_classmap.php @@ -53,6 +53,7 @@ 'OCA\\User_LDAP\\Mapping\\AbstractMapping' => $baseDir . '/../lib/Mapping/AbstractMapping.php', 'OCA\\User_LDAP\\Mapping\\GroupMapping' => $baseDir . '/../lib/Mapping/GroupMapping.php', 'OCA\\User_LDAP\\Mapping\\UserMapping' => $baseDir . '/../lib/Mapping/UserMapping.php', + 'OCA\\User_LDAP\\Migration\\GroupMappingMigration' => $baseDir . '/../lib/Migration/GroupMappingMigration.php', 'OCA\\User_LDAP\\Migration\\RemoveRefreshTime' => $baseDir . '/../lib/Migration/RemoveRefreshTime.php', 'OCA\\User_LDAP\\Migration\\SetDefaultProvider' => $baseDir . '/../lib/Migration/SetDefaultProvider.php', 'OCA\\User_LDAP\\Migration\\UUIDFix' => $baseDir . '/../lib/Migration/UUIDFix.php', @@ -63,6 +64,9 @@ 'OCA\\User_LDAP\\Migration\\Version1010Date20200630192842' => $baseDir . '/../lib/Migration/Version1010Date20200630192842.php', 'OCA\\User_LDAP\\Migration\\Version1120Date20210917155206' => $baseDir . '/../lib/Migration/Version1120Date20210917155206.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20211102154716' => $baseDir . '/../lib/Migration/Version1130Date20211102154716.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => $baseDir . '/../lib/Migration/Version1130Date20220110154717.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => $baseDir . '/../lib/Migration/Version1130Date20220110154718.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => $baseDir . '/../lib/Migration/Version1130Date20220110154719.php', 'OCA\\User_LDAP\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php', 'OCA\\User_LDAP\\PagedResults\\IAdapter' => $baseDir . '/../lib/PagedResults/IAdapter.php', 'OCA\\User_LDAP\\PagedResults\\Php73' => $baseDir . '/../lib/PagedResults/Php73.php', diff --git a/apps/user_ldap/composer/composer/autoload_static.php b/apps/user_ldap/composer/composer/autoload_static.php index 0dd682ffa7dd5..98520fc599351 100644 --- a/apps/user_ldap/composer/composer/autoload_static.php +++ b/apps/user_ldap/composer/composer/autoload_static.php @@ -68,6 +68,7 @@ class ComposerStaticInitUser_LDAP 'OCA\\User_LDAP\\Mapping\\AbstractMapping' => __DIR__ . '/..' . '/../lib/Mapping/AbstractMapping.php', 'OCA\\User_LDAP\\Mapping\\GroupMapping' => __DIR__ . '/..' . '/../lib/Mapping/GroupMapping.php', 'OCA\\User_LDAP\\Mapping\\UserMapping' => __DIR__ . '/..' . '/../lib/Mapping/UserMapping.php', + 'OCA\\User_LDAP\\Migration\\GroupMappingMigration' => __DIR__ . '/..' . '/../lib/Migration/GroupMappingMigration.php', 'OCA\\User_LDAP\\Migration\\RemoveRefreshTime' => __DIR__ . '/..' . '/../lib/Migration/RemoveRefreshTime.php', 'OCA\\User_LDAP\\Migration\\SetDefaultProvider' => __DIR__ . '/..' . '/../lib/Migration/SetDefaultProvider.php', 'OCA\\User_LDAP\\Migration\\UUIDFix' => __DIR__ . '/..' . '/../lib/Migration/UUIDFix.php', @@ -78,6 +79,9 @@ class ComposerStaticInitUser_LDAP 'OCA\\User_LDAP\\Migration\\Version1010Date20200630192842' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630192842.php', 'OCA\\User_LDAP\\Migration\\Version1120Date20210917155206' => __DIR__ . '/..' . '/../lib/Migration/Version1120Date20210917155206.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20211102154716' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20211102154716.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154717.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154718.php', + 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154719.php', 'OCA\\User_LDAP\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php', 'OCA\\User_LDAP\\PagedResults\\IAdapter' => __DIR__ . '/..' . '/../lib/PagedResults/IAdapter.php', 'OCA\\User_LDAP\\PagedResults\\Php73' => __DIR__ . '/..' . '/../lib/PagedResults/Php73.php', From 101338063ecc1c803268ec081d95b8ad2192d72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 13 Jan 2022 17:20:45 +0100 Subject: [PATCH 6/6] Remove useless indexes with duplicated names on backup table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/Migration/Version1130Date20211102154716.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php index b359f9fd3571b..8695f90ca65ba 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php @@ -114,8 +114,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'length' => 64, ]); $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); - $table2->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); - $table2->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); $changeSchema = true; } }