Skip to content

Commit ae95a7c

Browse files
committed
Add oauth2_clients migration for Owncloud
Signed-off-by: Louis Chemineau <[email protected]>
1 parent 5613ff3 commit ae95a7c

File tree

6 files changed

+235
-4
lines changed

6 files changed

+235
-4
lines changed

apps/oauth2/lib/Migration/Version010402Date20190107124745.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
4242
/** @var ISchemaWrapper $schema */
4343
$schema = $schemaClosure();
4444

45-
$table = $schema->getTable('oauth2_clients');
46-
$table->dropIndex('oauth2_client_id_idx');
47-
$table->addUniqueIndex(['client_identifier'], 'oauth2_client_id_idx');
48-
return $schema;
45+
// The identifier column comes from Owncloud, skip this migration as long as the repair step as not been ran to rename the column.
46+
if ($schema->getTable('oauth2_clients')->hasColumn('identifier')) {
47+
return;
48+
}
49+
50+
if (!$schema->getTable('oauth2_clients')->hasIndex('oauth2_client_id_idx')) {
51+
$table = $schema->getTable('oauth2_clients');
52+
$table->dropIndex('oauth2_client_id_idx');
53+
$table->addUniqueIndex(['client_identifier'], 'oauth2_client_id_idx');
54+
return $schema;
55+
}
4956
}
5057
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,10 +1349,12 @@
13491349
'OC\\Repair\\NC21\\ValidatePhoneNumber' => $baseDir . '/lib/private/Repair/NC21/ValidatePhoneNumber.php',
13501350
'OC\\Repair\\NC22\\LookupServerSendCheck' => $baseDir . '/lib/private/Repair/NC22/LookupServerSendCheck.php',
13511351
'OC\\Repair\\OldGroupMembershipShares' => $baseDir . '/lib/private/Repair/OldGroupMembershipShares.php',
1352+
'OC\\Repair\\Owncloud\\AddIdentifierIndexForOauth2' => $baseDir . '/lib/private/Repair/Owncloud/AddIdentifierIndexForOauth2.php',
13521353
'OC\\Repair\\Owncloud\\CleanPreviews' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviews.php',
13531354
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
13541355
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => $baseDir . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
13551356
'OC\\Repair\\Owncloud\\InstallCoreBundle' => $baseDir . '/lib/private/Repair/Owncloud/InstallCoreBundle.php',
1357+
'OC\\Repair\\Owncloud\\MigrateOauthTables' => $baseDir . '/lib/private/Repair/Owncloud/MigrateOauthTables.php',
13561358
'OC\\Repair\\Owncloud\\MoveAvatars' => $baseDir . '/lib/private/Repair/Owncloud/MoveAvatars.php',
13571359
'OC\\Repair\\Owncloud\\MoveAvatarsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php',
13581360
'OC\\Repair\\Owncloud\\SaveAccountsTableData' => $baseDir . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,12 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
13781378
'OC\\Repair\\NC21\\ValidatePhoneNumber' => __DIR__ . '/../../..' . '/lib/private/Repair/NC21/ValidatePhoneNumber.php',
13791379
'OC\\Repair\\NC22\\LookupServerSendCheck' => __DIR__ . '/../../..' . '/lib/private/Repair/NC22/LookupServerSendCheck.php',
13801380
'OC\\Repair\\OldGroupMembershipShares' => __DIR__ . '/../../..' . '/lib/private/Repair/OldGroupMembershipShares.php',
1381+
'OC\\Repair\\Owncloud\\AddIdentifierIndexForOauth2' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/AddIdentifierIndexForOauth2.php',
13811382
'OC\\Repair\\Owncloud\\CleanPreviews' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviews.php',
13821383
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
13831384
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
13841385
'OC\\Repair\\Owncloud\\InstallCoreBundle' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/InstallCoreBundle.php',
1386+
'OC\\Repair\\Owncloud\\MigrateOauthTables' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MigrateOauthTables.php',
13851387
'OC\\Repair\\Owncloud\\MoveAvatars' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MoveAvatars.php',
13861388
'OC\\Repair\\Owncloud\\MoveAvatarsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php',
13871389
'OC\\Repair\\Owncloud\\SaveAccountsTableData' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php',

