? domain_prefix.patch
Index: domain_prefix.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.install,v
retrieving revision 1.4.2.1
diff -u -p -r1.4.2.1 domain_prefix.install
--- domain_prefix.install	30 Mar 2008 20:42:13 -0000	1.4.2.1
+++ domain_prefix.install	25 Jun 2008 15:35:44 -0000
@@ -47,6 +47,11 @@ function domain_prefix_uninstall() {
     $name = db_escape_table('domain_'. $table['domain_id'] .'_'. $table['tablename']);
     db_query("DROP TABLE {%s}", $name);
   }
+  // If there are values in the sequences table, delete them.
+  $result = db_query("SELECT DISTINCT(domain_id) FROM {domain_prefix} WHERE status > 1");
+  while ($domain = db_fetch_array($result)) {
+    db_query("DELETE FROM {sequences} WHERE name LIKE '{%s%}'", db_escape_table('domain_'. $domain['domain_id']));
+  }  
   // Now drop the storage table.
   db_query("DROP TABLE {domain_prefix}");
   variable_del('domain_prefix');
Index: domain_prefix.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.module,v
retrieving revision 1.12.2.2
diff -u -p -r1.12.2.2 domain_prefix.module
--- domain_prefix.module	30 Mar 2008 20:42:13 -0000	1.12.2.2
+++ domain_prefix.module	25 Jun 2008 15:35:46 -0000
@@ -345,7 +345,7 @@ function domain_prefix_form($domain_id, 
   }
   // Get the root source table data.
   $root = domain_default();
-  if (empty($settings)) {
+  if (empty($settings) && !$_POST) {
     drupal_set_message(t('There are no default settings configured.'));
   }
 
@@ -353,7 +353,7 @@ function domain_prefix_form($domain_id, 
   $tables = domain_prefix_lookup($domain_id);
   $submit = t('Update domain tables');
   if (empty($tables)) {
-    if (!$form_values['execute'] && !$arguments['user_submitted']) {
+    if (!$_POST && !$form_values['execute'] && !$arguments['user_submitted']) {
       drupal_set_message(t('The table creation sequence has not run for this domain.'));
     }
     $submit = t('Generate domain tables');
@@ -543,6 +543,8 @@ function domain_prefix_form_submit($form
               db_query("CREATE TABLE {%s} LIKE {%s}", $newtable, $sourcetable);
               db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);
             }
+            // Update {sequences} table.
+            domain_prefix_update_sequences('update', $prefix, $sourcetable);
             if ($msg) {
               drupal_set_message(t('!string table copied.', array('!string' => $newtable)));
             }
@@ -556,6 +558,8 @@ function domain_prefix_form_submit($form
           if ($exists > 0) {
             db_query("TRUNCATE TABLE {%s}", $newtable);
             db_query("INSERT INTO {%s} SELECT * FROM {%s}", $newtable, $sourcetable);
+            // Update {sequences} table.
+            domain_prefix_update_sequences('update', $prefix, $sourcetable);
             if ($msg) {
               drupal_set_message(t('!string table updated from source.', array('!string' => $newtable)));
             }
@@ -568,6 +572,8 @@ function domain_prefix_form_submit($form
           if ($exists > 0) {
             db_query("DROP TABLE {%s}", $newtable);
             $value = DOMAIN_PREFIX_IGNORE;
+            // Update {sequences} table.
+            domain_prefix_update_sequences('drop', $prefix, $sourcetable);
             if ($msg) {
               drupal_set_message(t('!string table dropped.', array('!string' => $newtable)));
             }
@@ -588,6 +594,49 @@ function domain_prefix_form_submit($form
 }
 
 /**
+ * Correct the {sequences} table if doing an update.
+ *
+ * @param $op
+ * The operation to perform, either 'update' or 'drop.'
+ * @param $prefix
+ * The prefix for the table being updated.
+ * @param $newtable
+ * The name of the table being updated.
+ * @param $sourcetable
+ * The name of the table providing the tempalte for the update
+ */
+function domain_prefix_update_sequences($op, $prefix, $sourcetable) {
+  $result = db_query("SELECT name, id FROM {sequences} WHERE name LIKE '{%s%}'", $sourcetable);
+  $source = explode('_', domain_prefix_get_prefix() . $sourcetable);
+  $dbprefix = domain_prefix_get_prefix();
+  while ($variable = db_fetch_array($result)) {
+    // We have to match the variable source name, to prevent duplicates.
+    $var = explode('_', $variable['name']);
+    array_pop($var); // Toss out the last element.
+    $diff = array_diff($var, $source);
+    if (empty($diff)) {
+      // We must remove domain_# if it exists.
+      $pattern = array('/'. $dbprefix .'/', '/domain_[0-9]+_/');
+      $newvariable = $prefix . preg_replace($pattern, '', $variable['name']);      
+      $target = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{%s}'", $newvariable));
+      if (!$target) {
+        if ($op == 'update') {
+          db_query("INSERT INTO {sequences} (name, id) VALUES ('{%s}', %d)", $newvariable, $variable['id']);
+        }  
+      }
+      else {
+        if ($op == 'update') {      
+          db_query("UPDATE {sequences} SET id = %d WHERE name = '{%s}'", $variable['id'], $newvariable);
+        }
+        else {
+          db_query("DELETE FROM {sequences} WHERE name = '{%s}'", $newvariable);
+        }
+      }
+    }
+  }
+}
+
+/**
  * Derive a module name from the table name
  *
  * In future, SchemaAPI will do this for us.
