<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220120115405 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('
CREATE OR REPLACE FUNCTION public.fn_contract__matcher_for__real_estate__and__business_area(
integer,
integer)
RETURNS integer
LANGUAGE \'sql\'
COST 100
STABLE PARALLEL UNSAFE
AS $BODY$
WITH RECURSIVE business_area__by__contract(id, parent, contract, level) AS (
SELECT b.id, b.parent_id, c.id, 1
FROM public.business_areas b
LEFT JOIN public.contracts__business_areas cb ON b.id = cb.business_area_id
LEFT JOIN public.contracts c ON c.id = cb.contract_id
LEFT JOIN public.contracts__real_estates cr ON c.id=cr.contract_id
WHERE b.enabled=1 AND b.deleted = 0 AND c.enabled = 1 AND c.deleted = 0
AND cr.real_estate_id=$1
), business_area__enfant(id, parent, contract, level) AS (
SELECT * FROM business_area__by__contract
UNION ALL
SELECT b.id, b.parent_id, rec.contract, level+1
FROM business_area__enfant rec, public.business_areas b
WHERE b.parent_id=rec.id AND b.enabled = 1 AND b.deleted = 0
)
SELECT contract
FROM business_area__enfant
WHERE id=$2
ORDER BY level ASC LIMIT 1 OFFSET 0
$BODY$;
');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
}
}