Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active August 29, 2015 14:09
Show Gist options
  • Save webdevilopers/0888dcc05b9d7f0f99ac to your computer and use it in GitHub Desktop.
Save webdevilopers/0888dcc05b9d7f0f99ac to your computer and use it in GitHub Desktop.
Schema commands not executed in postUp() method while prepared statements in queries are executed even with --write-sql option in Doctrine Migrations
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Version20141112104658 extends AbstractMigration implements ContainerAwareInterface
//class Version20141112104658 extends AbstractMigration
{
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function up(Schema $schema)
{
$table = $schema->getTable('users');
$table->addColumn('test', 'string');
}
public function postUp(Schema $schema)
{
$outputWriter = $this->version->getConfiguration()->getOutputWriter();
$outputWriter->write('Starting post-up...');
// Schema commands are never executed
// @see http://stackoverflow.com/questions/5564945/remove-column-in-postup-method#6170121
$table = $schema->getTable('users');
$table->dropColumn('test', 'string'); // not executed
// if ($this->version->isMigrated() !== true) echo 'Not migrated yet. Exiting post-up.'; exit;
// Will be executed even with --write-sql option
// @see https://gist.github.com/ludekstepan/3755289
// $container = $this->version->getConfiguration()->getContainer();
// $em = $container->get('doctrine.orm.entity_manager');
$em = $this->container->get('doctrine.orm.entity_manager');
// Initial FOSUserBundle `roles` setup for legacy users
$initialRolesQuery = "UPDATE users SET roles = 'a:0:{}'";
$stmt = $this->connection->prepare($initialRolesQuery);
$outputWriter->write("Initial FOSUserBundle `roles` setup for legacy users...");
$stmt->execute();
// $superAdmin = $em->getRepository('ApplicationSonataUserBundle:User')->find(1);
// $superAdmin->addRole('ROLE_SUPER_ADMIN');
// $em->persist($superAdmin);
// echo "Setting Superadmin role for User 1...";
// $em->flush();
// Get user data from legacy `roles` table
$legacyRolesQuery = "SELECT role, user_id FROM roles";
$stmt = $this->connection->prepare($legacyRolesQuery);
$outputWriter->write("Setting legacy roles on users...");
$stmt->execute();
// We can't use Doctrine's ORM to fetch the item, because there is no Entity for legacy table `roles`
while ($row = $stmt->fetch()) {
$userId = $row['user_id'];
$user = $em->getRepository('ApplicationSonataUserBundle:User')->find($userId);
$roles = []; // Set default value `a:0:{}`
switch ($row['role'])
{
case 'compartment_admin':
// a:2:{i:0;s:10:"ROLE_ADMIN";i:1;s:16:"ROLE_SUPER_ADMIN";}
$roles[] = 'ROLE_SUPER ADMIN';
case 'manager':
$roles[] = 'ROLE_ADMIN';
case 'project_manager':
$roles[] = 'ROLE_PROJECT_MANAGER';
case 'none':
default:
break;
}
$user->setRoles(array_unique($roles));
$em->persist($user);
}
$em->flush();
}
public function down(Schema $schema)
{
}
}
@webdevilopers
Copy link
Author

Schema commands are never executed as discussed in:
http://stackoverflow.com/questions/5564945/remove-column-in-postup-method#6170121

$container = $this->version->getConfiguration()->getContainer();
$em = $container->get('doctrine.orm.entity_manager');

causes
Call to undefined method Doctrine\DBAL\Migrations\Configuration\Configuration::getContainer()
because not implemented yet.
See example by @ludekstepan: https://gist.github.com/ludekstepan/3755289

Related issues:

Queries with prepared statements are executed even with option --write-sql. To prevent use:

if ($this->version->isMigrated() !== true) echo 'Not migrated yet. Exiting post-up.'; exit;

@webdevilopers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment