-- apply changes
create table sql_model (
  sys_pk                        bigint generated by default as identity not null,
  deleted_on                    timestamp,
  current_approval_level        integer default 0 not null,
  required_approval_levels      integer default 0 not null,
  sql                           text not null,
  deleted                       boolean default false not null,
  version                       integer default 1 not null,
  created_at                    timestamp default 'now()' not null,
  modified_at                   timestamp default 'now()' not null,
  deleted_by                    varchar(255),
  approval_status               varchar(8) default 'APPROVED' not null,
  tags                          varchar[] default '{}' not null,
  comments                      jsonb default '[]' not null,
  tenant_id                     varchar(255) not null,
  sql_id                        varchar(255) not null,
  entity_name                   varchar(255) not null,
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_sql_model_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint sql_unique_id unique (entity_name,sql_id,tenant_id),
  constraint pk_sql_model primary key (sys_pk)
);

ALTER TABLE sql_model ADD FOREIGN KEY(tenant_id) REFERENCES tenant_model(name);
ALTER TABLE sql_model ADD FOREIGN KEY(entity_name) REFERENCES entity_model(name);
