app/DoctrineMigrations/Version20111125175224.php
author ymh <ymh.work@gmail.com>
Mon, 23 Jan 2012 00:47:51 +0100
changeset 64 97ddfdc8bdde
parent 40 d67ff36f17e2
child 71 b01e36991488
permissions -rw-r--r--
improve search test and doc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace Application\Migrations;
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
     5
use Doctrine\DBAL\Migrations\OutputWriter;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
     6
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
     7
use Doctrine\DBAL\Migrations\AbstractMigration;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
     8
use Doctrine\DBAL\Schema\Schema;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
     9
use Doctrine\Common\EventManager;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    10
use Doctrine\ORM\EntityManager;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    11
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    12
use IRI\Bundle\WikiTagBundle\Entity\Tag;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    13
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
/**
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * Auto-generated Migration: Please modify to your need!
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 */
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class Version20111125175224 extends AbstractMigration
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
{
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    20
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    21
    private function showProgress($current, $total, $label, $width)
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    22
    {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    23
        $percent = (floatval($current)/floatval($total)) * 100.0;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    24
        $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    25
        $spaces = $width - $marks;
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    26
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    27
        $status_bar="\r[";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    28
        $status_bar.=str_repeat("=", $marks);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    29
        if($marks<$width){
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    30
            $status_bar.=">";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    31
            $status_bar.=str_repeat(" ", $spaces);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    32
        } else {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    33
            $status_bar.="=";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    34
        }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    35
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    36
        $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    37
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    38
        $label = str_pad(substr($label,0,50), 50, " ");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    39
        $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    40
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    41
        $status_bar.="] $disp%  $current_str/$total : $label";
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    42
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    43
        $this->write("$status_bar  ");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    44
    
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    45
        if($current == $total) {
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    46
            $this->write("\n");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    47
        }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    48
    }
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    49
    
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    public function up(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    {
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        // this up() migration is autogenerated, please modify it to your needs
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        $this->addSql("ALTER TABLE wikitag_document ADD CONSTRAINT FK_256601059F75D7B0 FOREIGN KEY (external_id) REFERENCES Document(id)");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        $this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT DEFAULT NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        $this->addSql("ALTER TABLE wikitag_tag ADD alternative_label VARCHAR(1024) DEFAULT NULL, ADD wikipedia_alternative_url VARCHAR(2048) DEFAULT NULL, ADD alternative_wikipedia_pageid BIGINT DEFAULT NULL, CHANGE normalized_pageid wikipedia_pageid BIGINT DEFAULT NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    public function postUp(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    {
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    62
        $doctrine = $GLOBALS["application"]->getKernel()->getContainer()->get('doctrine');
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    63
        $em = $doctrine->getEntityManager();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    64
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    65
        $this->skipIf($this->version->isMigrated() !== true, 'postUp can only apply if migration completes.');
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        //select all tags with redirect and alternative label null
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        $query = $em->createQuery("SELECT t FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        $query->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    70
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    71
        $querycount = $em->createQuery("SELECT count(t) FROM WikiTagBundle:Tag t WHERE t.urlStatus = :url_status AND t.alternativeLabel IS NULL");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    72
        $querycount->setParameter("url_status", Tag::$TAG_URL_STATUS_DICT['redirection']);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    73
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    74
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    75
        $total = $querycount->getSingleScalarResult();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        $iterableResult = $query->iterate();
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        $i = 0;
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    78
        $em->beginTransaction();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    79
        foreach($iterableResult as $row) {
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            $i++;
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            $tag = $row[0];
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    83
            $memory = ((($i%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    84
            $this->showProgress($i, $total, "tag : ".$tag->getLabel()."$memory", 50);
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            //process $tag
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
            $wk_info = WikiTagUtils::getWikipediaInfo($tag->getNormalizedLabel());
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            $tag->setWikipediaInfo($wk_info);
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    89
            
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    90
            $em->persist($tag);
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    91
                            
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            if (($i % 100) == 0) {
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    93
                $em->flush();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    94
                $em->commit();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                $em->clear(); // Detaches all objects from Doctrine!
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    96
                $em->beginTransaction();
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        }
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
    99
        
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   100
        $em->flush(); // Executes all updates.
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   101
        $em->commit();
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   102
        $em->clear(); // Detaches all objects from Doctrine!
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    public function down(Schema $schema)
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    {
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        // this down() migration is autogenerated, please modify it to your needs
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        $this->addSql("ALTER TABLE wikitag_document DROP FOREIGN KEY FK_256601059F75D7B0");
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        $this->addSql("ALTER TABLE wikitag_document_tag CHANGE wikipedia_revision_id wikipedia_revision_id BIGINT NOT NULL");
40
d67ff36f17e2 Correct migration
ymh <ymh.work@gmail.com>
parents: 39
diff changeset
   113
        $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");
39
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    }
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
    
03b14b0fe101 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
}