开发者

Remove column in postUp() method

Is possible to remove a column in a migration postUp() method or is only intended to be used for data manipula开发者_StackOverflow社区tion?


I don't know what version of Doctrine Migrations you are using. However, I just ran across this same question when working with Doctrine Migrations 2.0 and started digging into the code. When looking at how a Version is constructed, it appears that using the Schema object to make changes in the postUp() method does not take effect.

However, upon further consideration, this really makes sense. Each migration version is intended to modify the database structure. This takes place in the up() and down() methods of each migration. postUp() seems to be intended mostly for post structural changes cleanup (i.e. manipulating data). Any further structural modifications that you had intended on making in the postUp() method are should be made in a subsequent migration file.

For example, I was attempting to create a new table that was going to hold two columns from a previous table. I intended to drop those columns from the previous table after I had migrated data over to the new table. The code follows:

class Version20110512223208 extends AbstractMigration
{
    protected $customerRepository;

    protected $evernoteRepository;

    public function up(Schema $schema)
    {
        $table = $schema->createTable('customer_evernote');
        $table->addOption('type', 'INNODB');
        $table->addOption('charset', 'utf8');
        $table->addOption('collate', 'utf8_unicode_ci');

        // Columns.
        $table->addColumn('customer_id', 'bigint', array(
            'length'    => 20,
            'notnull'   => true,
            'autoincrement' => false));
        $table->addColumn('integration_date', 'datetime', array('notnull' => true));
        $table->addColumn('oauth_token', 'string', array(
            'length'    => 255,
            'notnull'   => true));
        $table->addColumn('oauth_shard_id', 'string', array(
            'length'    => 4,
            'notnull'   => true,
            'fixed'     => true));

        $table->setPrimaryKey(array('customer_id'), 'pk_customer_id');
        $table->addForeignKeyConstraint($schema->getTable('customer'), array('customer_id'), array('id'));
    }

    public function down(Schema $schema)
    {
        $schema->dropTable('customer_evernote');
    }

    public function preUp(Schema $schema)
    {
        $this->addSql("ALTER TABLE `customer` ENGINE = INNODB");
    }

    public function postUp(Schema $schema)
    {
        $this->skipIf($this->version->isMigrated() !== true, 'postUp can only apply if migration completes.');

        // Copy the data from the customer table into the newly created customer_evernote table.
        $this->doctrine = \Zend_Registry::get('doctrine');
        $this->entityManager = $this->doctrine->getEntityManager();
        $this->customerRepository = $this->entityManager->getRepository('My\Entity\Customer');
        $this->evernoteRepository = $this->entityManager->getRepository('My\Entity\CustomerEvernote');
        $customers = $this->customerRepository->findAll();

        foreach ($customers as $customer)
        {
            $evernoteRecord = new \My\Entity\CustomerEvernote();
            $evernoteRecord->setCustomerId($customer->getId());
            $evernoteRecord->setCustomer($customer);
            $evernoteRecord->setOauthToken($customer->getEvernoteOauthToken());
            $evernoteRecord->setOauthShardId($customer->getEvernoteOauthShardId());
            $evernoteRecord->setIntegrationDate(new \DateTime("now"));

            $this->evernoteRepository->saveEvernote($evernoteRecord);
        }

        // Drop the columns from the existing customer table.
        $table = $schema->getTable('customer');
        $table->dropColumn('evernote_oauth_token');
        $table->dropColumn('evernote_oauth_shard_id');
    }

    public function preDown(Schema $schema)
    {
        // Create the existing columns in the customer table.
        $table = $schema->getTable('customer');
        $table->addColumn('evernote_oauth_token', 'string', array(
            'length'    => 255,
            'notnull'   => false));
        $table->addColumn('evernote_oauth_shard_id', 'string', array(
            'length'    => 4,
            'notnull'   => false,
            'fixed'     => true));

        // Copy the data to the customer table.
        $this->doctrine = \Zend_Registry::get('doctrine');
        $this->entityManager = $this->doctrine->getEntityManager();
        $this->customerRepository = $this->entityManager->getRepository('My\Entity\Customer');
        $this->evernoteRepository = $this->entityManager->getRepository('My\Entity\CustomerEvernote');

        $integrations = $this->evernoteRepository->findAll();

        foreach ($integrations as $integration)
        {
            $integration->getCustomer()->setEvernoteOauthToken($integration->getOauthToken());
            $integration->getCustomer()->setEvernoteOauthShardId($integration->getOauthShardId());

            $this->customerRepository->saveCustomer($integration->getCustomer());
        }
    }
}

In reality, if I were to move the code at the end of postUp() to a new version's up() and the code at the beginning of the preDown() to a new version's down() method, I get the same results as in the class above, just performed in two separate steps. With this approach, I guarantee that I have structural modifications strictly taking place in my up and down methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