Author: sevein
Date: Wed Jul 11 15:48:24 2012
New Revision: 11910

Log:
New functions to add menu nodes with constant IDs and to drop database columns 
taking care of related column indexes and foreign keys, useful for new SQL 
migrations

Modified:
   trunk/lib/task/migrate/QubitMigrate.class.php

Modified: trunk/lib/task/migrate/QubitMigrate.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate.class.php       Wed Jul 11 11:51:11 
2012        (r11909)
+++ trunk/lib/task/migrate/QubitMigrate.class.php       Wed Jul 11 15:48:24 
2012        (r11910)
@@ -447,6 +447,7 @@
 
         case 'term':
         case 'taxonomy':
+        case 'menu':
           $columns[] = array('table' => $item, 'column' => 'id');
           $columns[] = array('table' => $item.'_i18n', 'column' => 'id');
       }
@@ -528,6 +529,125 @@
     catch (Exception $e)
     {
       $connection->rollback();
+
+      throw $e;
+    }
+
+    $connection->commit();
+  }
+
+  public static function addMenu(QubitMenu $object, $configuration)
+  {
+    $connection = Propel::getConnection();
+
+    $connection->beginTransaction();
+
+    try
+    {
+      // Check if it already exists
+      if (isset($object->id) && null !== QubitMenu::getById($object->id))
+      {
+        $connection->exec('SET FOREIGN_KEY_CHECKS = 0');
+
+        // Get new autonumeric
+        $last = QubitPdo::fetchOne('SELECT (MAX(id) + 1) AS last FROM 
object')->last;
+
+        $foreignKeys = self::findForeignKeys(array(
+          QubitObject::TABLE_NAME,
+          QubitMenu::TABLE_NAME), $configuration);
+
+        foreach ($foreignKeys as $item)
+        {
+          QubitPdo::modify(
+            "UPDATE $item[table] SET $item[column] = ? WHERE $item[column] = 
?", array(
+              $last,
+              $object->id));
+        }
+
+        $connection->exec('SET FOREIGN_KEY_CHECKS = 1');
+      }
+
+      $object->save();
+    }
+    catch (Exception $e)
+    {
+      $connection->rollback();
+
+      throw $e;
+    }
+
+    $connection->commit();
+  }
+
+  public static function dropColumn($table, $column)
+  {
+    $connection = Propel::getConnection();
+
+    $connection->beginTransaction();
+
+    try
+    {
+      $stmt = $connection->prepare('SHOW CREATE TABLE '.$table);
+      $stmt->execute();
+
+      $data = $stmt->fetchAll();
+
+      foreach (explode("\n", $data[0][1]) as $line)
+      {
+        $line = explode(' ', trim($line));
+
+        switch ($line[0])
+        {
+          // Indexes
+          case 'KEY':
+
+            // Build array with DROP INDEX commands
+            if ('(`'.$column.'`),' == $line[2])
+            {
+              $keys[] = 'DROP INDEX '.$line[1].' ON '.$table;
+            }
+            else
+            {
+              continue 2;
+            }
+
+            break;
+
+          // Foreign keys
+          case 'CONSTRAINT':
+
+            // Build array with DROP FOREIGN KEY commands
+            if ('FOREIGN' == $line[2] && '(`'.$column.'`)' == $line[4])
+            {
+              $foreignKeys[] = 'ALTER TABLE '.$table.' DROP FOREIGN KEY 
'.$line[1];
+            }
+            else
+            {
+              continue 2;
+            }
+
+            break;
+        }
+      }
+
+      foreach ($foreignKeys as $sqlCommand)
+      {
+        $connection->exec($sqlCommand);
+      }
+
+      foreach ($keys as $sqlCommand)
+      {
+        $connection->exec($sqlCommand);
+      }
+
+      // Drop column
+      $connection->exec('ALTER TABLE `'.$table.'` DROP COLUMN `'.$column.'`');
+    }
+    catch (Exception $e)
+    {
+      $connection->rollback();
+
+      throw $e;
     }
 
     $connection->commit();

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to