Skip to content

Commit e282645

Browse files
committed
Make entries table always have a UNIQUE KEY
On the `entry_id` column Fixes #6
1 parent 0f4fb92 commit e282645

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

extension.driver.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,16 @@ public function update($previousVersion=false) {
147147
$textbox_fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', 'textbox');
148148
foreach($textbox_fields as $field) {
149149
$table = "tbl_entries_data_" . $field->get('id');
150+
// Make sure we have an index on the handle
150151
if ($this->updateHasColumn('text_handle', $table) && !$this->updateHasIndex('handle', $table)) {
151152
$this->updateAddIndex('handle', $table);
152153
}
153154
// Handle length
154155
$this->updateModifyColumn('handle', 'VARCHAR(1024)', $table);
156+
// Make sure we have a unique key on `entry_id`
157+
if ($this->updateHasColumn('entry_id', $table) && !$this->updateHasUniqueKey('entry_id', $table)) {
158+
$this->updateAddUniqueKey('entry_id', $table);
159+
}
155160
}
156161

157162
return true;
@@ -193,6 +198,55 @@ public function updateHasIndex($index, $table) {
193198
);
194199
}
195200

201+
/**
202+
* Add a new Unique Key. Note that this does not check to see if an
203+
* unique key already exists and will remove any existing key on the column.
204+
*
205+
* @param string $column
206+
* @param string $table
207+
* @return boolean
208+
*/
209+
public function updateAddUniqueKey($column, $table = self::FIELD_TABLE) {
210+
try {
211+
Symphony::Database()->query("
212+
ALTER TABLE
213+
`$table`
214+
DROP KEY
215+
`$column`
216+
");
217+
} catch (Exception $ex) {
218+
// ignore
219+
}
220+
return Symphony::Database()->query("
221+
ALTER TABLE
222+
`$table`
223+
ADD UNIQUE KEY
224+
`$column` (`$column`)
225+
");
226+
}
227+
228+
/**
229+
* Check if the given `$table` has a unique key on `$column`.
230+
*
231+
* @param string $column
232+
* @param string $table
233+
* @return boolean
234+
*/
235+
public function updateHasUniqueKey($column, $table = self::FIELD_TABLE) {
236+
$db = Symphony::Configuration()->get('database', 'db');
237+
return (boolean)Symphony::Database()->fetchVar(
238+
'CONSTRAINT_NAME', 0,
239+
"
240+
SELECT DISTINCT CONSTRAINT_NAME
241+
FROM information_schema.TABLE_CONSTRAINTS
242+
WHERE CONSTRAINT_SCHEMA = '$db' AND
243+
CONSTRAINT_NAME = '$column' AND
244+
table_name = '$table' AND
245+
constraint_type = 'UNIQUE';
246+
"
247+
);
248+
}
249+
196250
/**
197251
* Add a new column to the settings table.
198252
*

extension.meta.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<releases>
2424
<release version="2.7.0" date="2017-05-18" min="2.6.0" max="2.x.x">
2525
- Give option to make handle unique or not (default to unique as was before)
26+
- Make sure all entries table always have a UNIQUE KEY on the `entry_id` column
2627
</release>
2728
<release version="2.6.2" date="2016-10-20" min="2.6.0" max="2.x.x">
2829
- Fixed a bug where handles could be bigger than 255 chars

0 commit comments

Comments
 (0)