Skip to content

Commit

Permalink
Handle 4 byte database encodings, max key length 191 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
tomneedham committed Jun 5, 2017
1 parent 0c4cf74 commit 866d39d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
35 changes: 29 additions & 6 deletions core/Migrations/Version20170526104128.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,48 @@
class Version20170526104128 implements ISchemaMigration {

public function changeSchema(Schema $schema, array $options) {
// Get the table
$prefix = $options['tablePrefix'];
$table = $schema->getTable("{$prefix}account_terms");

// Check column length
if($table->getColumn('term')->getLength() === 255) {
if($table->getColumn('term')->getLength() === 191) {
// we don't need to adjust it
return;
}

// Need to shorten the column by one character
// Check if we have any terms taking up the full 256 chars (unlikely)
// Check if we have any terms taking up 192 or more chars (unlikely)

/** @var IDBConnection $db */
$db = \OC::$server->getDatabaseConnection();


$qb = $db->getQueryBuilder();
$qb->delete('account_terms')
->where($qb->expr()->gte($qb->expr()->length('term'), new Literal(256)));
$qb->execute();
$qb->select(['id', 'term'])
->from('account_terms')
->where($qb->expr()->gte($qb->expr()->length('term'), new Literal(192)));
$results = $qb->execute();

// Now shorten these terms
$db->beginTransaction();
while($longTerm = $results->fetch()) {
$qb->update('account_terms')
->where($qb->expr()->eq('id', $qb->createNamedParameter($longTerm['id'])))
->set('term', $qb->createNamedParameter($this->trimTerm($longTerm['term'])))
->execute();
}
$db->commit();

// Now update the column length
$table->getColumn('term')->setLength(255);
$table->getColumn('term')->setLength(191);
}

/**
* @param $longTerm
* @return string the shortened string ready for the new db column
*/
public function trimTerm($longTerm) {
return (string) substr($longTerm, 0, 191);
}
}
8 changes: 4 additions & 4 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@ public function getSearchTerms() {
* @since 10.0.1
*/
public function setSearchTerms(array $terms) {
// Check length of terms
$terms = array_filter($terms, function($term) {
return strlen($term) <= 255;
});
// Check length of terms, cut if too long
$terms = array_map(function($term) {
return substr($term, 0, 191);
}, $terms);
$this->mapper->setTermsForAccount($this->account->getId(), $terms);
}
}
2 changes: 1 addition & 1 deletion tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function testSettingAccountTerms(array $terms, array $expected) {
public function setTermsData() {
return [
'normal terms' => [['term1'], ['term1']],
'too long terms' => [['term1', str_repeat(".", 300)], ['term1']]
'too long terms' => [['term1', str_repeat(".", 192)], ['term1']]
];
}

Expand Down

0 comments on commit 866d39d

Please sign in to comment.