From e014e0101cc6b2d63219a284961db18af42ed362 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 24 Feb 2023 18:35:45 +0300 Subject: [PATCH] do not update table if yet exist --- lib/classes/patch/416RC3PHRAS3602.php | 65 +++++++++++++++++---------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/classes/patch/416RC3PHRAS3602.php b/lib/classes/patch/416RC3PHRAS3602.php index eddc8b9d5e..29313050d9 100644 --- a/lib/classes/patch/416RC3PHRAS3602.php +++ b/lib/classes/patch/416RC3PHRAS3602.php @@ -1,6 +1,8 @@ get_connection(); - $sqls = [ - /** - * the sessions go directly to baskets, let's call this a "vote session" to not mispatch with former structure - */ - "update Baskets b inner join ValidationSessions v on v.basket_id=b.id set + if ($this->tableExistsAndNotEmpty($app['orm.em'], 'BasketElementVotes')) { + // the table exist and not empty + return 0; + } else { + $cnx = $appbox->get_connection(); + $sqls = [ + /** + * the sessions go directly to baskets, let's call this a "vote session" to not mispatch with former structure + */ + "update Baskets b inner join ValidationSessions v on v.basket_id=b.id set b.vote_initiator_id=v.initiator_id, b.vote_created=v.created, b.vote_updated=v.updated, b.vote_expires=v.expires", - /** - * the participants are now in relation with a basket - * nb: now a participant is kept unique to a basket, it was not the case before (very rare doubles ? bug ?) - * we keep only the _most recent_ occurence ( max(p.id) ) - * nb: we let the participant id unchanged because it's easier when copying "validationdatas" - */ - "insert into BasketParticipants (id, user_id, basket_id, can_modify, + /** + * the participants are now in relation with a basket + * nb: now a participant is kept unique to a basket, it was not the case before (very rare doubles ? bug ?) + * we keep only the _most recent_ occurence ( max(p.id) ) + * nb: we let the participant id unchanged because it's easier when copying "validationdatas" + */ + "insert into BasketParticipants (id, user_id, basket_id, can_modify, is_aware, is_confirmed, can_agree, can_see_others, reminded) select vp_id as id, user_id, basket_id, 0 as can_modify, is_aware, is_confirmed, can_agree, can_see_others, reminded from ( @@ -94,23 +100,36 @@ private function patch_appbox(base $appbox, Application $app) ) as a inner join ValidationParticipants p on p.id=a.vp_id order by vp_id asc", - /** - * the "datas" are now "votes" - * we don't copy orphan data - * nb: we let the id unchanged - */ - "insert into BasketElementVotes (id, participant_id, basket_element_id, agreement, note, updated) + /** + * the "datas" are now "votes" + * we don't copy orphan data + * nb: we let the id unchanged + */ + "insert into BasketElementVotes (id, participant_id, basket_element_id, agreement, note, updated) SELECT d.id, d.participant_id, d.basket_element_id, d.agreement, d.note, d.updated FROM ValidationDatas d inner join BasketParticipants p on p.id=d.participant_id inner join BasketElements e on e.id=d.basket_element_id order by d.id asc", - "UPDATE `Baskets` SET `share_expires`=`vote_expires` + "UPDATE `Baskets` SET `share_expires`=`vote_expires` WHERE `share_expires` IS NULL AND `vote_expires` IS NOT NULL AND `vote_expires` < NOW();" - ]; + ]; - foreach($sqls as $sql) { - $cnx->exec($sql); + foreach($sqls as $sql) { + $cnx->exec($sql); + } + return 0; } } + + private function tableExistsAndNotEmpty(EntityManager $em, $table) + { + $rsm = (new ResultSetMapping()) + ->addScalarResult('Name', 'Name') + ->addScalarResult('Rows', 'Rows'); + + return (Boolean) $em->createNativeQuery( + 'SHOW TABLE STATUS WHERE Name="'.$table.'" AND `Rows` >=1', $rsm + )->getOneOrNullResult(); + } }