Skip to content

Commit 12454c1

Browse files
committed
Update logic
1 parent 1caea8d commit 12454c1

File tree

1 file changed

+79
-48
lines changed

1 file changed

+79
-48
lines changed

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

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,63 +59,94 @@ public function down(Schema $schema): void
5959
$this->addSql('DROP SEQUENCE swp_article_extra_id_seq CASCADE');
6060
$this->addSql('DROP TABLE swp_article_extra');
6161
}
62-
6362
public function postUp(Schema $schema): void
6463
{
65-
$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
66-
$entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
67-
68-
$batchSize = 500;
69-
$numberOfRecordsPerPage = 2000;
70-
71-
$totalArticles = $entityManager
72-
->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a')
73-
->getSingleScalarResult();
74-
75-
$totalArticlesProcessed = 0;
76-
$isProcessing = true;
77-
78-
while ($isProcessing) {
79-
$sql = "SELECT id, extra FROM swp_article";
80-
$query = $entityManager->getConnection()->prepare($sql);
81-
$query->execute();
82-
$results = $query->fetchAll();
83-
84-
foreach ($results as $result) {
85-
$legacyExtra = $this->unserializeExtraField($result['extra']);
86-
if (empty($legacyExtra)) {
87-
++$totalArticlesProcessed;
88-
continue;
89-
}
64+
try {
65+
$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
66+
$connection = $entityManager->getConnection();
67+
$connection->getConfiguration()->setSQLLogger(null);
68+
69+
// Verify table exists
70+
$tableExists = $connection->executeQuery(
71+
"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'swp_article_extra')"
72+
)->fetchOne();
73+
74+
if (!$tableExists) {
75+
return;
76+
}
9077

91-
$article = $entityManager->find(
92-
Article::class,
93-
$result['id']
94-
);
78+
$batchSize = 500;
79+
$numberOfRecordsPerPage = 2000;
9580

96-
foreach ($legacyExtra as $key => $extraItem) {
97-
if (is_array($extraItem)) {
98-
$extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
99-
} else {
100-
$extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem);
101-
}
102-
$extra->setArticle($article);
103-
$entityManager->persist($extra);
81+
// Get total count
82+
$totalArticles = $entityManager
83+
->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a WHERE a.extra IS NOT NULL')
84+
->getSingleScalarResult();
85+
86+
if ($totalArticles == 0) {
87+
return;
88+
}
89+
90+
$totalArticlesProcessed = 0;
91+
92+
// Pagination loop
93+
while ($totalArticlesProcessed < $totalArticles) {
94+
$sql = "SELECT id, extra FROM swp_article WHERE extra IS NOT NULL ORDER BY id LIMIT ? OFFSET ?";
95+
$query = $connection->prepare($sql);
96+
$results = $query->executeQuery([$numberOfRecordsPerPage, $totalArticlesProcessed])->fetchAllAssociative();
97+
98+
// Break if no results (end of data)
99+
if (empty($results)) {
100+
break;
104101
}
105102

106-
++$totalArticlesProcessed;
107-
if (0 == ($totalArticlesProcessed % $batchSize)) {
108-
$entityManager->flush();
109-
$entityManager->clear();
103+
foreach ($results as $result) {
104+
try {
105+
$legacyExtra = $this->unserializeExtraField($result['extra']);
106+
if (empty($legacyExtra)) {
107+
++$totalArticlesProcessed;
108+
continue;
109+
}
110+
111+
$article = $entityManager->find(
112+
Article::class,
113+
$result['id']
114+
);
115+
116+
if (!$article) {
117+
++$totalArticlesProcessed;
118+
continue;
119+
}
120+
121+
foreach ($legacyExtra as $key => $extraItem) {
122+
if (is_array($extraItem)) {
123+
$extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
124+
} else {
125+
$extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem);
126+
}
127+
$extra->setArticle($article);
128+
$entityManager->persist($extra);
129+
}
130+
131+
++$totalArticlesProcessed;
132+
if (0 == ($totalArticlesProcessed % $batchSize)) {
133+
$entityManager->flush();
134+
$entityManager->clear();
135+
}
136+
} catch (\Exception $e) {
137+
++$totalArticlesProcessed;
138+
error_log('Error processing article ' . $result['id'] . ': ' . $e->getMessage());
139+
continue;
140+
}
110141
}
111142
}
112143

113-
// flush remaining entities in queue and break loop
114-
if ($totalArticlesProcessed === $totalArticles) {
115-
$entityManager->flush();
116-
$entityManager->clear();
117-
break;
118-
}
144+
// Flush remaining entities
145+
$entityManager->flush();
146+
$entityManager->clear();
147+
} catch (\Exception $e) {
148+
error_log('postUp error: ' . $e->getMessage());
149+
throw $e;
119150
}
120151
}
121152

0 commit comments

Comments
 (0)