mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-26 17:43:54 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,62 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\Report;
|
||||
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
|
||||
|
||||
/**
|
||||
* Add a scope to report
|
||||
* Add a scope to report.
|
||||
*/
|
||||
class Version20150622233319 extends AbstractMigration
|
||||
implements ContainerAwareInterface
|
||||
class Version20150622233319 extends AbstractMigration implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
if ($container === NULL) {
|
||||
throw new \RuntimeException('Container is not provided. This migration '
|
||||
$this->abortIf(
|
||||
$this->connection->getDatabasePlatform()->getName() != 'postgresql',
|
||||
'Migration can only be executed safely on \'postgresql\'.'
|
||||
);
|
||||
|
||||
$this->addSql('ALTER TABLE Report DROP CONSTRAINT FK_report_scope');
|
||||
$this->addSql('DROP INDEX IDX_report_scope');
|
||||
$this->addSql('ALTER TABLE Report ADD scope VARCHAR(255) DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE Report DROP scope_id');
|
||||
}
|
||||
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
if (null === $container) {
|
||||
throw new RuntimeException('Container is not provided. This migration '
|
||||
. 'need container to set a default center');
|
||||
}
|
||||
|
||||
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql',
|
||||
'Migration can only be executed safely on \'postgresql\'.');
|
||||
$this->abortIf(
|
||||
$this->connection->getDatabasePlatform()->getName() != 'postgresql',
|
||||
'Migration can only be executed safely on \'postgresql\'.'
|
||||
);
|
||||
|
||||
$this->addSql('ALTER TABLE report ADD scope_id INT DEFAULT NULL');
|
||||
|
||||
|
||||
/*
|
||||
* Before the upgrade to symfony version 4, this code worked.
|
||||
*
|
||||
*
|
||||
* But it doesn't work any more after the migration and currently this
|
||||
* code should note be necessary.
|
||||
*
|
||||
*
|
||||
* This code is kept for reference, and may be re-activated if needed, but
|
||||
* the probability that this will happens is near 0
|
||||
*
|
||||
*
|
||||
|
||||
//add a default scope
|
||||
$scopes = $this->container->get('doctrine.orm.default_entity_manager')
|
||||
->getRepository('ChillMainBundle:Scope')
|
||||
->findAll();
|
||||
|
||||
|
||||
if (count($scopes) > 0) {
|
||||
$defaultScopeId = $scopes[0]->getId();
|
||||
} else {
|
||||
@@ -64,7 +80,7 @@ class Version20150622233319 extends AbstractMigration
|
||||
$nbReports = $this->container->get('doctrine.orm.default_entity_manager')
|
||||
->createQuery('SELECT count(r.id) FROM ChillReportBundle:Report r')
|
||||
->getSingleScalarResult();
|
||||
|
||||
|
||||
if ($nbReports > 0) {
|
||||
//create a default scope
|
||||
$scope = new Scope();
|
||||
@@ -90,27 +106,13 @@ class Version20150622233319 extends AbstractMigration
|
||||
$this->addSql('ALTER TABLE report ADD CONSTRAINT FK_report_scope '
|
||||
. 'FOREIGN KEY (scope_id) '
|
||||
. 'REFERENCES scopes (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
|
||||
if (isset($defaultScopeId)){
|
||||
$this->addSql('UPDATE report SET scope_id = :id', array(
|
||||
'id' => $defaultScopeId
|
||||
));
|
||||
|
||||
if (isset($defaultScopeId)) {
|
||||
$this->addSql('UPDATE report SET scope_id = :id', [
|
||||
'id' => $defaultScopeId,
|
||||
]);
|
||||
}
|
||||
$this->addSql('ALTER TABLE report ALTER COLUMN scope_id SET NOT NULL');
|
||||
$this->addSql('CREATE INDEX IDX_report_scope ON report (scope_id)');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql',
|
||||
'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE Report DROP CONSTRAINT FK_report_scope');
|
||||
$this->addSql('DROP INDEX IDX_report_scope');
|
||||
$this->addSql('ALTER TABLE Report ADD scope VARCHAR(255) DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE Report DROP scope_id');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user