Delete column in postUp () method

Is it possible to delete a column in the postUp () method of migration or is it intended only for use in working with data?

+4
source share
1 answer

I do not know which version of the Migration Doctrine you are using. However, I just ran into this issue when working with Doctrine Migrations 2.0 and started digging into the code. Considering how the version is created, it seems that using the Schema object to make changes to the postUp () method does not take effect.

However, upon further consideration this really makes sense. Each version of the migration is designed to change the structure of the database. This happens in the up () and down () methods for each migration. It seems that postUp () is mainly intended to clear post-structural changes (i.e. data manipulation). Any additional structural changes that you intended to make in the postUp () method should be made in a subsequent migration file.

For example, I tried to create a new table that would contain two columns from the previous table. I was going to remove these columns from the previous table after moving the data to a new table. 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 fact, if I were to move the code at the end of postUp () to the new version of up () and the code at the beginning of preDown () for the method of the new version of down (), I get the same results as in the above class, they just execute in two separate steps. With this approach, I guarantee that I have structural changes that are strictly implemented in my up and down methods.

+4
source

Source: https://habr.com/ru/post/1346900/


All Articles