From 52d69a077f07ab147b3d678b452d25a56b94ac15 Mon Sep 17 00:00:00 2001
From: Pablo Cerda <pablo@citla.com>
Date: Thu, 28 Jun 2012 15:58:24 -0500
Subject: [PATCH] Fix the group audience float schema issue changid the gid to
 int type and updating all the existing group audience field
 instances via hook_updateN.


Signed-off-by: Pablo Cerda <pablo@citla.com>
---
 www/sites/all/modules/contrib/og/og.install |   31 ++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/www/sites/all/modules/contrib/og/og.install b/www/sites/all/modules/contrib/og/og.install
index 8ea77a2..b8c2b66 100644
--- a/www/sites/all/modules/contrib/og/og.install
+++ b/www/sites/all/modules/contrib/og/og.install
@@ -625,143 +625,172 @@ function og_schema_7000_info() {
 function og_field_schema($field) {
   $columns = array(
     'gid' => array(
       'description' => 'The group unique ID.',
-      'type' => 'float',
+      'type' => 'int',
       'unsigned' => TRUE,
       'not null' => FALSE,
     ),
     // This columns should be deprecated and removed, as the group membership
     // entity takes care of it. However, there is currently no way to remove
     // them.
     'state' => array(
       'description' => 'The state of the group content.',
       'type' => 'varchar',
       'length' => 255,
       'not null' => FALSE,
       'default' => '',
     ),
     'created' => array(
       'description' => 'The Unix timestamp when the group content was created.',
       'type' => 'int',
       'not null' => TRUE,
       'default' => 0,
     ),
   );
   return array(
     'columns' => $columns,
     'indexes' => array(
       'gid' => array('gid'),
     ),
     'foreign keys' => array(
       'og' => array(
         'table' => 'og',
         'columns' => array('gid' => 'gid'),
       ),
     ),
   );
 }
 
 /**
  * Upgrade from Organic groups 6 to 7.
  */
 function og_update_7000(&$sandbox) {
   if (db_field_exists('og', 'nid') && !db_table_exists('d6_og')) {
     // Rename the old table, so we can populate the new {og} table using API
     // functions, that assume the new DB structure.
     db_rename_table('og', 'd6_og');
     db_rename_table('og_ancestry', 'd6_og_ancestry');
     db_rename_table('og_uid', 'd6_og_uid');
 
     // Add serial ID to d6_og* tables so we can keep track of records that were
     // processed.
     foreach (array('d6_og', 'd6_og_ancestry', 'd6_og_uid') as $table) {
       // Drop the current primary key, as we are adding a serial column.
       db_drop_primary_key($table);
       db_add_field($table, 'upgrade_id', array(
         'description' => 'This a serial ID that keeps track of records that were processed.',
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
       ),
       array('primary key' => array('upgrade_id')));
     }
 
     $schema = og_schema_7000_info();
     foreach (array('og', 'og_role_permission', 'og_role', 'og_users_roles') as $table) {
       db_create_table($table, $schema[$table]);
     }
 
     // We can't call og_needs_migrate() directly, so just set the variable.
     variable_set('og_needs_migrate', TRUE);
     return t('Renamed Organic groups 6 tables. Enable Organic groups migrate module to continue the migration of data.');
   }
   return t('No change needed in Organic groups schema.');
 }
 
 /**
  * Add group membership entities instead of field data.
  */
 function og_update_7001(&$sandbox) {
   $schema = og_schema_info();
 
   // Due to changes in the upgrade path (e.g. moving data migration to
   // og-migrate) some users already have the tables, so be careful about the
   // creation of these tables.
   if (!db_table_exists('og_membership')) {
     db_create_table('og_membership', $schema['og_membership']);
   }
 
   if (!db_table_exists('og_membership_type')) {
     db_create_table('og_membership_type', $schema['og_membership_type']);
   }
   // We can't call og_needs_migrate() directly, so just set the variable.
   variable_set('og_needs_migrate', TRUE);
   return t('Organic groups added group membership tables. Enable Organic groups migrate module to continue the migration of data.');
 }
 
 /**
  * Add indexes to og and og_membership tables.
  */
 function og_update_7100(&$sandbox) {
   // Make sure we don't try to re-add an index that was added in previous
   // updates.
   if (!db_index_exists('og_membership', 'entity')) {
     db_add_index('og_membership', 'entity', array('etid', 'entity_type'));
   }
   if (!db_index_exists('og_membership', 'gid')) {
     db_add_index('og_membership', 'gid', array('gid'));
   }
   if (!db_index_exists('og', 'entity')) {
     db_add_index('og', 'entity', array('etid', 'entity_type'));
   }
   return t('Added table indexes for the following tables: @tables', array('@tables' => 'og, og_membership'));
 }
 
 /**
  * Rename the 'name' column to 'type' in {OG membership}.
  */
 function og_update_7101() {
   if (db_field_exists('og_membership', 'type')) {
     // Return early if field already created in og_update_7000().
     return;
   }
   $column = array(
     'description' => 'Reference to a group membership type.',
     'type' => 'varchar',
     'length' => 255,
     'not null' => TRUE,
     'default' => '',
   );
   db_change_field('og_membership', 'name', 'type', $column);
 }
 
 /**
  * Add default value function to all audience field instances.
  */
 function og_update_7102() {
   $instances = field_read_instances(array('field_name' => 'group_audience'));
   foreach ($instances as $instance) {
     $instance['default_value_function'] = 'og_field_audience_default_value';
     field_update_instance($instance);
   }
 }
+
+/**
+ * Fix the group_audience fields type that was created as float but should be
+ * int to avoid SQL exceptions in MSSQL and sqlite
+ */
+function og_update_7103() {
+  $field_instances = field_info_instances();
+  foreach ($field_instances as $entity_type => $bundle) {
+    foreach ($bundle as $bundle_type => $bundle_fields) {
+      foreach ($bundle_fields as $field_name => $field_info) {
+        if ($field_info['widget']['type'] == 'group_audience') {
+          $field = field_info_field_by_id($field_info['field_id']);
+          if ($field['storage']['type'] == 'field_sql_storage') {
+            $field_storage = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'];
+            foreach ($field_storage as $table => $field_storage_info) {
+              $new_field_schema = array(
+                'type' => 'int',
+                'unsigned' => TRUE,
+                'not null' => FALSE,
+              );
+
+              db_change_field($table, $field_storage_info['gid'], $field_storage_info['gid'], $new_field_schema);
+            }
+          }
+        }
+      }
+    }
+  }
+}
-- 
1.7.10.4

