Fix migration from v3.8.8 to v4.0.0

This commit is contained in:
Thibaud Fabre
2016-02-03 16:35:20 +01:00
parent 0bd25711b8
commit dc51e489fe
6 changed files with 109 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ use Alchemy\Phrasea\Setup\ConfigurationTester;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade\PreSchemaUpgradeCollection;
use Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade\Upgrade39Feeds;
use Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade\Upgrade39Sessions;
use Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade\Upgrade39Tokens;
use Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade\Upgrade39Users;
use Silex\Application as SilexApplication;
@@ -30,7 +31,7 @@ class ConfigurationTesterServiceProvider implements ServiceProviderInterface
});
$app['phraseanet.pre-schema-upgrader.upgrades'] = $app->share(function () {
return [new Upgrade39Feeds(), new Upgrade39Users(), new Upgrade39Tokens()];
return [new Upgrade39Feeds(), new Upgrade39Users(), new Upgrade39Tokens(), new Upgrade39Sessions()];
});
$app['phraseanet.pre-schema-upgrader'] = $app->share(function (Application $app) {

View File

@@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Table(name="Sessions", indexes={@ORM\index(name="session_user_id", columns={"user_id"})})
* @ORM\Table(name="Sessions")
* @ORM\Entity(repositoryClass="Alchemy\Phrasea\Model\Repositories\SessionRepository")
*/
class Session

View File

@@ -17,16 +17,18 @@ class SessionMigration extends AbstractMigration
{
public function isAlreadyApplied()
{
return $this->tableExists('Sessions');
return false;
}
public function doUpSql(Schema $schema)
{
$this->addSql("CREATE TABLE SessionModules (id INT AUTO_INCREMENT NOT NULL, session_id INT DEFAULT NULL, module_id INT NOT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, INDEX IDX_BA36EF49613FECDF (session_id), UNIQUE INDEX unique_module (session_id, module_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
if (! $schema->hasTable('Sessions')) {
$this->addSql("CREATE TABLE Sessions (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, user_agent VARCHAR(512) NOT NULL, ip_address VARCHAR(40) DEFAULT NULL, platform VARCHAR(128) DEFAULT NULL, browser_name VARCHAR(128) DEFAULT NULL, browser_version VARCHAR(32) DEFAULT NULL, screen_width INT DEFAULT NULL, screen_height INT DEFAULT NULL, token VARCHAR(128) DEFAULT NULL, nonce VARCHAR(16) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, UNIQUE INDEX UNIQ_6316FF455F37A13B (token), INDEX IDX_6316FF45A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE SessionModules (id INT AUTO_INCREMENT NOT NULL, session_id INT DEFAULT NULL, module_id INT NOT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, INDEX IDX_BA36EF49613FECDF (session_id), UNIQUE INDEX unique_module (session_id, module_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("ALTER TABLE SessionModules ADD CONSTRAINT FK_BA36EF49613FECDF FOREIGN KEY (session_id) REFERENCES Sessions (id)");
$this->addSql("ALTER TABLE Sessions ADD CONSTRAINT FK_6316FF45A76ED395 FOREIGN KEY (user_id) REFERENCES Users (id)");
}
}
public function doDownSql(Schema $schema)
{

View File

@@ -14,6 +14,8 @@ class Version20150519173347 extends BaseMigration
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->skipIf($schema->hasTable('Secrets'), 'Table already exists');
$this->addSql('CREATE TABLE Secrets (id INT AUTO_INCREMENT NOT NULL, creator_id INT NOT NULL, created DATETIME NOT NULL, token VARCHAR(40) COLLATE utf8_bin NOT NULL, INDEX IDX_48F428861220EA6 (creator_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE Secrets ADD CONSTRAINT FK_48F428861220EA6 FOREIGN KEY (creator_id) REFERENCES Users (id)');
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Setup\Version\PreSchemaUpgrade;
use Alchemy\Phrasea\Application;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\ResultSetMapping;
class Upgrade39Sessions implements PreSchemaUpgradeInterface
{
/**
* {@inheritdoc}
*/
public function apply(EntityManager $em, \appbox $appbox, Configuration $conf)
{
if ($this->tableExists($em, 'Sessions')) {
$this->dropTable($em, 'SessionModules');
$this->dropTable($em, 'Sessions');
}
}
/**
* {@inheritdoc}
*/
public function isApplyable(Application $app)
{
return $this->tableExists($app['orm.em'], 'Sessions');
}
/**
* {@inheritdoc}
*/
public function rollback(EntityManager $em, \appbox $appbox, Configuration $conf)
{
if ($this->tableExists($em, 'Sessions')) {
}
}
/**
* Checks whether the table exists or not.
*
* @param $tableName
*
* @return boolean
*/
private function tableExists(EntityManager $em, $table)
{
return (Boolean) $em->createNativeQuery(
'SHOW TABLE STATUS WHERE Name="'.$table.'" COLLATE utf8_bin ', (new ResultSetMapping())->addScalarResult('Name', 'Name')
)->getOneOrNullResult();
}
/**
* @param EntityManager $em
* @param $tableName
*/
private function dropTable(EntityManager $em, $tableName)
{
$em->getConnection()->getSchemaManager()->dropTable($tableName);
}
/**
*
* @param EntityManager $em
* @param $tableName
*/
private function doTableBackup(EntityManager $em, $tableName)
{
$em->getConnection()->getSchemaManager()->renameTable($tableName, $tableName . '_backup');
}
private function doTableRestore(EntityManager $em, $tableName)
{
$em->getConnection()->getSchemaManager()->renameTable($tableName . '_backup', $tableName);
}
}

View File

@@ -35,6 +35,14 @@ class module_console_systemUpgrade extends Command
protected function doExecute(InputInterface $input, OutputInterface $output)
{
require_once rtrim($this->container['root.path'], '\\/') . '/plugins/autoload.php';
$serviceProvider = new \Alchemy\Phrasea\Core\Provider\PluginServiceProvider();
$serviceProvider->register($this->getContainer());
$serviceProvider->boot($this->getContainer());
$this->getContainer()->loadPlugins();
$interactive = !$input->getOption('yes');
while ($migrations = $this->container['phraseanet.configuration-tester']->getMigrations()) {