Skip to content

Commit

Permalink
Migration to fix term column length
Browse files Browse the repository at this point in the history
  • Loading branch information
tomneedham committed May 26, 2017
1 parent c69ebf2 commit c78cba7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/Migrations/Version20170516100103.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function changeSchema(Schema $schema, array $options) {

$table->addColumn('term', Type::STRING, [
'notnull' => true,
'length' => 256
'length' => 255
]);

$table->setPrimaryKey(['id']);
Expand Down
33 changes: 33 additions & 0 deletions core/Migrations/Version20170526104128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace OC\Migrations;

use Doctrine\DBAL\Schema\Schema;
use OC\DB\QueryBuilder\Literal;
use OCP\IDBConnection;
use OCP\Migration\ISchemaMigration;

/**
* Update term column length to ensure index creation works on all db setups
*/
class Version20170526104128 implements ISchemaMigration {

public function changeSchema(Schema $schema, array $options) {
$prefix = $options['tablePrefix'];
$table = $schema->getTable("{$prefix}account_terms");
// Check column length
if($table->getColumn('term')->getLength() === 255) {
// 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)

/** @var IDBConnection $db */
$db = \OC::$server->getDatabaseConnection();
$db->executeQuery("DELETE FROM {$prefix}account_terms WHERE CHAR_LENGTH(term) >= 256;");

// Now update the column length
$table->getColumn('term')->setLength(255);
}
}
4 changes: 4 additions & 0 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +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;
});
$this->mapper->setTermsForAccount($this->account->getId(), $terms);
}
}
26 changes: 26 additions & 0 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,30 @@ public function testGetCloudId() {
->willReturn('http://localhost:8888/owncloud');
$this->assertEquals("foo@localhost:8888/owncloud", $this->user->getCloudId());
}

/**
* @dataProvider setTermsData
* @param array $terms
* @param array $expected
*/
public function testSettingAccountTerms(array $terms, array $expected) {
$account = $this->getMockBuilder(Account::class)->getMock();
$account->expects($this->once())->method('__call')->with('getId')->willReturn('foo');

$this->accountMapper->expects($this->once())
->method('setTermsForAccount')
->with('foo', $expected);

// Call the method
$user = new User($account, $this->accountMapper, null, $this->config);
$user->setSearchTerms($terms);
}

public function setTermsData() {
return [
'normal terms' => [['term1'], ['term1']],
'too long terms' => [['term1', str_repeat(".", 300)], ['term1']]
];
}

}

0 comments on commit c78cba7

Please sign in to comment.