Index: portmgr/jobs/PortIndex2MySQL.tcl
===================================================================
--- portmgr/jobs/PortIndex2MySQL.tcl    (revision 128993)
+++ portmgr/jobs/PortIndex2MySQL.tcl    (working copy)
@@ -244,47 +244,35 @@
     terminate 1
 }
 
-if {$create_tables} {
-    # Initial creation of database tables: log, portfiles, categories, maintainers, dependencies, variants and platforms.
-    # Do we need any other?
-    puts $sqlfile_fd "DROP TABLE IF EXISTS log;"
-    puts $sqlfile_fd "CREATE TABLE log (activity VARCHAR(255), activity_time TIMESTAMP(14)) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS portfiles;"
-    puts $sqlfile_fd "CREATE TABLE portfiles (name VARCHAR(255) PRIMARY KEY NOT NULL, path VARCHAR(255), version VARCHAR(255),  description TEXT) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS categories;"
-    puts $sqlfile_fd "CREATE TABLE categories (portfile VARCHAR(255), category VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS maintainers;"
-    puts $sqlfile_fd "CREATE TABLE maintainers (portfile VARCHAR(255), maintainer VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS dependencies;"
-    puts $sqlfile_fd "CREATE TABLE dependencies (portfile VARCHAR(255), library VARCHAR(255)) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS variants;"
-    puts $sqlfile_fd "CREATE TABLE variants (portfile VARCHAR(255), variant VARCHAR(255)) DEFAULT CHARSET=utf8;"
-    
-    puts $sqlfile_fd "DROP TABLE IF EXISTS platforms;"
-    puts $sqlfile_fd "CREATE TABLE platforms (portfile VARCHAR(255), platform VARCHAR(255)) DEFAULT CHARSET=utf8;"
+# Initial creation of database tables: log, portfiles, categories, maintainers, dependencies, variants and platforms.
+puts $sqlfile_fd "DROP TABLE IF EXISTS log_new;"
+puts $sqlfile_fd "CREATE TABLE log_new (activity VARCHAR(255), activity_time TIMESTAMP(14)) DEFAULT CHARSET=utf8;"
 
-    puts $sqlfile_fd "DROP TABLE IF EXISTS licenses;"
-    puts $sqlfile_fd "CREATE TABLE licenses (portfile VARCHAR(255), license VARCHAR(255)) DEFAULT CHARSET=utf8;"
-} else {
-    # if we are not creating tables from scratch, remove the old data
-    puts $sqlfile_fd "TRUNCATE log;"
-    puts $sqlfile_fd "TRUNCATE portfiles;"
-    puts $sqlfile_fd "TRUNCATE categories;"
-    puts $sqlfile_fd "TRUNCATE maintainers;"
-    puts $sqlfile_fd "TRUNCATE dependencies;"
-    puts $sqlfile_fd "TRUNCATE variants;"
-    puts $sqlfile_fd "TRUNCATE platforms;"
-    puts $sqlfile_fd "TRUNCATE licenses;"
-}
+puts $sqlfile_fd "DROP TABLE IF EXISTS portfiles_new;"
+puts $sqlfile_fd "CREATE TABLE portfiles_new (name VARCHAR(255) PRIMARY KEY NOT NULL, path VARCHAR(255), version VARCHAR(255),  description TEXT) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS categories_new;"
+puts $sqlfile_fd "CREATE TABLE categories_new (portfile VARCHAR(255), category VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS maintainers_new;"
+puts $sqlfile_fd "CREATE TABLE maintainers_new (portfile VARCHAR(255), maintainer VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS dependencies_new;"
+puts $sqlfile_fd "CREATE TABLE dependencies_new (portfile VARCHAR(255), library VARCHAR(255)) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS variants_new;"
+puts $sqlfile_fd "CREATE TABLE variants_new (portfile VARCHAR(255), variant VARCHAR(255)) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS platforms_new;"
+puts $sqlfile_fd "CREATE TABLE platforms_new (portfile VARCHAR(255), platform VARCHAR(255)) DEFAULT CHARSET=utf8;"
+
+puts $sqlfile_fd "DROP TABLE IF EXISTS licenses_new;"
+puts $sqlfile_fd "CREATE TABLE licenses_new (portfile VARCHAR(255), license VARCHAR(255)) DEFAULT CHARSET=utf8;"
  
 # Iterate over each matching port, extracting its information from the
 # portinfo array.
 foreach {name array} $ports {
+    puts $sqlfile_fd "START TRANSACTION;"
 
     array unset portinfo
     array set portinfo $array
@@ -352,64 +340,105 @@
         set licenses ""
     }
 