lib/private/Repair.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use OC\Repair\MoveUpdaterStepFile;
4848
use OC\Repair\NC22\LookupServerSendCheck;
4949
use OC\Repair\Owncloud\CleanPreviews;
50+
use OC\Repair\Owncloud\MigrateOauthTables;
5051
use OC\Repair\NC11\FixMountStorages;
5152
use OC\Repair\Owncloud\MoveAvatars;
5253
use OC\Repair\Owncloud\InstallCoreBundle;
@@ -185,6 +186,7 @@ public static function getRepairSteps() {
185186
\OC::$server->getUserManager(),
186187
\OC::$server->getConfig()
187188
),
189+
new MigrateOauthTables(\OC::$server->getDatabaseConnection()),
188190
new FixMountStorages(\OC::$server->getDatabaseConnection()),
189191
new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
190192
new InstallCoreBundle(
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Louis Chemineau <[email protected]>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
namespace OC\Repair\Owncloud;
22+
23+
use OCP\BackgroundJob\IJobList;
24+
use OCP\IConfig;
25+
use OCP\IUserManager;
26+
use OCP\Migration\IOutput;
27+
use OCP\Migration\IRepairStep;
28+
use OCP\IDBConnection;
29+
use Doctrine\DBAL\Types\Types;
30+
31+
class AddIdentifierIndexForOauth2 implements IRepairStep {
32+
33+
/** @var IDBConnection */
34+
protected $db;
35+
36+
/**
37+
* MoveAvatars constructor.
38+
*
39+
* @param IJobList $jobList
40+
* @param IUserManager $userManager
41+
* @param IConfig $config
42+
*/
43+
public function __construct(IDBConnection $db) {
44+
$this->db = $db;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getName() {
51+
return 'Migrate oauth_clients table to nextcloud schema';
52+
}
53+
54+
public function run(IOutput $output) {
55+
$tableName = 'oauth_clients';
56+
$oldIdentifierColumnName = 'identifier';
57+
$newIdentifierColumnName = 'client_identifier';
58+
59+
if (!$this->db->tableExists($tableName)) {
60+
return;
61+
}
62+
63+
// Update the oauth_client table schema.
64+
$schema = $this->db->createSchema();
65+
$table = $schema->getTable($tableName);
66+
$table->getColumn('name')->setLength(64);
67+
$table->dropColumn('allow_subdomains');
68+
$table->addColumn($newIdentifierColumnName, 'string', [
69+
'notnull' => true,
70+
'length' => 64,
71+
]);
72+
$table->addUniqueIndex([$newIdentifierColumnName], 'oauth2_client_id_idx');
73+
$this->db->migrateToSchema($schema);
74+
75+
// Move identifier column's data to the new client_identifier column.
76+
// 1. Fetch all [id, identifier] couple.
77+
$qb = $this->db->getQueryBuilder();
78+
$qb->select('id', $oldIdentifierColumnName)->from($tableName);
79+
$result = $qb->executeQuery();
80+
$identifiers = $result->fetchAll();
81+
$result->closeCursor();
82+
83+
// 2. Insert them into the client_identifier column.
84+
$qb = $this->db->getQueryBuilder();
85+
foreach ($identifiers as [$id, $clientIdentifier]) {
86+
$qb->update($tableName)
87+
->set($newIdentifierColumnName, $clientIdentifier)
88+
->where($qb->expr()->eq('id', $id));
89+
}
90+
$result = $qb->executeQuery();
91+
$result->closeCursor();
92+
93+
// Drop the identifier column.
94+
$schema = $this->db->createSchema();
95+
$table = $schema->getTable($tableName);
96+
$table->dropColumn($oldIdentifierColumnName);
97+
$this->db->migrateToSchema($schema);
98+
}
99+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Louis Chemineau <[email protected]>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
namespace OC\Repair\Owncloud;
22+
23+
use OCP\BackgroundJob\IJobList;
24+
use OCP\IConfig;
25+
use OCP\IUserManager;
26+
use OCP\Migration\IOutput;
27+
use OCP\Migration\IRepairStep;
28+
use OCP\IDBConnection;
29+
use OC\DB\SchemaWrapper;
30+
31+
class MigrateOauthTables implements IRepairStep {
32+
33+
/** @var IDBConnection */
34+
protected $db;
35+
36+
/**
37+
* MoveAvatars constructor.
38+
*
39+
* @param IJobList $jobList
40+
* @param IUserManager $userManager
41+
* @param IConfig $config
42+
*/
43+
public function __construct(IDBConnection $db) {
44+
$this->db = $db;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getName() {
51+
return 'Migrate oauth2_clients table to nextcloud schema';
52+
}
53+
54+
public function run(IOutput $output) {
55+
$schema = new SchemaWrapper($this->db);
56+
if (!$schema->hasTable('oauth2_clients')) {
57+
$output->info("oauth2_clients table does not exists.");
58+
return;
59+
}
60+
61+
$output->info("Update the oauth2_access_tokens table schema.");
62+
$schema = new SchemaWrapper($this->db);
63+
$table = $schema->getTable('oauth2_access_tokens');
64+
$table->addColumn('hashed_code', 'string', [
65+
'notnull' => true,
66+
'length' => 128,
67+
]);
68+
$table->addColumn('encrypted_token', 'string', [
69+
'notnull' => true,
70+
'length' => 786,
71+
]);
72+
$table->addUniqueIndex(['hashed_code'], 'oauth2_access_hash_idx');
73+
$table->addIndex(['client_id'], 'oauth2_access_client_id_idx');
74+
75+
76+
$output->info("Update the oauth2_clients table schema.");
77+
$schema = new SchemaWrapper($this->db);
78+
$table = $schema->getTable('oauth2_clients');
79+
$table->getColumn('name')->setLength(64);
80+
$table->dropColumn('allow_subdomains');
81+
82+
if (!$schema->getTable('oauth2_clients')->hasColumn('client_identifier')) {
83+
$table->addColumn('client_identifier', 'string', [
84+
'notnull' => true,
85+
'length' => 64,
86+
'default' => ''
87+
]);
88+
$table->addIndex(['client_identifier'], 'oauth2_client_id_idx');
89+
}
90+
91+
$this->db->migrateToSchema($schema->getWrappedSchema());
92+
93+
94+
if ($schema->getTable('oauth2_clients')->hasColumn('identifier')) {
95+
$output->info("Move identifier column's data to the new client_identifier column.");
96+
// 1. Fetch all [id, identifier] couple.
97+
$qb = $this->db->getQueryBuilder();
98+
$qb->select('id', 'identifier')->from('oauth2_clients');
99+
$result = $qb->execute();
100+
$identifiers = $result->fetchAll();
101+
$result->closeCursor();
102+
103+
// 2. Insert them into the client_identifier column.
104+
foreach ($identifiers as ["id" => $id, "identifier" => $clientIdentifier]) {
105+
$this->db->getQueryBuilder()
106+
->update('oauth2_clients')
107+
->set('client_identifier', $clientIdentifier)
108+
->where($qb->expr()->eq('id', $id))
109+
->execute();
110+
}
111+
112+
$output->info("Drop the identifier column.");
113+
$schema = new SchemaWrapper($this->db);
114+
$table = $schema->getTable('oauth2_clients');
115+
$table->dropColumn('identifier');
116+
$this->db->migrateToSchema($schema->getWrappedSchema());
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)