alter table music_sessions_history
add constraint music_sessions_history_pkey PRIMARY KEY (id);

CREATE TABLE likes
(
  id character varying(64) NOT NULL DEFAULT uuid_generate_v4(),
  liker_id character varying(64) NOT NULL,
  user_id character varying(64),
  band_id character varying(64),
  session_id character varying(64),
  recording_id character varying(64),
  likable_type character varying(25),
  created_at timestamp without time zone NOT NULL DEFAULT now(),
  updated_at timestamp without time zone NOT NULL DEFAULT now(),
  CONSTRAINT likes_pkey PRIMARY KEY (id),
  CONSTRAINT liker_fkey FOREIGN KEY (liker_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT user_likes_fkey FOREIGN KEY (user_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT bands_likes_fkey FOREIGN KEY (band_id)
      REFERENCES bands (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT session_likes_fkey FOREIGN KEY (session_id)
      REFERENCES music_sessions_history (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT recording_likes_fkey FOREIGN KEY (recording_id)
      REFERENCES recordings (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,      
  CONSTRAINT user_likes_uniqkey UNIQUE (liker_id, user_id),
  CONSTRAINT band_likes_uniqkey UNIQUE (liker_id, band_id),
  CONSTRAINT session_likes_uniqkey UNIQUE (liker_id, session_id),
  CONSTRAINT recording_likes_uniqkey UNIQUE (liker_id, recording_id)
);

CREATE TABLE followings
(
  id character varying(64) NOT NULL DEFAULT uuid_generate_v4(),
  follower_id character varying(64) NOT NULL,
  user_id character varying(64),
  band_id character varying(64),
  followable_type character varying(25),
  created_at timestamp without time zone NOT NULL DEFAULT now(),
  updated_at timestamp without time zone NOT NULL DEFAULT now(),
  CONSTRAINT followings_pkey PRIMARY KEY (id),
  CONSTRAINT follower_fkey FOREIGN KEY (follower_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT user_followings_fkey FOREIGN KEY (user_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT bands_followings_fkey FOREIGN KEY (band_id)
      REFERENCES bands (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT user_followings_uniqkey UNIQUE (follower_id, user_id),
  CONSTRAINT band_followings_uniqkey UNIQUE (follower_id, band_id)
);

