Skip to content

Commit 24c4db9

Browse files
committed
Add oauth2_clients migration for Owncloud
Signed-off-by: Louis Chemineau <[email protected]>
1 parent 2ad566a commit 24c4db9

File tree

5 files changed

+132
-8
lines changed

5 files changed

+132
-8
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+
// Skip this migration if the 'identifier' column from ownCLoud is present, as the 'client_identifier' column would not exists.
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@
13561356
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
13571357
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => $baseDir . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
13581358
'OC\\Repair\\Owncloud\\InstallCoreBundle' => $baseDir . '/lib/private/Repair/Owncloud/InstallCoreBundle.php',
1359+
'OC\\Repair\\Owncloud\\MigrateOauthTables' => $baseDir . '/lib/private/Repair/Owncloud/MigrateOauthTables.php',
13591360
'OC\\Repair\\Owncloud\\MoveAvatars' => $baseDir . '/lib/private/Repair/Owncloud/MoveAvatars.php',
13601361
'OC\\Repair\\Owncloud\\MoveAvatarsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php',
13611362
'OC\\Repair\\Owncloud\\SaveAccountsTableData' => $baseDir . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php',

lib/composer/composer/autoload_static.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
88
{
99
public static $prefixLengthsPsr4 = array (
10-
'O' =>
10+
'O' =>
1111
array (
1212
'OC\\Core\\' => 8,
1313
'OC\\' => 3,
@@ -16,15 +16,15 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
1616
);
1717

1818
public static $prefixDirsPsr4 = array (
19-
'OC\\Core\\' =>
19+
'OC\\Core\\' =>
2020
array (
2121
0 => __DIR__ . '/../../..' . '/core',
2222
),
23-
'OC\\' =>
23+
'OC\\' =>
2424
array (
2525
0 => __DIR__ . '/../../..' . '/lib/private',
2626
),
27-
'OCP\\' =>
27+
'OCP\\' =>
2828
array (
2929
0 => __DIR__ . '/../../..' . '/lib/public',
3030
),
@@ -1385,6 +1385,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
13851385
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
13861386
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
13871387
'OC\\Repair\\Owncloud\\InstallCoreBundle' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/InstallCoreBundle.php',
1388+
'OC\\Repair\\Owncloud\\MigrateOauthTables' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MigrateOauthTables.php',
13881389
'OC\\Repair\\Owncloud\\MoveAvatars' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MoveAvatars.php',
13891390
'OC\\Repair\\Owncloud\\MoveAvatarsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php',
13901391
'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->get(Connection::class)),
188190
new FixMountStorages(\OC::$server->getDatabaseConnection()),
189191
new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
190192
new InstallCoreBundle(
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\Migration\IOutput;
24+
use OCP\Migration\IRepairStep;
25+
use OC\DB\Connection;
26+
use OC\DB\SchemaWrapper;
27+
use OCP\DB\QueryBuilder\IQueryBuilder;
28+
29+
class MigrateOauthTables implements IRepairStep {
30+
31+
/** @var Connection */
32+
protected $db;
33+
34+
/**
35+
* @param Connection $db
36+
*/
37+
public function __construct(Connection $db) {
38+
$this->db = $db;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getName() {
45+
return 'Migrate oauth2_clients table to nextcloud schema';
46+
}
47+
48+
public function run(IOutput $output) {
49+
$schema = new SchemaWrapper($this->db);
50+
if (!$schema->hasTable('oauth2_clients')) {
51+
$output->info("oauth2_clients table does not exist.");
52+
return;
53+
}
54+
55+
$output->info("Update the oauth2_access_tokens table schema.");
56+
$schema = new SchemaWrapper($this->db);
57+
$table = $schema->getTable('oauth2_access_tokens');
58+
$table->addColumn('hashed_code', 'string', [
59+
'notnull' => true,
60+
'length' => 128,
61+
]);
62+
$table->addColumn('encrypted_token', 'string', [
63+
'notnull' => true,
64+
'length' => 786,
65+
]);
66+
$table->addUniqueIndex(['hashed_code'], 'oauth2_access_hash_idx');
67+
$table->addIndex(['client_id'], 'oauth2_access_client_id_idx');
68+
69+
70+
$output->info("Update the oauth2_clients table schema.");
71+
$schema = new SchemaWrapper($this->db);
72+
$table = $schema->getTable('oauth2_clients');
73+
$table->getColumn('name')->setLength(64);
74+
$table->dropColumn('allow_subdomains');
75+
76+
if (!$schema->getTable('oauth2_clients')->hasColumn('client_identifier')) {
77+
$table->addColumn('client_identifier', 'string', [
78+
'notnull' => true,
79+
'length' => 64,
80+
'default' => ''
81+
]);
82+
$table->addIndex(['client_identifier'], 'oauth2_client_id_idx');
83+
}
84+
85+
$this->db->migrateToSchema($schema->getWrappedSchema());
86+
87+
88+
if ($schema->getTable('oauth2_clients')->hasColumn('identifier')) {
89+
$output->info("Move identifier column's data to the new client_identifier column.");
90+
// 1. Fetch all [id, identifier] couple.
91+
$selectQuery = $this->db->getQueryBuilder();
92+
$selectQuery->select('id', 'identifier')->from('oauth2_clients');
93+
$result = $selectQuery->executeQuery();
94+
$identifiers = $result->fetchAll();
95+
$result->closeCursor();
96+
97+
// 2. Insert them into the client_identifier column.
98+
foreach ($identifiers as ["id" => $id, "identifier" => $clientIdentifier]) {
99+
$insertQuery = $this->db->getQueryBuilder();
100+
$insertQuery->update('oauth2_clients')
101+
->set('client_identifier', $insertQuery->createNamedParameter($clientIdentifier, IQueryBuilder::PARAM_STR))
102+
->where($insertQuery->expr()->eq('id', $insertQuery->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
103+
->executeStatement();
104+
}
105+
106+
$output->info("Drop the identifier column.");
107+
$schema = new SchemaWrapper($this->db);
108+
$table = $schema->getTable('oauth2_clients');
109+
$table->dropColumn('identifier');
110+
$this->db->migrateToSchema($schema->getWrappedSchema());
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)