Skip to content

Commit c36ae14

Browse files
authored
Merge pull request #30663 from nextcloud/backport/30568/stable23
[stable23] Fix primary key change in user_ldap migration
2 parents 1938c41 + 1013380 commit c36ae14

7 files changed

Lines changed: 337 additions & 21 deletions

apps/user_ldap/composer/composer/autoload_classmap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
'OCA\\User_LDAP\\Mapping\\AbstractMapping' => $baseDir . '/../lib/Mapping/AbstractMapping.php',
5454
'OCA\\User_LDAP\\Mapping\\GroupMapping' => $baseDir . '/../lib/Mapping/GroupMapping.php',
5555
'OCA\\User_LDAP\\Mapping\\UserMapping' => $baseDir . '/../lib/Mapping/UserMapping.php',
56+
'OCA\\User_LDAP\\Migration\\GroupMappingMigration' => $baseDir . '/../lib/Migration/GroupMappingMigration.php',
5657
'OCA\\User_LDAP\\Migration\\RemoveRefreshTime' => $baseDir . '/../lib/Migration/RemoveRefreshTime.php',
5758
'OCA\\User_LDAP\\Migration\\SetDefaultProvider' => $baseDir . '/../lib/Migration/SetDefaultProvider.php',
5859
'OCA\\User_LDAP\\Migration\\UUIDFix' => $baseDir . '/../lib/Migration/UUIDFix.php',
@@ -63,6 +64,9 @@
6364
'OCA\\User_LDAP\\Migration\\Version1010Date20200630192842' => $baseDir . '/../lib/Migration/Version1010Date20200630192842.php',
6465
'OCA\\User_LDAP\\Migration\\Version1120Date20210917155206' => $baseDir . '/../lib/Migration/Version1120Date20210917155206.php',
6566
'OCA\\User_LDAP\\Migration\\Version1130Date20211102154716' => $baseDir . '/../lib/Migration/Version1130Date20211102154716.php',
67+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => $baseDir . '/../lib/Migration/Version1130Date20220110154717.php',
68+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => $baseDir . '/../lib/Migration/Version1130Date20220110154718.php',
69+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => $baseDir . '/../lib/Migration/Version1130Date20220110154719.php',
6670
'OCA\\User_LDAP\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
6771
'OCA\\User_LDAP\\PagedResults\\IAdapter' => $baseDir . '/../lib/PagedResults/IAdapter.php',
6872
'OCA\\User_LDAP\\PagedResults\\Php73' => $baseDir . '/../lib/PagedResults/Php73.php',

