Skip to content

Commit 563d4b1

Browse files
committed
shrink ldap_dn column to 4000
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 21fbf1f commit 563d4b1

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\User_LDAP\Migration;
6+
7+
use Closure;
8+
use OCP\DB\ISchemaWrapper;
9+
use OCP\DB\QueryBuilder\IQueryBuilder;
10+
use OCP\IDBConnection;
11+
use OCP\Migration\IOutput;
12+
use OCP\Migration\SimpleMigrationStep;
13+
14+
/**
15+
* Auto-generated migration step: Please modify to your needs!
16+
*/
17+
class Version1141Date20220323143801 extends SimpleMigrationStep {
18+
19+
private IDBConnection $dbc;
20+
21+
public function __construct(IDBConnection $dbc) {
22+
$this->dbc = $dbc;
23+
}
24+
25+
/**
26+
* @param IOutput $output
27+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
28+
* @param array $options
29+
*/
30+
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
31+
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
32+
$dnsTooLong = [];
33+
$lengthExpr = $this->dbc->getDatabasePlatform()->getLengthExpression('ldap_dn');
34+
35+
$qb = $this->dbc->getQueryBuilder();
36+
$qb->select('ldap_dn')
37+
->from($tableName)
38+
->where($qb->expr()->gt($qb->createFunction($lengthExpr), '255', IQueryBuilder::PARAM_INT));
39+
40+
$result = $qb->executeQuery();
41+
while($dn = $result->fetchOne()) {
42+
if(mb_strlen($dn) > 4000) {
43+
$dnsTooLong = [$dn];
44+
}
45+
}
46+
$result->closeCursor();
47+
$this->shortenDNs($dnsTooLong, $tableName);
48+
}
49+
}
50+
51+
protected function shortenDNs(array $dns, string $table): void {
52+
$qb = $this->dbc->getQueryBuilder();
53+
$qb->update($table)
54+
->set('ldap_dn', $qb->createParameter('shortenedDn'))
55+
->where($qb->expr()->eq('ldap_dn', $qb->createParameter('originalDn')));
56+
57+
$pageSize = 1000;
58+
$page = 0;
59+
do {
60+
$subset = array_slice($dns, $page * $pageSize, $pageSize);
61+
try {
62+
$this->dbc->beginTransaction();
63+
foreach ($subset as $dn) {
64+
$shortenedDN = mb_substr($dn, 0, 4000);
65+
$qb->setParameter('shortenedDn', $shortenedDN);
66+
$qb->setParameter('originalDn', $dn);
67+
$qb->executeStatement();
68+
}
69+
$this->dbc->commit();
70+
} catch (\Throwable $t) {
71+
$this->dbc->rollBack();
72+
throw $t;
73+
}
74+
$page++;
75+
} while (count($subset) === $pageSize);
76+
}
77+
78+
/**
79+
* @param IOutput $output
80+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
81+
* @param array $options
82+
* @return null|ISchemaWrapper
83+
*/
84+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
85+
/** @var ISchemaWrapper $schema */
86+
$schema = $schemaClosure();
87+
88+
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
89+
$table = $schema->getTable($tableName);
90+
$column = $table->getColumn('ldap_dn');
91+
if ($column->getLength() > 4000) {
92+
$column->setLength(4000);
93+
}
94+
}
95+
96+
return $schema;
97+
}
98+
}

0 commit comments

Comments
 (0)