? domain_prefix_seq.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	22 Jun 2008 15:29:47 -0000
@@ -46,6 +46,8 @@ function domain_prefix_uninstall() {
   while ($table = db_fetch_array($result)) {
     $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.
+    db_query("DELETE FROM {sequences} WHERE name LIKE '%s%'", $name);
   }
   // Now drop the storage table.
   db_query("DROP TABLE {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	22 Jun 2008 15:29:48 -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.'));
   }
 
@@ -516,6 +516,7 @@ function domain_prefix_form_submit($form
         $exists = domain_prefix_table_exists($prefix, $key);
         $oldtable = db_escape_table($key);
         $sourcetable = db_escape_table($source_prefix . $key);
+        $drop_sequence = FALSE;
         if ($value == DOMAIN_PREFIX_CREATE) {
           if (!$exists) {
             if ($db_type == 'pgsql') {
@@ -543,6 +544,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($prefix, $newtable, $sourcetable);
             if ($msg) {
               drupal_set_message(t('!string table copied.', array('!string' => $newtable)));
             }
@@ -556,6 +559,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($prefix, $newtable, domain_prefix_get_prefix() . $sourcetable);
             if ($msg) {
               drupal_set_message(t('!string table updated from source.', array('!string' => $newtable)));
             }
@@ -568,6 +573,7 @@ function domain_prefix_form_submit($form
           if ($exists > 0) {
             db_query("DROP TABLE {%s}", $newtable);
             $value = DOMAIN_PREFIX_IGNORE;
+            $drop_sequence = TRUE;
             if ($msg) {
               drupal_set_message(t('!string table dropped.', array('!string' => $newtable)));
             }
@@ -582,12 +588,42 @@ function domain_prefix_form_submit($form
           $value = $current[$oldtable]['status'];
         }
         db_query("INSERT INTO {domain_prefix} (domain_id, status, tablename, module, source) VALUES (%d, %d, '%s', '%s', %d)", $form_values['domain_id'], $value, $key, $module, $form_values['_source_'. $key]);
+        if ($drop_sequence) {
+          // If there are values in the sequences table, delete them.
+          db_query("DELETE FROM {sequences} WHERE name LIKE '%s%'", $prefix);
+        }
       }
     }
   }
 }
 
 /**
+ * Correct the {sequences} table if doing an update.
+ *
+ * @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($prefix, $newtable, $sourcetable) {
+  $result = db_query("SELECT name, id FROM {sequences} WHERE name LIKE '%s%'", $sourcetable);
+  while ($variable = db_fetch_array($result)) {
+    $tablename = str_replace($prefix, '', $newtable);
+    $source = substr($variable['name'], 0, strpos($sourcetable, $tablename));
+    $newvariable = domain_prefix_get_prefix() . $prefix . substr($variable['name'], strpos($sourcetable, $tablename));
+    $target = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $newvariable));
+    if (!$target) {
+      db_query("INSERT INTO {sequences} (name, id) VALUES ('%s', %d)", $newvariable, $variable['id']);
+    }
+    else {
+      db_query("UPDATE {sequences} SET id = %d WHERE name = '%s'", $variable['id'], $newvariable);
+    }
+  }
+}
+
+/**
  * Derive a module name from the table name
  *
  * In future, SchemaAPI will do this for us.
