Skip to content

Commit

Permalink
Make entries table always have a UNIQUE KEY
Browse files Browse the repository at this point in the history
On the `entry_id` column

Fixes #6
  • Loading branch information
nitriques committed May 18, 2017
1 parent 0f4fb92 commit e282645
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,16 @@ public function update($previousVersion=false) {
$textbox_fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', 'textbox');
foreach($textbox_fields as $field) {
$table = "tbl_entries_data_" . $field->get('id');
// Make sure we have an index on the handle
if ($this->updateHasColumn('text_handle', $table) && !$this->updateHasIndex('handle', $table)) {
$this->updateAddIndex('handle', $table);
}
// Handle length
$this->updateModifyColumn('handle', 'VARCHAR(1024)', $table);
// Make sure we have a unique key on `entry_id`
if ($this->updateHasColumn('entry_id', $table) && !$this->updateHasUniqueKey('entry_id', $table)) {
$this->updateAddUniqueKey('entry_id', $table);
}
}

return true;
Expand Down Expand Up @@ -193,6 +198,55 @@ public function updateHasIndex($index, $table) {
);
}

/**
* Add a new Unique Key. Note that this does not check to see if an
* unique key already exists and will remove any existing key on the column.
*
* @param string $column
* @param string $table
* @return boolean
*/
public function updateAddUniqueKey($column, $table = self::FIELD_TABLE) {
try {
Symphony::Database()->query("
ALTER TABLE
`$table`
DROP KEY
`$column`
");
} catch (Exception $ex) {
// ignore
}
return Symphony::Database()->query("
ALTER TABLE
`$table`
ADD UNIQUE KEY
`$column` (`$column`)
");
}

/**
* Check if the given `$table` has a unique key on `$column`.
*
* @param string $column
* @param string $table
* @return boolean
*/
public function updateHasUniqueKey($column, $table = self::FIELD_TABLE) {
$db = Symphony::Configuration()->get('database', 'db');
return (boolean)Symphony::Database()->fetchVar(
'CONSTRAINT_NAME', 0,
"
SELECT DISTINCT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = '$db' AND
CONSTRAINT_NAME = '$column' AND
table_name = '$table' AND
constraint_type = 'UNIQUE';
"
);
}

/**
* Add a new column to the settings table.
*
Expand Down
1 change: 1 addition & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<releases>
<release version="2.7.0" date="2017-05-18" min="2.6.0" max="2.x.x">
- Give option to make handle unique or not (default to unique as was before)
- Make sure all entries table always have a UNIQUE KEY on the `entry_id` column
</release>
<release version="2.6.2" date="2016-10-20" min="2.6.0" max="2.x.x">
- Fixed a bug where handles could be bigger than 255 chars
Expand Down

0 comments on commit e282645

Please sign in to comment.