DO $$ 
    BEGIN
          IF EXISTS (SELECT "Id" FROM public."Versions" WHERE "Build">=1765) THEN
		RETURN;
          END IF;
        BEGIN
           CREATE TABLE public."Departments" (
    "Id" bigserial NOT NULL,
    "Name" character varying(256) NOT NULL,
    "TypeId" int,
    "CustomerId" int,
    "Timestamp" timestamp without time zone DEFAULT timezone('utc', now())  NOT NULL, 
    "ParentId" bigint,
    "MainUserId" bigint NULL,
    PRIMARY KEY ("Name")
);

CREATE TABLE public."DepartmentUserRelation"
(
	"Id" bigserial NOT NULL,
	"DepartmentId" bigint NOT NULL,
	"UserId" bigint NOT NULL,
	"Timestamp" timestamp without time zone DEFAULT timezone('utc', now())  NOT NULL,
	PRIMARY KEY("DepartmentId","UserId")
);
        EXCEPTION
            WHEN duplicate_table THEN RAISE NOTICE 'Create table <Departments>.';
        END;
    END;
$$

