@@ -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 *
0 commit comments