add trigger for canonicalization and anonymouse flag on 3party

This commit is contained in:
2021-10-07 21:52:30 +02:00
parent ca3ced0308
commit 2820ad83de
4 changed files with 131 additions and 9 deletions

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\ThirdParty;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Create trigger for canonicalisation on 3party + indexes
*/
final class Version20211007165001 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create trigger for canonicalisation on 3party + indexes';
}
public function up(Schema $schema): void
{
$this->addSql("
UPDATE chill_3party.third_party
SET canonicalized =
COALESCE(
UNACCENT(
LOWER(
name ||
CASE WHEN name <> '' THEN ' ' ELSE '' END ||
name_company ||
CASE WHEN name_company <> '' THEN ' ' ELSE '' END ||
acronym ||
CASE WHEN acronym <> '' THEN ' ' ELSE '' END
)
),
''
)
");
$this->addSql("
CREATE OR REPLACE FUNCTION chill_3party.canonicalize() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.canonicalized = UNACCENT(LOWER(
NEW.name ||
CASE WHEN NEW.name <> '' THEN ' ' ELSE '' END ||
NEW.name_company ||
CASE WHEN NEW.name_company <> '' THEN ' ' ELSE '' END ||
NEW.acronym ||
CASE WHEN NEW.acronym <> '' THEN ' ' ELSE '' END
));
return NEW;
END
$$
");
$this->addSql("
CREATE TRIGGER canonicalize_fullname_on_insert
BEFORE INSERT
ON chill_3party.third_party
FOR EACH ROW
EXECUTE procedure chill_3party.canonicalize();
");
$this->addSql("
CREATE TRIGGER canonicalize_fullname_on_update
BEFORE UPDATE
ON chill_3party.third_party
FOR EACH ROW
EXECUTE procedure chill_3party.canonicalize();
");
$this->addSql("
CREATE INDEX chill_custom_canonicalized_trgm_idx_gist
ON chill_3party.third_party USING GIST (canonicalized gist_trgm_ops)
");
}
public function down(Schema $schema): void
{
$this->addSql('DROP TRIGGER canonicalize_fullname_on_update ON chill_3party.third_party');
$this->addSql('DROP TRIGGER canonicalize_fullname_on_insert ON chill_3party.third_party');
$this->addSql('DROP FUNCTION chill_3party.canonicalize()');
$this->addSql("
DROP INDEX chill_3party.chill_custom_canonicalized_trgm_idx_gist
");
}
}

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\ThirdParty;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add anonymous flag for contacts
*/
final class Version20211007194942 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add anonymous flag for contacts';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party ADD contact_data_anonymous BOOLEAN DEFAULT \'false\' NOT NULL;');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party DROP contact_data_anonymous');
}
}