<?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 schema idm_reconcile</comment>
    <sql>
      CREATE SCHEMA IF NOT EXISTS idm_reconcile;
      ALTER SCHEMA idm_reconcile OWNER TO ervu_business_metrics;
    </sql>
  </changeSet>

  <changeSet id="0002" author="a.kalimullin">
    <comment>creat table domain</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS idm_reconcile.domain (
      id varchar(255) PRIMARY KEY,
      version int NOT NULL,
      modified timestamp without time zone,
      schema varchar(255) NOT NULL,
      name varchar(255),
      shortname varchar(255),
      fullname varchar(255),
      dns varchar(255),
      email varchar(255),
      phone varchar(255),
      address varchar(1024),
      postal_address varchar(1024),
      address_id varchar(255),
      postal_address_id varchar(255),
      military_code varchar(255),
      timezone varchar(255),
      reports_enabled boolean,
      inn varchar(255),
      leg varchar(255),
      ogrn varchar(255),
      region varchar(255),
      epgu_id varchar(255),
      type varchar(255),
      esia_employee_authorization boolean,
      default_s3_bucket varchar(255),
      opf varchar(255),
      kpp varchar(255),
      checking_account varchar(255),
      bik varchar(255),
      bank_name varchar(255),
      bank_correspondent_account varchar(255),
      oktmo varchar(255),
      okato varchar(255),
      gov_registration_date varchar(255),
      gov_organization_type varchar(255),
      alias_key varchar(255),
      pass_key varchar(255),
      certificate varchar(2048),
      account_number_tofk varchar(255),
      bik_tofk varchar(255),
      correspondent_bank_account_tofk varchar(255),
      name_tofk varchar(255),
      nsi_organization_id varchar(255),
      doc_handle varchar(255),
      division_type varchar(255),
      tns_department_id varchar(255),
      enabled boolean,
      parent varchar(255),
      region_id varchar(255),
      managed varchar(255)
      );

      ALTER TABLE idm_reconcile.domain OWNER TO ervu_business_metrics;
    </sql>
  </changeSet>
  
  <changeSet id="0003" author="a.kalimullin">
    <comment>create table role</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS idm_reconcile.role (
      id varchar(255) PRIMARY KEY,
      version int NOT NULL,
      modified timestamp without time zone,
      schema varchar(255) NOT NULL,
      name varchar(255),
      shortname varchar(255),
      display_name varchar(255),
      sessions_limit int,
      ervu_role boolean,
      imported int,
      description TEXT
      );

      ALTER TABLE idm_reconcile.role OWNER TO ervu_business_metrics;
    </sql>
  </changeSet>

  <changeSet id="0004" author="a.kalimulin">
    <comment>create table account and account_role</comment>
    <sql>
      CREATE TABLE IF NOT EXISTS idm_reconcile.account (
      id varchar(36) PRIMARY KEY,
      version int NOT NULL,
      modified timestamp without time zone,
      schema varchar(100) NOT NULL,
      start varchar(50),
      finish varchar(50),
      enabled boolean NOT NULL DEFAULT TRUE,
      position varchar(255),
      fio varchar(255),
      work_mail varchar(255),
      esia_account boolean NOT NULL DEFAULT FALSE,
      domain_id varchar(36)
      );

      ALTER TABLE idm_reconcile.account OWNER TO ervu_business_metrics;

      CREATE TABLE IF NOT EXISTS idm_reconcile.account_role (
      account_id varchar(36) NOT NULL,
      role_id varchar(36) NOT NULL,

      CONSTRAINT pk_account_role PRIMARY KEY (account_id, role_id),

      CONSTRAINT fk_account_role_account FOREIGN KEY (account_id)
      REFERENCES idm_reconcile.account (id)
      ON DELETE CASCADE
      );

      ALTER TABLE idm_reconcile.account_role OWNER TO ervu_business_metrics;
    </sql>
  </changeSet>
</databaseChangeLog>