<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

  <changeSet id="0001" author="a.kalimullin">
    <comment>Create table user_application_role</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.user_application_role (
      user_role_id VARCHAR(36) PRIMARY KEY,
      role_name VARCHAR(255) UNIQUE NOT NULL,
      created TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
      updated TIMESTAMP WITHOUT TIME ZONE DEFAULT now()
      )
      TABLESPACE pg_default;

      ALTER TABLE IF EXISTS public.user_application_role
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.user_application_role TO ervu_account_applications;


      COMMENT ON COLUMN public.user_application_role.user_role_id IS 'Уникальный идентификатор роли пользователя';
      COMMENT ON COLUMN public.user_application_role.role_name IS 'Название роли пользователя';
      COMMENT ON COLUMN public.user_application_role.created IS 'Дата и время создания записи';
      COMMENT ON COLUMN public.user_application_role.updated IS 'Дата и время последнего обновления записи';
    </sql>
  </changeSet>

  <changeSet id="0002" author="a.kalimullin">
    <comment>Drop table public.link_user_application_user_group</comment>
    <sql>
      DROP TABLE IF EXISTS public.link_user_application_user_group CASCADE;
    </sql>
  </changeSet>

  <changeSet id="0003" author="a.kalimullin">
    <comment>Create table link_user_application_user_application_role</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS public.link_user_application_user_application_role (
      link_user_application_user_role_id bigserial,
      user_application_list_id bigint NOT NULL,
      user_role_id VARCHAR(36) NOT NULL,
      created TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
      CONSTRAINT pk_link_user_application_user_application_role PRIMARY KEY (link_user_application_user_role_id),
      CONSTRAINT uni_user_application_role UNIQUE (user_application_list_id, user_role_id),
      CONSTRAINT fk_user_application_list FOREIGN KEY (user_application_list_id)
      REFERENCES public.user_application_list (user_application_list_id) MATCH SIMPLE
      ON UPDATE NO ACTION
      ON DELETE CASCADE,
      CONSTRAINT fk_user_role FOREIGN KEY (user_role_id)
      REFERENCES public.user_application_role (user_role_id) MATCH SIMPLE
      ON UPDATE NO ACTION
      ON DELETE NO ACTION
      ) TABLESPACE pg_default;

      ALTER TABLE IF EXISTS public.link_user_application_user_application_role
      OWNER TO ervu_account_applications;

      GRANT ALL ON TABLE public.link_user_application_user_application_role TO ervu_account_applications;
    </sql>
  </changeSet>
</databaseChangeLog>