Skip to content

Instantly share code, notes, and snippets.

@simo97
Created April 29, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simo97/89f49d64765b0e928cebd20f715469f0 to your computer and use it in GitHub Desktop.
Save simo97/89f49d64765b0e928cebd20f715469f0 to your computer and use it in GitHub Desktop.
-- creation de la table common_domain
CREATE TABLE public.common_domain
(
id SERIAL,
domain character varying(253) COLLATE pg_catalog."default" NOT NULL,
is_primary boolean NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT common_domain_pkey PRIMARY KEY (id),
CONSTRAINT common_domain_domain_key UNIQUE (domain),
CONSTRAINT common_domain_tenant_id_1965101a_fk_common_company_id FOREIGN KEY (tenant_id)
REFERENCES public.common_company (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
DEFERRABLE INITIALLY DEFERRED
)
-- creation de la table common_domaint (pour la copy)
CREATE TABLE public.common_domaint
(
id SERIAL,
domain character varying(253) COLLATE pg_catalog."default" NOT NULL,
is_primary boolean NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT common_domaint_pkey PRIMARY KEY (id),
CONSTRAINT common_domaint_domain_key UNIQUE (domain),
CONSTRAINT common_domaint_tenant_id_1965101a_fk_common_company_id FOREIGN KEY (tenant_id)
REFERENCES public.common_company (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
DEFERRABLE INITIALLY DEFERRED
)
--- function pgplsql
CREATE OR REPLACE FUNCTION syncData() RETURNS TRIGGER AS $$
BEGIN
-- SELECT 'domain' from common_domaint WHERE common_domaint.id = old.id;
UPDATE common_domaint SET "domain" = new.domain, is_primary= new.is_primary, tenant_id = new.tenant_id WHERE id = new.id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- assignation du trigger
CREATE TRIGGER syncData
AFTER UPDATE
ON common_domain
FOR EACH ROW
EXECUTE PROCEDURE syncData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment