--up

CREATE TABLE voice_conversion_models (
  id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  uuid uuid UNIQUE NOT NULL DEFAULT gen_random_uuid(),
  created_at timestamp without time zone DEFAULT now() NOT NULL,
  author_id integer REFERENCES users(id),
  name text NOT NULL,
  archived boolean NOT NULL DEFAULT false
);

CREATE UNIQUE INDEX voice_conversion_models_author_id_name_uq ON voice_conversion_models(author_id, name) WHERE author_id IS NOT NULL AND NOT archived;
CREATE UNIQUE INDEX voice_conversion_models_no_author_id_name_uq ON voice_conversion_models(name) WHERE author_id IS NULL AND NOT archived;

CREATE TABLE voice_conversion_model_corpus (
  model_id integer NOT NULL REFERENCES voice_conversion_models(id),
  sample_id integer NOT NULL REFERENCES samples(id),
  UNIQUE (model_id, sample_id)
);

CREATE TABLE voice_conversion_model_training_runs (
  id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  model_id integer NOT NULL REFERENCES voice_conversion_models(id),
  created_at timestamp without time zone DEFAULT now() NOT NULL,
  state text NOT NULL DEFAULT 'pending',
  trainer_version text NOT NULL,
  progress real NOT NULL DEFAULT 0.0,
  s3_key text,
  error text
);

CREATE INDEX voice_conversion_model_training_runs_model_id_idx ON voice_conversion_model_training_runs(model_id);

--down

DROP TABLE voice_conversion_model_training_runs;
DROP TABLE voice_conversion_model_corpus;
DROP TABLE voice_conversion_models;