1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SWP\Migrations; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Doctrine\Migrations\AbstractMigration; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Auto-generated Migration: Please modify to your needs! |
14
|
|
|
*/ |
15
|
|
|
final class Version20200622125414 extends AbstractMigration implements ContainerAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ContainerInterface |
19
|
|
|
*/ |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
|
|
public function setContainer(ContainerInterface $container = null) |
23
|
|
|
{ |
24
|
|
|
$this->container = $container; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function up(Schema $schema): void |
28
|
|
|
{ |
29
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
30
|
|
|
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.'); |
31
|
|
|
|
32
|
|
|
$this->addSql('CREATE SEQUENCE swp_article_metadata_place_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); |
33
|
|
|
$this->addSql('CREATE SEQUENCE swp_article_metadata_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); |
34
|
|
|
$this->addSql('CREATE SEQUENCE swp_article_metadata_service_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); |
35
|
|
|
$this->addSql('CREATE SEQUENCE swp_article_metadata_subject_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); |
36
|
|
|
$this->addSql('CREATE TABLE swp_article_metadata_place (id INT NOT NULL, metadata_id INT DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, world_region VARCHAR(255) DEFAULT NULL, state VARCHAR(255) DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, qgroup VARCHAR(255) DEFAULT NULL, qcode VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'); |
37
|
|
|
$this->addSql('CREATE INDEX IDX_6C173802DC9EE959 ON swp_article_metadata_place (metadata_id)'); |
38
|
|
|
$this->addSql('CREATE TABLE swp_article_metadata (id INT NOT NULL, article_id INT DEFAULT NULL, profile VARCHAR(255) DEFAULT NULL, priority INT DEFAULT NULL, urgency INT DEFAULT NULL, ed_note VARCHAR(255) DEFAULT NULL, language VARCHAR(255) DEFAULT NULL, genre VARCHAR(255) DEFAULT NULL, guid VARCHAR(255) DEFAULT NULL, located VARCHAR(255) DEFAULT NULL, byline VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'); |
39
|
|
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_EEF4773C7294869C ON swp_article_metadata (article_id)'); |
40
|
|
|
$this->addSql('CREATE TABLE swp_article_metadata_service (id INT NOT NULL, metadata_id INT DEFAULT NULL, code VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); |
41
|
|
|
$this->addSql('CREATE INDEX IDX_779FF189DC9EE959 ON swp_article_metadata_service (metadata_id)'); |
42
|
|
|
$this->addSql('CREATE TABLE swp_article_metadata_subject (id INT NOT NULL, metadata_id INT DEFAULT NULL, code VARCHAR(255) NOT NULL, scheme VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'); |
43
|
|
|
$this->addSql('CREATE INDEX IDX_6DCC5521DC9EE959 ON swp_article_metadata_subject (metadata_id)'); |
44
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_place ADD CONSTRAINT FK_6C173802DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); |
45
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata ADD CONSTRAINT FK_EEF4773C7294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); |
46
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_service ADD CONSTRAINT FK_779FF189DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); |
47
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_subject ADD CONSTRAINT FK_6DCC5521DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); |
48
|
|
|
$this->addSql('ALTER TABLE swp_item ADD profile VARCHAR(255) DEFAULT NULL'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function postUp(Schema $schema): void |
52
|
|
|
{ |
53
|
|
|
$metadataFactory = $this->container->get('swp.factory.metadata'); |
54
|
|
|
$entityManager = $this->container->get('doctrine.orm.default_entity_manager'); |
55
|
|
|
$query = $entityManager |
56
|
|
|
->createQuery('SELECT a FROM SWP\Bundle\CoreBundle\Model\Article a'); |
57
|
|
|
|
58
|
|
|
$batchSize = 20; |
59
|
|
|
$i = 1; |
60
|
|
|
$iterableResult = $query->iterate(); |
61
|
|
|
foreach ($iterableResult as $row) { |
62
|
|
|
$article = $row[0]; |
63
|
|
|
$legacyMetadata = $article->getMetadata(); |
64
|
|
|
if (empty($legacyMetadata)) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$metadata = $metadataFactory->createFrom($legacyMetadata); |
69
|
|
|
|
70
|
|
|
$entityManager->persist($metadata); |
71
|
|
|
|
72
|
|
|
$article->setData($metadata); |
73
|
|
|
|
74
|
|
|
if (0 === ($i % $batchSize)) { |
75
|
|
|
$entityManager->flush(); |
76
|
|
|
$entityManager->clear(); |
77
|
|
|
} |
78
|
|
|
++$i; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$entityManager->flush(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function down(Schema $schema): void |
85
|
|
|
{ |
86
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
87
|
|
|
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.'); |
88
|
|
|
|
89
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_place DROP CONSTRAINT FK_6C173802DC9EE959'); |
90
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_service DROP CONSTRAINT FK_779FF189DC9EE959'); |
91
|
|
|
$this->addSql('ALTER TABLE swp_article_metadata_subject DROP CONSTRAINT FK_6DCC5521DC9EE959'); |
92
|
|
|
$this->addSql('DROP SEQUENCE swp_article_metadata_place_id_seq CASCADE'); |
93
|
|
|
$this->addSql('DROP SEQUENCE swp_article_metadata_id_seq CASCADE'); |
94
|
|
|
$this->addSql('DROP SEQUENCE swp_article_metadata_service_id_seq CASCADE'); |
95
|
|
|
$this->addSql('DROP SEQUENCE swp_article_metadata_subject_id_seq CASCADE'); |
96
|
|
|
$this->addSql('DROP TABLE swp_article_metadata_place'); |
97
|
|
|
$this->addSql('DROP TABLE swp_article_metadata'); |
98
|
|
|
$this->addSql('DROP TABLE swp_article_metadata_service'); |
99
|
|
|
$this->addSql('DROP TABLE swp_article_metadata_subject'); |
100
|
|
|
$this->addSql('ALTER TABLE swp_item DROP profile'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|