apps/user_ldap/composer/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ComposerStaticInitUser_LDAP
6868
'OCA\\User_LDAP\\Mapping\\AbstractMapping' => __DIR__ . '/..' . '/../lib/Mapping/AbstractMapping.php',
6969
'OCA\\User_LDAP\\Mapping\\GroupMapping' => __DIR__ . '/..' . '/../lib/Mapping/GroupMapping.php',
7070
'OCA\\User_LDAP\\Mapping\\UserMapping' => __DIR__ . '/..' . '/../lib/Mapping/UserMapping.php',
71+
'OCA\\User_LDAP\\Migration\\GroupMappingMigration' => __DIR__ . '/..' . '/../lib/Migration/GroupMappingMigration.php',
7172
'OCA\\User_LDAP\\Migration\\RemoveRefreshTime' => __DIR__ . '/..' . '/../lib/Migration/RemoveRefreshTime.php',
7273
'OCA\\User_LDAP\\Migration\\SetDefaultProvider' => __DIR__ . '/..' . '/../lib/Migration/SetDefaultProvider.php',
7374
'OCA\\User_LDAP\\Migration\\UUIDFix' => __DIR__ . '/..' . '/../lib/Migration/UUIDFix.php',
@@ -78,6 +79,9 @@ class ComposerStaticInitUser_LDAP
7879
'OCA\\User_LDAP\\Migration\\Version1010Date20200630192842' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630192842.php',
7980
'OCA\\User_LDAP\\Migration\\Version1120Date20210917155206' => __DIR__ . '/..' . '/../lib/Migration/Version1120Date20210917155206.php',
8081
'OCA\\User_LDAP\\Migration\\Version1130Date20211102154716' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20211102154716.php',
82+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154717.php',
83+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154718.php',
84+
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154719.php',
8185
'OCA\\User_LDAP\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
8286
'OCA\\User_LDAP\\PagedResults\\IAdapter' => __DIR__ . '/..' . '/../lib/PagedResults/IAdapter.php',
8387
'OCA\\User_LDAP\\PagedResults\\Php73' => __DIR__ . '/..' . '/../lib/PagedResults/Php73.php',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
7+
*
8+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\User_LDAP\Migration;
28+
29+
use OCP\IDBConnection;
30+
use OCP\Migration\SimpleMigrationStep;
31+
32+
abstract class GroupMappingMigration extends SimpleMigrationStep {
33+
34+
/** @var IDBConnection */
35+
private $dbc;
36+
37+
public function __construct(IDBConnection $dbc) {
38+
$this->dbc = $dbc;
39+
}
40+
41+
protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void {
42+
$insert = $this->dbc->getQueryBuilder();
43+
$insert->insert($destinationTable)
44+
->values([
45+
'ldap_dn' => $insert->createParameter('ldap_dn'),
46+
'owncloud_name' => $insert->createParameter('owncloud_name'),
47+
'directory_uuid' => $insert->createParameter('directory_uuid'),
48+
'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'),
49+
]);
50+
51+
$query = $this->dbc->getQueryBuilder();
52+
$query->select('*')
53+
->from($sourceTable);
54+
55+
56+
$result = $query->executeQuery();
57+
while ($row = $result->fetch()) {
58+
$insert
59+
->setParameter('ldap_dn', $row['ldap_dn'])
60+
->setParameter('owncloud_name', $row['owncloud_name'])
61+
->setParameter('directory_uuid', $row['directory_uuid'])
62+
->setParameter('ldap_dn_hash', $row['ldap_dn_hash'])
63+
;
64+
65+
$insert->executeStatement();
66+
}
67+
$result->closeCursor();
68+
}
69+
}

