Skip to content

Instantly share code, notes, and snippets.

@fabsor
Created January 13, 2014 13:41
Show Gist options
  • Save fabsor/8400396 to your computer and use it in GitHub Desktop.
Save fabsor/8400396 to your computer and use it in GitHub Desktop.
Metatag upgrade path
diff --git a/metatag.install b/metatag.install
index 777b265..1d83005 100644
--- a/metatag.install
+++ b/metatag.install
@@ -259,10 +259,12 @@ function metatag_update_7001() {
'description' => 'The primary identifier for a metatag configuration set.',
);
$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.
+ // Before making any changes, drop the existing primary key.
+ // Let's add a temporary unique key for cid so MySQL will let it go.
+ // Hint taken from https://drupal.org/node/2064305#comment-7753197.
+ db_add_unique_key($table_name, 'temp_key', array($field_name, 'instance'));
+ // 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);
}
@@ -272,6 +274,12 @@ function metatag_update_7001() {
// Rejig the field, and turn on the primary key again.
db_change_field($table_name, $field_name, $field_name, $field_spec, $keys);
+
+ // Finally, remove the temporary unique key because it's no longer userful.
+ db_drop_unique_key($table_name, 'temp_key');
+
+ // Finally, remove the temporary unique key because it's no longer userful.
+ db_drop_unique_key($table_name, 'temp_key');
}
/**
@@ -431,6 +439,10 @@ function metatag_update_7007() {
}
}
+function metatag_skip_update_7015() {
+ variable_set('metatag_skip_update_7015', TRUE);
+}
+
/**
* Remove any empty records that may be hanging around from old releases.
*/
@@ -452,6 +464,9 @@ function metatag_update_7008() {
*/
function metatag_update_7009() {
if (module_exists('taxonomy')) {
+ // Fix the metatag table.
+ metatag_update_7015();
+ metatag_skip_update_7015();
// Remove duplicates.
_metatag_remove_dupes('taxonomy_term');
}
@@ -468,6 +483,9 @@ function metatag_update_7009() {
* Fix {metatag} records for users.
*/
function metatag_update_7010() {
+ // Fix the metatag table.
+ metatag_update_7015();
+ metatag_skip_update_7015();
// Remove duplicates.
_metatag_remove_dupes('user');
@@ -482,6 +500,8 @@ function metatag_update_7010() {
* Fix {metatag} records for nodes.
*/
function metatag_update_7011(&$sandbox) {
+ metatag_update_7015();
+ metatag_skip_update_7015();
// Only proceed if Entity_Translation is not enabled as it allows each node
// record to have multiple languages available.
if (module_exists('entity_translation')) {
@@ -591,6 +611,8 @@ function metatag_update_7011(&$sandbox) {
* Remove duplicate {metatag} records for non-core entities.
*/
function metatag_update_7012() {
+ metatag_update_7015();
+ metatag_skip_update_7015();
if (module_exists('entity_translation')) {
drupal_set_message(t("Entity Translation is enabled, duplicate meta tags will not be removed for custom entities, to avoid accidental dataloss."));
return;
@@ -621,6 +643,8 @@ function metatag_update_7012() {
* take a while, depending on how much data needs to be converted.
*/
function metatag_update_7013(&$sandbox) {
+ metatag_update_7015();
+ metatag_skip_update_7015();
if (module_exists('entity_translation')) {
drupal_set_message(t("Entity Translation is enabled, meta tags will not be updated for custom entities, to avoid accidental dataloss."));
return;
@@ -912,26 +936,31 @@ function metatag_update_7014() {
* keys accordingly.
*/
function metatag_update_7015() {
- // Tell update 7016 that it isn't needed.
- variable_set('metatag_skip_update_7017', TRUE);
+ if (!variable_get('metatag_skip_update_7015', FALSE)) {
+ // Tell update 7016 that it isn't needed.
+ variable_set('metatag_skip_update_7017', TRUE);
- // Add the new field.
- db_add_field('metatag', 'revision_id', array(
- 'type' => 'int',
- 'unsigned' => TRUE,
- 'not null' => TRUE,
- 'default' => 0,
- 'description' => 'The revision_id for the entity object this data is attached to.',
- ));
+ // Add the new field.
+ db_add_field('metatag', 'revision_id', array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ 'description' => 'The revision_id for the entity object this data is attached to.',
+ ));
- // Remove the existing primary key. This may take some work so it can be
- // database agnostic, i.e. some databases will not like it.
- db_drop_primary_key('metatag');
+ // Remove the existing primary key. This may take some work so it can be
+ // database agnostic, i.e. some databases will not like it.
+ db_drop_primary_key('metatag');
- // Add the new primary key.
- db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
+ // Add the new primary key.
+ db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
- drupal_set_message(t('Added the {metatag}.revision_id field.'));
+ drupal_set_message(t('Added the {metatag}.revision_id field.'));
+ }
+ else {
+ variable_del('metatag_skip_update_7015');
+ }
}
/**
@@ -949,7 +978,7 @@ function metatag_update_7017() {
if (!variable_get('metatag_skip_update_7017', FALSE)) {
// Let's add a temporary unique key so MySQL will let it go.
db_add_unique_key('metatag', 'temp_key', array('entity_type', 'entity_id', 'revision_id', 'language'));
-
+
// Now remove the PK before changing a field from serial.
db_drop_primary_key('metatag');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment