diff --git a/metatag.install b/metatag.install index 6ff3292..31af61a 100644 --- a/metatag.install +++ b/metatag.install @@ -230,14 +230,33 @@ function metatag_update_7001() { ); $keys = array('primary key' => array($field_name)); - // Before making any changes, drop the existing primary key. Unforunately - // there is no API way to check if a primary key exists, so if it doesn't - // exist the db_drop_primary_key() call will fail. - try { - db_drop_primary_key($table_name); + // Before making any changes, drop the existing primary key. This is a little + // hackish as there's no Drupal API to identify whether a table already has a + // primary key. + $db_class = get_class(db_query("SELECT entity_id FROM {metatag}")); + // MySQL. + if ($db_class == 'DatabaseConnection_mysql') { + $indices = db_query("SHOW indexes FROM {metatag_config} WHERE key_name = 'PRIMARY'"); + } + // PostgreSQL. + elseif ($db_class == 'DatabaseConnection_pgsql') { + $indices = db_query("SELECT c.column_name, c.data_type + FROM information_schema.table_constraints tc + JOIN information_schema.constraint_column_usage AS ccu + USING (constraint_schema, constraint_name) + JOIN information_schema.columns AS c + ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name + WHERE constraint_type = 'PRIMARY KEY' and tc.table_name = {metatag_config}"); + } + // A different database. Hopefully no sites were using the original Metatags + // module on something other than MySQL or PostgreSQL. + else { + $indices = NULL; } - catch (Exception $e) { - drupal_set_message('Caught an exception: ', $e->getMessage()); + + // If ither the $indices query was not ran + if (is_null($indices) || $indices->getCount() > 0) { + db_drop_primary_key($table_name); } // Rejig the field, and turn on the primary key again.