Add category migration, remove old upgrade script, catch some errors if
authorMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 6 Oct 2010 15:24:41 +0000 (11:24 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 6 Oct 2010 15:30:09 +0000 (11:30 -0400)
migration run after some of these updates were already applied.

ansel/migration/3_ansel_upgrade_style.php
ansel/migration/4_ansel_upgrade_tagstocontent.php
ansel/migration/5_ansel_upgrade_categoriestotags.php [new file with mode: 0644]
ansel/scripts/upgrades/2010-09-16_migrate_categories_to_tags.php [deleted file]

index e72fc5a..9f661ad 100644 (file)
@@ -50,7 +50,7 @@ class AnselUpgradeStyle extends Horde_Db_Migration_Base
         foreach ($rows as $row) {
             // Make sure we haven't already migrated
             if (@unserialize($row['attribute_style']) instanceof Ansel_Style) {
-                $this->announce('Skipping share ' . $row[0] . ', already migrated.', 'cli.message');
+                $this->announce('Skipping share ' . $row['attribute_style'] . ', already migrated.', 'cli.message');
                 continue;
             }
             if (empty($styles[$row['attribute_style']])) {
index 07340ae..1d6add0 100644 (file)
@@ -16,32 +16,42 @@ class AnselUpgradeTagsToContent extends Horde_Db_Migration_Base
 {
     public function up()
     {
-        $GLOBALS['registry']->pushApp('ansel');
+        $tableList = $this->tables();
+        if (in_array('ansel_galleries_tags', $tableList)) {
+            $GLOBALS['registry']->pushApp('ansel');
 
-        /* Gallery tags */
-        $sql = 'SELECT gallery_id, tag_name, share_owner FROM ansel_shares RIGHT JOIN '
-            . 'ansel_galleries_tags ON ansel_shares.share_id = ansel_galleries_tags.gallery_id '
-            . 'LEFT JOIN ansel_tags ON ansel_tags.tag_id = ansel_galleries_tags.tag_id;';
+            /* Gallery tags */
+            $sql = 'SELECT gallery_id, tag_name, share_owner FROM ansel_shares RIGHT JOIN '
+                . 'ansel_galleries_tags ON ansel_shares.share_id = ansel_galleries_tags.gallery_id '
+                . 'LEFT JOIN ansel_tags ON ansel_tags.tag_id = ansel_galleries_tags.tag_id;';
 
-        // Maybe iterate over results and aggregate them by user and gallery so we can
-        // tag all tags for a single gallery at once. Probably not worth it for a one
-        // time upgrade script.
-        $this->announce('Migrating gallery tags. This may take a while.');
-        $rows = $this->_connection->selectAssoc($sql);
-        foreach ($rows as $row) {
-            $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row['gallery_id'], $row['tag_name'], $row['share_owner'], 'gallery');
-        }
-        $this->announce('Gallery tags finished.');
-        $sql = 'SELECT ansel_images.image_id iid, tag_name, share_owner FROM ansel_images '
-            . 'RIGHT JOIN ansel_images_tags ON ansel_images.image_id = ansel_images_tags.image_id '
-            . 'LEFT JOIN ansel_shares ON ansel_shares.share_id = ansel_images.gallery_id '
-            . 'LEFT JOIN ansel_tags ON ansel_tags.tag_id = ansel_images_tags.tag_id';
-        $this->announce('Migrating image tags. This may take even longer...');
-        $rows = $this->_connection->selectAssoc($sql);
-        foreach ($rows as $row) {
-            $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row['iid'], $row['tag_name'], $row['share_owner'], 'image');
+            // Maybe iterate over results and aggregate them by user and gallery so we can
+            // tag all tags for a single gallery at once. Probably not worth it for a one
+            // time upgrade script.
+            $this->announce('Migrating gallery tags. This may take a while.');
+            $rows = $this->_connection->selectAll($sql);
+            foreach ($rows as $row) {
+                $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row['gallery_id'], $row['tag_name'], $row['share_owner'], 'gallery');
+            }
+            $this->announce('Gallery tags finished.');
+            $sql = 'SELECT ansel_images.image_id iid, tag_name, share_owner FROM ansel_images '
+                . 'RIGHT JOIN ansel_images_tags ON ansel_images.image_id = ansel_images_tags.image_id '
+                . 'LEFT JOIN ansel_shares ON ansel_shares.share_id = ansel_images.gallery_id '
+                . 'LEFT JOIN ansel_tags ON ansel_tags.tag_id = ansel_images_tags.tag_id';
+            $this->announce('Migrating image tags. This may take even longer...');
+            $rows = $this->_connection->selectAll($sql);
+            foreach ($rows as $row) {
+                $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row['iid'], $row['tag_name'], $row['share_owner'], 'image');
+            }
+            $this->announce('Image tags finished.');
+
+            $this->announce('Dropping ansel tag tables');
+            $this->dropTable('ansel_galleries_tags');
+            $this->dropTable('ansel_images_tags');
+            $this->dropTable('ansel_tags');
+        } else {
+            $this->announce('Tags ALREADY migrated to content system.');
         }
-        $this->announce('Image tags finished.');
     }
 
     public function down()
diff --git a/ansel/migration/5_ansel_upgrade_categoriestotags.php b/ansel/migration/5_ansel_upgrade_categoriestotags.php
new file mode 100644 (file)
index 0000000..c92ba17
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Move tags from ansel to content storage.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author   Michael J. Rubinsky <mrubinsk@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @package  Ansel
+ */
+class AnselUpgradeCategoriesToTags extends Horde_Db_Migration_Base
+{
+    public function up()
+    {
+        $GLOBALS['registry']->pushApp('ansel');
+
+        /* Gallery tags */
+        $t = $this->_connection->table('ansel_shares');
+        $cols = $t->getColumns();
+        if (in_array('attribute_category', array_keys($cols))) {
+            $sql = 'SELECT share_id, attribute_category, share_owner FROM ansel_shares';
+            $this->announce('Migrating gallery categories.');
+            $rows = $this->_connection->selectAll($sql);
+            foreach ($rows as $row) {
+                $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row['share_id'], $row['attribute_category'], $row['share_owner'], 'gallery');
+            }
+            $this->announce('Gallery categories successfully migrated.');
+            $this->removeColumn('ansel_shares', 'attribute_category');
+        } else {
+            $this->announce('Gallery categories ALREADY migrated.');
+        }
+    }
+
+    public function down()
+    {
+        // Not supported, no way to tell which tags were categories.
+    }
+
+}
\ No newline at end of file
diff --git a/ansel/scripts/upgrades/2010-09-16_migrate_categories_to_tags.php b/ansel/scripts/upgrades/2010-09-16_migrate_categories_to_tags.php
deleted file mode 100644 (file)
index baca56b..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/usr/bin/env php
-<?php
-/**
- * Script for migrating Ansel 1.x categories to the tags system in Ansel 2.
- * This script should be run *after* tags are migrated to content/.
- *
- * @author Michael J. Rubinsky <mrubinsk@horde.org>
- */
-require_once dirname(__FILE__) . '/../../lib/Application.php';
-Horde_Registry::appInit('ansel', array('authentication' => 'none', 'cli' => true));
-
-/* Gallery tags */
-$sql = 'SELECT share_id, attribute_category, share_owner FROM ansel_shares';
-
-// Maybe iterate over results and aggregate them by user and gallery so we can
-// tag all tags for a single gallery at once. Probably not worth it for a one
-// time upgrade script.
-$cli->message('Migrating gallery categories.', 'cli.message');
-$rows = $ansel_db->queryAll($sql);
-foreach ($rows as $row) {
-    $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($row[0], $row[1], $row[2], 'gallery');
-}
-$cli->message('Gallery categories successfully migrated.', 'cli.success');