CREATE TABLE IF NOT EXISTS category (
  id   BIGINT AUTO_INCREMENT
    PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  CONSTRAINT category_unique_name_constraint
  UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS ignored_category (
  id   BIGINT AUTO_INCREMENT
    PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  CONSTRAINT ignored_category_unique_name_constraint
  UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS model (
  id                  BIGINT AUTO_INCREMENT
    PRIMARY KEY,
  data_cube_reference VARCHAR(255) NULL,
  name                VARCHAR(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS model_tags (
  models_id BIGINT NOT NULL,
  tags_id   BIGINT NOT NULL,
  PRIMARY KEY (models_id, tags_id)
);

CREATE INDEX model_tags_by_id_index
  ON model_tags (tags_id);

CREATE TABLE IF NOT EXISTS tag (
  id          BIGINT AUTO_INCREMENT
    PRIMARY KEY,
  name        VARCHAR(255) NOT NULL,
  category_id BIGINT       NULL,
  CONSTRAINT tag_name_category_id_unique_constraint
  UNIQUE (name, category_id)
);

CREATE INDEX tags_by_category_id_index
  ON tag (category_id);