-    puts $sqlfile_fd "INSERT INTO portfiles VALUES ('$portname', '$portdir', '$portversion', '$description');"
+    puts $sqlfile_fd "INSERT INTO portfiles_new VALUES ('$portname', '$portdir', '$portversion', '$description');"
 
     set primary 1
     foreach category $categories {
         set category [sql_escape $category]
-        puts $sqlfile_fd "INSERT INTO categories VALUES ('$portname', '$category', $primary);"
+        puts $sqlfile_fd "INSERT INTO categories_new VALUES ('$portname', '$category', $primary);"
         set primary 0
     }
     
     set primary 1
     foreach maintainer $maintainers {
         set maintainer [sql_escape $maintainer]
-        puts $sqlfile_fd "INSERT INTO maintainers VALUES ('$portname', '$maintainer', $primary);"
+        puts $sqlfile_fd "INSERT INTO maintainers_new VALUES ('$portname', '$maintainer', $primary);"
         set primary 0
     }
 
     foreach fetch_dep $depends_fetch {
         set fetch_dep [sql_escape $fetch_dep]
-        puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$fetch_dep');"
+        puts $sqlfile_fd "INSERT INTO dependencies_new VALUES ('$portname', '$fetch_dep');"
     }
     
     foreach extract_dep $depends_extract {
         set extract_dep [sql_escape $extract_dep]
-        puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$extract_dep');"
+        puts $sqlfile_fd "INSERT INTO dependencies_new VALUES ('$portname', '$extract_dep');"
     }
 
     foreach build_dep $depends_build {
         set build_dep [sql_escape $build_dep]
-        puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$build_dep');"
+        puts $sqlfile_fd "INSERT INTO dependencies_new VALUES ('$portname', '$build_dep');"
     }
 
     foreach lib $depends_lib {
         set lib [sql_escape $lib]
-        puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$lib');"
+        puts $sqlfile_fd "INSERT INTO dependencies_new VALUES ('$portname', '$lib');"
     }
 
     foreach run_dep $depends_run {
         set run_dep [sql_escape $run_dep]
-        puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$run_dep');"
+        puts $sqlfile_fd "INSERT INTO dependencies_new VALUES ('$portname', '$run_dep');"
     }
 
     foreach variant $variants {
         set variant [sql_escape $variant]
-        puts $sqlfile_fd "INSERT INTO variants VALUES ('$portname', '$variant');"
+        puts $sqlfile_fd "INSERT INTO variants_new VALUES ('$portname', '$variant');"
     }
 
     foreach platform $platforms {
         set platform [sql_escape $platform]
-        puts $sqlfile_fd "INSERT INTO platforms VALUES ('$portname', '$platform');"
+        puts $sqlfile_fd "INSERT INTO platforms_new VALUES ('$portname', '$platform');"
     }
 
     foreach license $licenses {
         set license [sql_escape $license]
-        puts $sqlfile_fd "INSERT INTO licenses VALUES ('$portname', '$license');"
+        puts $sqlfile_fd "INSERT INTO licenses_new VALUES ('$portname', '$license');"
     }
+    puts $sqlfile_fd "COMMIT;"
 
 }
 
+if {$create_tables} {
+    puts $sqlfile_fd "RENAME TABLE \
+        portfiles_new TO portfiles, \
+        categories_new TO categories, \
+        maintainers_new TO maintainers, \
+        dependencies_new TO dependencies, \
+        variants_new TO variants, \
+        platforms_new TO platforms, \
+        licenses_new TO licenses_old, \
+        log_new TO log;"
+} else {
+    puts $sqlfile_fd "RENAME TABLE \
+        portfiles TO portfiles_old, \
+        portfiles_new TO portfiles, \
+        categories TO categories_old, \
+        categories_new TO categories, \
+        maintainers TO maintainers_old, \
+        maintainers_new TO maintainers, \
+        dependencies TO dependencies_old, \
+        dependencies_new TO dependencies, \
+        variants TO variants_old, \
+        variants_new TO variants, \
+        platforms TO platforms_old, \
+        platforms_new TO platforms, \
+        licenses TO licenses_old, \
+        licenses_new TO licenses_old, \
+        log TO log_old, \
+        log_new TO log;"
+}
+
+puts $sqlfile_fd "DROP TABLES IF EXISTS \
+    portfiles_old, \
+    categories_old, \
+    maintainers_old, \
+    dependencies_old, \
+    variants_old, \
+    platforms_old, \
+    licenses_old, \
+    log_old;"
+
 # Mark the db regen as done only once we're done processing all ports:
 puts $sqlfile_fd "INSERT INTO log VALUES ('update', NOW());"
 
