Correct migration
authorymh <ymh.work@gmail.com>
Sun, 27 Nov 2011 22:36:22 +0100
changeset 40 d67ff36f17e2
parent 39 03b14b0fe101
child 42 8836f21d6ac0
Correct migration
.hgsubstate
app/DoctrineMigrations/Version20111125175224.php
--- a/.hgsubstate	Fri Nov 25 18:55:43 2011 +0100
+++ b/.hgsubstate	Sun Nov 27 22:36:22 2011 +0100
@@ -1,1 +1,1 @@
-0e57c730bb180b2f25aca6cac72f0bd479e1f30c vendor/bundles/IRI/Bundle/WikiTagBundle
+54f204bceb28e10bb1f9776e5d6bbde138d6aa35 vendor/bundles/IRI/Bundle/WikiTagBundle
--- a/app/DoctrineMigrations/Version20111125175224.php	Fri Nov 25 18:55:43 2011 +0100
+++ b/app/DoctrineMigrations/Version20111125175224.php	Sun Nov 27 22:36:22 2011 +0100
@@ -2,14 +2,51 @@
 
 namespace Application\Migrations;
 
-use Doctrine\DBAL\Migrations\AbstractMigration,
-    Doctrine\DBAL\Schema\Schema;
+use Doctrine\DBAL\Migrations\OutputWriter;
+
+use Doctrine\DBAL\Migrations\AbstractMigration;
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\Common\EventManager;
+use Doctrine\ORM\EntityManager;
+use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+use IRI\Bundle\WikiTagBundle\Entity\Tag;
+
 
 /**
  * Auto-generated Migration: Please modify to your need!
  */
 class Version20111125175224 extends AbstractMigration
 {
+    
+    private function showProgress($current, $total, $label, $width)
+    {
+        $percent = (floatval($current)/floatval($total)) * 100.0;
+        $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
+        $spaces = $width - $marks;
+    
+        $status_bar="\r[";
+        $status_bar.=str_repeat("=", $marks);
+        if($marks<$width){
+            $status_bar.=">";
+            $status_bar.=str_repeat(" ", $spaces);
+        } else {
+            $status_bar.="=";
+        }
+    
+        $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
+    
+        $label = str_pad(substr($label,0,50), 50, " ");
+        $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
+    
+        $status_bar.="] $disp%  $current_str/$total : $label";
+    
+        $this->write("$status_bar  ");
+    
+        if($current == $total) {
+            $this->write("\n");
+        }
+    }
+    
     public function up(Schema $schema)
     {
         // this up() migration is autogenerated, please modify it to your needs
@@ -22,30 +59,47 @@
     
     public function postUp(Schema $schema)
     {
-        $evm = new EventManager();
-        $em = EntityManager::create($conn, $this->configuration, $evm);
+        $doctrine = $GLOBALS["application"]->getKernel()->getContainer()->get('doctrine');
+        $em = $doctrine->getEntityManager();
+        
+        $this->skipIf($this->version->isMigrated() !== true, 'postUp can only apply if migration completes.');
     
         //select all tags with redirect and alternative label null
         $query = $em->createQuery("SELECT t FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
         $query->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
-    
+
+        $querycount = $em->createQuery("SELECT count(t) FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
+        $querycount->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
+        
+        
+        $total = $querycount->getSingleScalarResult();
         $iterableResult = $query->iterate();
         $i = 0;
-    
-        foreach($iterableResult AS $row) {
+        $em->beginTransaction();
+        foreach($iterableResult as $row) {
             $i++;
             $tag = $row[0];
     
+            $memory = ((($i%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
+            $this->showProgress($i, $total, "tag : ".$tag->getLabel()."$memory", 50);
             //process $tag
             $wk_info = WikiTagUtils::getWikipediaInfo($tag->getNormalizedLabel());
     
             $tag->setWikipediaInfo($wk_info);
-    
+            
+            $em->persist($tag);
+                            
             if (($i % 100) == 0) {
-                $em->flush(); // Executes all updates.
+                $em->flush();
+                $em->commit();
                 $em->clear(); // Detaches all objects from Doctrine!
+                $em->beginTransaction();
             }
         }
+        
+        $em->flush(); // Executes all updates.
+        $em->commit();
+        $em->clear(); // Detaches all objects from Doctrine!
     
     }
     
@@ -56,7 +110,7 @@
         
         $this->addSql("ALTER TABLE wikitag_document DROP FOREIGN KEY FK_256601059F75D7B0");
         $this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT NOT NULL");
-        $this->addSql("ALTER TABLE wikitag_tag ADD normalized_pageid BIGINT DEFAULT NULL, DROP alternative_label, DROP wikipedia_alternative_url, DROP alternative_wikipedia_pageid, CHANGE wikipedia_pageid normalized_pageid BIGINT DEFAULT NULL");
+        $this->addSql("ALTER TABLE wikitag_tag DROP alternative_label, DROP wikipedia_alternative_url, DROP alternative_wikipedia_pageid, CHANGE wikipedia_pageid normalized_pageid BIGINT DEFAULT NULL");
     }