Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-3814 do not update table if exist and not empty #4260

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions lib/classes/patch/416RC3PHRAS3602.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Alchemy\Phrasea\Application;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\ResultSetMapping;

class patch_416RC3PHRAS3602 implements patchInterface
{
Expand Down Expand Up @@ -63,24 +65,28 @@ private function patch_databox(base $databox, Application $app)

private function patch_appbox(base $appbox, Application $app)
{
$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
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
(
Expand All @@ -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();
}
}
Loading