Index: views.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/views.install,v
retrieving revision 1.50
diff -u -p -r1.50 views.install
--- views.install	30 Jun 2009 19:28:44 -0000	1.50
+++ views.install	10 Mar 2010 23:28:38 -0000
@@ -5,6 +5,9 @@
  * Contains install and update functions for Views.
  */
 
+/**
+ * Implementation of hook_install().
+ */
 function views_install() {
   if ($GLOBALS['db_type'] == 'pgsql') {
     db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
@@ -15,23 +18,83 @@ function views_install() {
   db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
 }
 
+/**
+ * Implementation of hook_uninstall().
+ */
 function views_uninstall() {
   drupal_uninstall_schema('views');
 }
 
 /**
- * Implementation of hook_schemea
+ * Implementation of hook_schema().
+ *
+ * Generate the current version of the database schema from
+ * the sequence of schema update functions. Uses a similar
+ * method to install.inc's drupal_get_schema_versions() to
+ * establish the update sequence.
+ *
+ * To change the schema, add a new views_schema_N()
+ * function to match the associated views_update_N()
+ *
+ * @param $caller_function
+ *   The name of the function that called us.
+ *   Used internally, if requesting a specific schema version.
  */
-function views_schema() {
-  // Currently, schema 1 is the only schema we have. As we make updates,
-  // we might either create a new schema 2 or make adjustments here.
-  return views_schema_1();
+function views_schema($caller_function = FALSE) {
+  static $get_current;
+  static $schemas = array();
+
+  // If called with no arguments, get the latest version of the schema.
+  if (!isset($get_current)) {
+    $get_current = $caller_function ? FALSE : TRUE;
+  }
+
+  // Generate a sorted list of available schema update functions.
+  if ($get_current || empty($schemas)) {
+    $get_current = FALSE;
+    $functions = get_defined_functions();
+    foreach ($functions['user'] as $function) {
+      if (strpos($function, 'views_schema_') === 0) {
+        $version = substr($function, strlen('views_schema_'));
+        if (is_numeric($version)) {
+          $schemas[] = $version;
+        }
+      }
+    }
+    if ($schemas) {
+      sort($schemas, SORT_NUMERIC);
+
+      // If a specific version was requested, drop any later
+      // updates from the sequence.
+      if ($caller_function) {
+        do {
+          $schema = array_pop($schemas);
+        } while ($schemas && $caller_function != 'views_schema_'. $schema);
+      }
+    }
+  }
+
+  // Call views_schema_<n>, for the highest available <n>.
+  if ($schema = array_pop($schemas)) {
+    $function = 'views_schema_'. $schema;
+    return $function();
+  }
+
+  return array();
 }
 
 /**
- * Views 2's initial schema; separated for the purposes of updates.
+ * Views 2's initial schema.
+ * Called directly by views_update_6000() for updates from Drupal 5.
+ *
+ * Important: Do not edit this schema!
+ *
+ * Updates to the views schema must be provided as views_schema_6xxx() functions,
+ * which views_schema() automatically sees and applies. See below for examples.
+ *
+ * Please do document updates with comments in this function, however.
  */
-function views_schema_1() {
+function views_schema_6000() {
   $schema['views_view'] = array(
     'description' => 'Stores the general data for a view.',
     'fields' => array(
@@ -67,7 +130,7 @@ function views_schema_1() {
       ),
       'base_table' => array(
         'type' => 'varchar',
-        'length' => '64',
+        'length' => '32', // Updated to '64' in views_schema_6005()
         'default' => '',
         'not null' => TRUE,
         'description' => 'What table this view is based on, such as node, user, comment, or term.',
@@ -80,7 +143,7 @@ function views_schema_1() {
       ),
     ),
     'primary key' => array('vid'),
-    'unique keys' => array('name' => array('name')),
+    'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
   );
 
   $schema['views_display'] = array(
@@ -131,11 +194,6 @@ function views_schema_1() {
   );
 
   $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
-  
-  $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
-  $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
-  $schema['cache_views_data']['fields']['serialized']['default'] = 1;
-
 
   $schema['views_object_cache'] = array(
     'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
@@ -163,8 +221,7 @@ function views_schema_1() {
         'description' => 'The time this cache was created or updated.',
       ),
       'data' => array(
-        'type' => 'text',
-        'size' => 'big',
+        'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
         'description' => 'Serialized data being stored.',
         'serialize' => TRUE,
       ),
@@ -174,6 +231,9 @@ function views_schema_1() {
       'updated' => array('updated'),
     ),
   );
+
+  // $schema['cache_views_data'] added in views_schema_6006()
+
   return $schema;
 }
 
@@ -194,9 +254,9 @@ function views_update_6000() {
   }
 
   // This is mostly the same as drupal_install_schema, but it forces
-  // views_schema_1 rather than the default schema. This will be important
-  // if we have table updates.
-  $schema = views_schema_1();
+  // views_schema_6000() rather than the default views_schema().
+  // This is important for processing subsequent table updates.
+  $schema = views_schema_6000();
   _drupal_initialize_schema('views', $schema);
 
   foreach ($schema as $name => $table) {
@@ -205,6 +265,9 @@ function views_update_6000() {
   return $ret;
 }
 
+/**
+ * Remove '$' symbol in special blocks, as it is invalid for theming.
+ */
 function views_update_6001() {
   $ret = array();
   $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'");
@@ -218,14 +281,20 @@ function views_update_6001() {
 }
 
 // NOTE: Update 6002 removed because it did not always work.
+// Update 6004 implements the change correctly.
+
 /**
  * Add missing unique key.
  */
+function views_schema_6003() {
+  $schema = views_schema(__FUNCTION__);
+  $schema['views_view']['unique keys'] = array('name' => array('name'));
+  unset($schema['views_view']['unique key']);
+  return $schema;
+}
 function views_update_6003() {
   $ret = array();
-
   db_add_unique_key($ret, 'views_view', 'name', array('name'));
-
   return $ret;
 }
 
@@ -233,6 +302,12 @@ function views_update_6003() {
  * Enlarge the views_object_cache.data column to prevent truncation and JS
  * errors.
  */
+function views_schema_6004() {
+  $schema = views_schema(__FUNCTION__);
+  $schema['views_object_cache']['fields']['data']['type'] = 'text';
+  $schema['views_object_cache']['fields']['data']['size'] = 'big';
+  return $schema;
+}
 function views_update_6004() {
   $ret = array();
 
@@ -254,6 +329,11 @@ function views_update_6004() {
 /**
  * Enlarge the base_table column
  */
+function views_schema_6005() {
+  $schema = views_schema(__FUNCTION__);
+  $schema['views_view']['fields']['base_table']['length'] = 64;
+  return $schema;
+}
 function views_update_6005() {
   $ret = array();
   
@@ -271,6 +351,13 @@ function views_update_6005() {
 /**
  * Add the cache_views_data table to support standard caching.
  */
+function views_schema_6006() {
+  $schema = views_schema(__FUNCTION__);
+  $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
+  $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
+  $schema['cache_views_data']['fields']['serialized']['default'] = 1;
+  return $schema;
+}
 function views_update_6006() {
   $ret = array();
   