apps/user_ldap/lib/Migration/Version1130Date20211102154716.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
7373
$changeSchema = true;
7474
}
7575
$column = $table->getColumn('ldap_dn');
76-
if ($column->getLength() < 4096) {
77-
$column->setLength(4096);
78-
$changeSchema = true;
79-
}
8076
if ($tableName === 'ldap_user_mapping') {
77+
if ($column->getLength() < 4096) {
78+
$column->setLength(4096);
79+
$changeSchema = true;
80+
}
81+
8182
if ($table->hasIndex('ldap_dn_users')) {
8283
$table->dropIndex('ldap_dn_users');
8384
$changeSchema = true;
@@ -91,23 +92,29 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
9192
$changeSchema = true;
9293
}
9394
} else {
94-
if ($table->hasIndex('owncloud_name_groups')) {
95-
$table->dropIndex('owncloud_name_groups');
96-
$changeSchema = true;
97-
}
98-
if (!$table->hasIndex('ldap_group_dn_hashes')) {
99-
$table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes');
100-
$changeSchema = true;
101-
}
102-
if (!$table->hasIndex('ldap_group_directory_uuid')) {
103-
$table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid');
104-
$changeSchema = true;
105-
}
106-
if (!$table->hasPrimaryKey() || ($table->getPrimaryKeyColumns() !== ['owncloud_name'])) {
107-
$table->dropPrimaryKey();
108-
$table->setPrimaryKey(['owncloud_name']);
109-
$changeSchema = true;
110-
}
95+
// We need to copy the table twice to be able to change primary key, prepare the backup table
96+
$table2 = $schema->createTable('ldap_group_mapping_backup');
97+
$table2->addColumn('ldap_dn', Types::STRING, [
98+
'notnull' => true,
99+
'length' => 4096,
100+
'default' => '',
101+
]);
102+
$table2->addColumn('owncloud_name', Types::STRING, [
103+
'notnull' => true,
104+
'length' => 64,
105+
'default' => '',
106+
]);
107+
$table2->addColumn('directory_uuid', Types::STRING, [
108+
'notnull' => true,
109+
'length' => 255,
110+
'default' => '',
111+
]);
112+
$table2->addColumn('ldap_dn_hash', Types::STRING, [
113+
'notnull' => false,
114+
'length' => 64,
115+
]);
116+
$table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary');
117+
$changeSchema = true;
111118
}
112119
}
113120

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
7+
*
8+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\User_LDAP\Migration;
28+
29+
use Closure;
30+
use OCP\DB\ISchemaWrapper;
31+
use OCP\Migration\IOutput;
32+
33+
class Version1130Date20220110154717 extends GroupMappingMigration {
34+
public function getName() {
35+
return 'Copy ldap_group_mapping data to backup table if needed';
36+
}
37+
38+
/**
39+
* @param IOutput $output
40+
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
41+
* @param array $options
42+
* @since 13.0.0
43+
*/
44+
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
45+
/** @var ISchemaWrapper $schema */
46+
$schema = $schemaClosure();
47+
48+
if (!$schema->hasTable('ldap_group_mapping_backup')) {
49+
// Backup table does not exist
50+
return;
51+
}
52+
53+
$output->startProgress();
54+
$this->copyGroupMappingData('ldap_group_mapping', 'ldap_group_mapping_backup');
55+
$output->finishProgress();
56+
}
57+
58+
/**
59+
* @param IOutput $output
60+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
61+
* @param array $options
62+
* @return null|ISchemaWrapper
63+
*/
64+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
65+
/** @var ISchemaWrapper $schema */
66+
$schema = $schemaClosure();
67+
68+
if (!$schema->hasTable('ldap_group_mapping_backup')) {
69+
// Backup table does not exist
70+
return null;
71+
}
72+
73+
$schema->dropTable('ldap_group_mapping');
74+
75+
return $schema;
76+
}
77+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
7+
*
8+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\User_LDAP\Migration;
28+
29+
use Closure;
30+
use OCP\DB\ISchemaWrapper;
31+
use OCP\DB\Types;
32+
use OCP\Migration\IOutput;
33+
34+
class Version1130Date20220110154718 extends GroupMappingMigration {
35+
public function getName() {
36+
return 'Copy ldap_group_mapping data from backup table and if needed';
37+
}
38+
39+
/**
40+
* @param IOutput $output
41+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
42+
* @param array $options
43+
* @return null|ISchemaWrapper
44+
*/
45+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
46+
/** @var ISchemaWrapper $schema */
47+
$schema = $schemaClosure();
48+
49+
if (!$schema->hasTable('ldap_group_mapping_backup')) {
50+
// Backup table does not exist
51+
return null;
52+
}
53+
54+
$table = $schema->createTable('ldap_group_mapping');
55+
$table->addColumn('ldap_dn', Types::STRING, [
56+
'notnull' => true,
57+
'length' => 4096,
58+
'default' => '',
59+
]);
60+
$table->addColumn('owncloud_name', Types::STRING, [
61+
'notnull' => true,
62+
'length' => 64,
63+
'default' => '',
64+
]);
65+
$table->addColumn('directory_uuid', Types::STRING, [
66+
'notnull' => true,
67+
'length' => 255,
68+
'default' => '',
69+
]);
70+
$table->addColumn('ldap_dn_hash', Types::STRING, [
71+
'notnull' => false,
72+
'length' => 64,
73+
]);
74+
$table->setPrimaryKey(['owncloud_name']);
75+
$table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes');
76+
$table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid');
77+
78+
return $schema;
79+
}
80+
81+
/**
82+
* @param IOutput $output
83+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
84+
* @param array $options
85+
*/
86+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
87+
/** @var ISchemaWrapper $schema */
88+
$schema = $schemaClosure();
89+
90+
if (!$schema->hasTable('ldap_group_mapping_backup')) {
91+
// Backup table does not exist
92+
return;
93+
}
94+
95+
$output->startProgress();
96+
$this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping');
97+
$output->finishProgress();
98+
}
99+
}

0 commit comments

Comments
 (0)