@@ -29,13 +29,13 @@ 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
3233 $ this ->abortIf (
3334 'postgresql ' !== $ this ->connection ->getDatabasePlatform ()->getName (),
3435 'Migration can only be executed safely on \'postgresql \'. '
3536 );
3637
37- // Check if table already exists before creating
38- if (!$ schema ->hasTable ('swp_article_extra ' )) {
38+ if (!$ schema ->hasTable ('swp_article_extra ' )) {
3939 $ this ->addSql ('CREATE SEQUENCE swp_article_extra_id_seq INCREMENT BY 1 MINVALUE 1 START WITH 1 ' );
4040 $ this ->addSql (
4141 '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)) '
@@ -59,99 +59,78 @@ public function up(Schema $schema): void
5959
6060 public function down (Schema $ schema ): void
6161 {
62+ // this down() migration is auto-generated, please modify it to your needs
6263 $ this ->abortIf (
6364 'postgresql ' !== $ this ->connection ->getDatabasePlatform ()->getName (),
6465 'Migration can only be executed safely on \'postgresql \'. '
6566 );
6667
67- $ this ->addSql ('DROP SEQUENCE IF EXISTS swp_article_extra_id_seq CASCADE ' );
68- $ this ->addSql ('DROP TABLE IF EXISTS swp_article_extra ' );
68+ $ this ->addSql ('DROP SEQUENCE swp_article_extra_id_seq CASCADE ' );
69+ $ this ->addSql ('DROP TABLE swp_article_extra ' );
6970 }
7071
7172 public function postUp (Schema $ schema ): void
7273 {
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 ;
74+ $ entityManager = $ this ->container ->get ('doctrine.orm.default_entity_manager ' );
75+ $ entityManager ->getConnection ()->getConfiguration ()->setSQLLogger (null );
8976
90- $ totalArticles = $ entityManager
91- ->createQuery ('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a ' )
92- ->getSingleScalarResult ();
77+ $ batchSize = 500 ;
78+ $ numberOfRecordsPerPage = 2000 ;
9379
94- if ( $ totalArticles == 0 ) {
95- return ;
96- }
80+ $ totalArticles = $ entityManager
81+ -> createQuery ( ' SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a ' )
82+ -> getSingleScalarResult ();
9783
98- $ totalArticlesProcessed = 0 ;
84+ $ totalArticlesProcessed = 0 ;
85+ $ isProcessing = true ;
9986
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 ();
87+ while ($ isProcessing ) {
88+ $ sql = "SELECT id, extra FROM swp_article LIMIT $ numberOfRecordsPerPage OFFSET $ totalArticlesProcessed " ;
89+ $ query = $ entityManager ->getConnection ()->prepare ($ sql );
90+ $ query ->execute ();
91+ $ results = $ query ->fetchAll ();
10492
105- if (empty ($ results )) {
106- break ;
93+ foreach ($ results as $ result ) {
94+ $ legacyExtra = $ this ->unserializeExtraField ($ result ['extra ' ]);
95+ if (empty ($ legacyExtra )) {
96+ ++$ totalArticlesProcessed ;
97+ continue ;
10798 }
10899
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- );
100+ $ article = $ entityManager ->find (
101+ Article::class,
102+ $ result ['id ' ]
103+ );
120104
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 );
105+ foreach ($ legacyExtra as $ key => $ extraItem ) {
106+ if (is_array ($ extraItem )) {
107+ $ extra = ArticleExtraEmbedField::newFromValue ($ key , $ extraItem );
108+ } else {
109+ $ extra = ArticleExtraTextField::newFromValue ($ key , (string ) $ extraItem );
134110 }
111+ $ extra ->setArticle ($ article );
112+ $ entityManager ->persist ($ extra );
113+ }
135114
136- ++$ totalArticlesProcessed ;
137- if (0 == ($ totalArticlesProcessed % $ batchSize )) {
138- $ entityManager ->flush ();
139- $ entityManager ->clear ();
140- }
115+ ++$ totalArticlesProcessed ;
116+ if (0 == ($ totalArticlesProcessed % $ batchSize )) {
117+ $ entityManager ->flush ();
118+ $ entityManager ->clear ();
141119 }
142120 }
143121
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 ());
122+ // flush remaining entities in queue and break loop
123+ if ( $ totalArticlesProcessed === $ totalArticles ) {
124+ $ entityManager ->flush ();
125+ $ entityManager -> clear ();
126+ break ;
127+ }
150128 }
151129 }
152130
153131 private function unserializeExtraField (?string $ data )
154132 {
133+ // Handle null or empty data
155134 if (empty ($ data )) {
156135 return null ;
157136 }
0 commit comments