CREATE TABLE fanbox_newsletters_temp_old(
  id varchar NOT NULL,
  user_id varchar NOT NULL,
  content text NOT NULL,
  added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  published timestamp,
  PRIMARY KEY (id)
);

CREATE TABLE fanbox_newsletters(
  user_id varchar NOT NULL,
  hash varchar NOT NULL,
  "content" varchar NOT NULL,
  added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  published timestamp NOT NULL,
  CONSTRAINT fanbox_newsletters_pkey PRIMARY KEY (user_id, hash)
);

CREATE TABLE fanbox_embeds(
  id varchar NOT NULL,
  user_id varchar NOT NULL,
  post_id varchar NOT NULL,
  type varchar NOT NULL,
  json varchar NOT NULL,
  added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  processed varchar,
  "iframely_key" varchar(255) NULL,
  "iframely_data" jsonb NULL,
  "iframely_url" text NULL,
  PRIMARY KEY (id)
);

CREATE TABLE fanbox_fancards(
  id serial PRIMARY KEY,
  user_id varchar NOT NULL,
  file_id int REFERENCES files(id),
  last_checked_at timestamp DEFAULT CURRENT_TIMESTAMP,
  price text NOT NULL DEFAULT '',
  CONSTRAINT fanbox_fancards_user_id_file_id_price_unique_idx UNIQUE (user_id, file_id, price)
);

CREATE INDEX fanbox_newsletters_temp_old_user_id_idx ON fantia_newsletters USING btree(user_id);

CREATE INDEX fanbox_newsletters_temp_old_added_idx ON fantia_newsletters USING btree(added);

CREATE INDEX fanbox_newsletters_temp_old_published_idx ON fantia_newsletters USING btree(published);

CREATE INDEX fanbox_newsletters_user_id_published_idx ON public.fanbox_newsletters_temp_new USING btree(user_id, published);

CREATE INDEX fanbox_embeds_user_id_idx ON fanbox_embeds USING btree(user_id);

CREATE INDEX fanbox_embeds_post_id_idx ON fanbox_embeds USING btree(post_id);

CREATE INDEX fanbox_embeds_added_idx ON fanbox_embeds USING btree(added);

CREATE INDEX fanbox_embeds_type_idx ON fanbox_embeds USING btree(type);

CREATE INDEX fanbox_fancards_user_id_idx ON fanbox_fancards USING btree(user_id);

CREATE UNIQUE INDEX fanbox_fancards_null_file_id_user_id_price_unique_idx ON fanbox_fancards(user_id, price)
WHERE
  file_id IS NULL;
