Feature: avoid duplicates for the same period in acc period user history

This commit is contained in:
Julien Fastré 2023-06-27 15:30:44 +02:00
parent 90be68002a
commit a7c3089736
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,28 @@
kind: Feature
body: "Force accompanying period user history to be unique for the same period and
stardate/enddate [:warning: may encounter migration issue]"
time: 2023-06-27T15:16:15.775571488+02:00
custom:
Issue: ""
Long: "If some issue is encountered during migration, use this SQL to find the line which are in conflict, examine the problem and delete some of the concerning line
```sql
-- to see the line which are in conflict with another one
SELECT o.*
FROM chill_person_accompanying_period_user_history o
JOIN chill_person_accompanying_period_user_history c ON o.id < c.id AND o.accompanyingperiod_id = c.accompanyingperiod_id
WHERE tsrange(o.startdate, o.enddate, '[)') && tsrange(c.startdate, c.enddate, '[)')
ORDER BY accompanyingperiod_id;
-- to examine line in conflict for a given accompanyingperiod_id (given by the previous query)
SELECT * FROM chill_person_accompanying_period_user_history WHERE accompanyingperiod_id = IIIIDDDD order by startdate, enddate;
```
"

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20230627130331 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add constraint to force unicity on chill_person_accompanying_period_user_history';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_user_history
ADD CONSTRAINT acc_period_user_history_not_overlaps
EXCLUDE USING GIST (accompanyingperiod_id with =, tsrange(startdate, enddate) with &&)
DEFERRABLE INITIALLY DEFERRED');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_user_history DROP CONSTRAINT acc_period_user_history_not_overlaps');
}
}