Skip to content

Commit 254b528

Browse files
committed
Fix
1 parent e520cb1 commit 254b528

File tree

1 file changed

+87
-58
lines changed

1 file changed

+87
-58
lines changed

src/SWP/Bundle/CoreBundle/Migrations/2021/01/Version20210112135542.php

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,100 +29,129 @@ public function setContainer(ContainerInterface $container = null)
2929

3030
public function up(Schema $schema): void
3131
{
32-
// this up() migration is auto-generated, please modify it to your needs
3332
$this->abortIf(
3433
'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
3534
'Migration can only be executed safely on \'postgresql\'.'
3635
);
3736

38-
//$this->addSql('CREATE SEQUENCE IF NOT EXISTS swp_article_extra_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
39-
$this->addSql('CREATE SEQUENCE IF NOT EXISTS swp_article_extra_id_seq INCREMENT BY 1 MINVALUE 1 START WITH 1');
40-
$this->addSql(
41-
'CREATE TABLE IF NOT EXISTS swp_article_extra (id INT NOT NULL, article_id INT DEFAULT NULL, field_name VARCHAR(255) NOT NULL, discr VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, embed VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'
42-
);
43-
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_9E61B3177294869C ON swp_article_extra (article_id)');
44-
$this->addSql(
45-
'ALTER TABLE swp_article_extra DROP CONSTRAINT IF EXISTS FK_9E61B3177294869C'
46-
);
47-
$this->addSql(
48-
'ALTER TABLE swp_article_extra ADD CONSTRAINT FK_9E61B3177294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'
49-
);
37+
// Check if table already exists before creating
38+
if (!$schema->hasTable('swp_article_extra')) {
39+
$this->addSql('CREATE SEQUENCE swp_article_extra_id_seq INCREMENT BY 1 MINVALUE 1 START WITH 1');
40+
$this->addSql(
41+
'CREATE TABLE swp_article_extra (id INT NOT NULL DEFAULT nextval(\'swp_article_extra_id_seq\'), article_id INT DEFAULT NULL, field_name VARCHAR(255) NOT NULL, discr VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, embed VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'
42+
);
43+
$this->addSql('CREATE INDEX IDX_9E61B3177294869C ON swp_article_extra (article_id)');
44+
45+
// Add constraint immediately after table creation
46+
$this->addSql(
47+
'ALTER TABLE swp_article_extra ADD CONSTRAINT FK_9E61B3177294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'
48+
);
49+
} else {
50+
// Table exists, just ensure constraint is in place
51+
$this->addSql(
52+
'ALTER TABLE swp_article_extra DROP CONSTRAINT IF EXISTS FK_9E61B3177294869C'
53+
);
54+
$this->addSql(
55+
'ALTER TABLE swp_article_extra ADD CONSTRAINT FK_9E61B3177294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'
56+
);
57+
}
5058
}
5159

5260
public function down(Schema $schema): void
5361
{
54-
// this down() migration is auto-generated, please modify it to your needs
5562
$this->abortIf(
5663
'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
5764
'Migration can only be executed safely on \'postgresql\'.'
5865
);
5966

60-
$this->addSql('DROP SEQUENCE swp_article_extra_id_seq CASCADE');
61-
$this->addSql('DROP TABLE swp_article_extra');
67+
$this->addSql('DROP SEQUENCE IF EXISTS swp_article_extra_id_seq CASCADE');
68+
$this->addSql('DROP TABLE IF EXISTS swp_article_extra');
6269
}
6370

6471
public function postUp(Schema $schema): void
6572
{
66-
$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
67-
$entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
73+
try {
74+
$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
75+
$connection = $entityManager->getConnection();
76+
$connection->getConfiguration()->setSQLLogger(null);
77+
78+
// Verify table exists before processing
79+
$tableExists = $connection->executeQuery(
80+
"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'swp_article_extra')"
81+
)->fetchOne();
82+
83+
if (!$tableExists) {
84+
return;
85+
}
86+
87+
$batchSize = 500;
88+
$numberOfRecordsPerPage = 2000;
6889

69-
$batchSize = 500;
70-
$numberOfRecordsPerPage = 2000;
90+
$totalArticles = $entityManager
91+
->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a')
92+
->getSingleScalarResult();
7193

72-
$totalArticles = $entityManager
73-
->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a')
74-
->getSingleScalarResult();
94+
if ($totalArticles == 0) {
95+
return;
96+
}
7597

76-
$totalArticlesProcessed = 0;
77-
$isProcessing = true;
98+
$totalArticlesProcessed = 0;
7899

79-
while ($isProcessing) {
80-
$sql = "SELECT id, extra FROM swp_article LIMIT $numberOfRecordsPerPage OFFSET $totalArticlesProcessed";
81-
$query = $entityManager->getConnection()->prepare($sql);
82-
$query->execute();
83-
$results = $query->fetchAll();
100+
while ($totalArticlesProcessed < $totalArticles) {
101+
$sql = "SELECT id, extra FROM swp_article WHERE extra IS NOT NULL LIMIT ? OFFSET ?";
102+
$query = $connection->prepare($sql);
103+
$results = $query->executeQuery([$numberOfRecordsPerPage, $totalArticlesProcessed])->fetchAllAssociative();
84104

85-
foreach ($results as $result) {
86-
$legacyExtra = $this->unserializeExtraField($result['extra']);
87-
if (empty($legacyExtra)) {
88-
++$totalArticlesProcessed;
89-
continue;
105+
if (empty($results)) {
106+
break;
90107
}
91108

92-
$article = $entityManager->find(
93-
Article::class,
94-
$result['id']
95-
);
109+
foreach ($results as $result) {
110+
$legacyExtra = $this->unserializeExtraField($result['extra']);
111+
if (empty($legacyExtra)) {
112+
++$totalArticlesProcessed;
113+
continue;
114+
}
115+
116+
$article = $entityManager->find(
117+
Article::class,
118+
$result['id']
119+
);
96120

97-
foreach ($legacyExtra as $key => $extraItem) {
98-
if (is_array($extraItem)) {
99-
$extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
100-
} else {
101-
$extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem);
121+
if (!$article) {
122+
++$totalArticlesProcessed;
123+
continue;
124+
}
125+
126+
foreach ($legacyExtra as $key => $extraItem) {
127+
if (is_array($extraItem)) {
128+
$extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
129+
} else {
130+
$extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem);
131+
}
132+
$extra->setArticle($article);
133+
$entityManager->persist($extra);
102134
}
103-
$extra->setArticle($article);
104-
$entityManager->persist($extra);
105-
}
106135

107-
++$totalArticlesProcessed;
108-
if (0 == ($totalArticlesProcessed % $batchSize)) {
109-
$entityManager->flush();
110-
$entityManager->clear();
136+
++$totalArticlesProcessed;
137+
if (0 == ($totalArticlesProcessed % $batchSize)) {
138+
$entityManager->flush();
139+
$entityManager->clear();
140+
}
111141
}
112142
}
113143

114-
// flush remaining entities in queue and break loop
115-
if ($totalArticlesProcessed === $totalArticles) {
116-
$entityManager->flush();
117-
$entityManager->clear();
118-
break;
119-
}
144+
// Flush remaining entities
145+
$entityManager->flush();
146+
$entityManager->clear();
147+
} catch (\Exception $e) {
148+
// Log error but don't fail migration
149+
error_log('postUp error: ' . $e->getMessage());
120150
}
121151
}
122152

123153
private function unserializeExtraField(?string $data)
124154
{
125-
// Handle null or empty data
126155
if (empty($data)) {
127156
return null;
128157
}

0 commit comments

Comments
 (0)