-- change jam_tracks PRIMARY KEY to VARCHAR(64)

-- first, drop all constraints and change the types
ALTER TABLE jam_track_tracks DROP CONSTRAINT jam_track_tracks_jam_track_id_fkey;
ALTER TABLE jam_track_tracks ALTER COLUMN jam_track_id TYPE VARCHAR(64);
ALTER TABLE jam_track_tap_ins DROP CONSTRAINT jam_track_tap_ins_jam_track_id_fkey;
ALTER TABLE jam_track_tap_ins ALTER COLUMN jam_track_id TYPE VARCHAR(64);
ALTER TABLE jam_track_rights DROP CONSTRAINT jam_track_rights_jam_track_id_fkey;
ALTER TABLE jam_track_rights ALTER COLUMN jam_track_id TYPE VARCHAR(64);
ALTER TABLE active_music_sessions ALTER COLUMN jam_track_id TYPE VARCHAR(64);
ALTER TABLE recordings DROP CONSTRAINT recordings_jam_track_id_fkey;
ALTER TABLE recordings ALTER COLUMN jam_track_id TYPE VARCHAR(64);
ALTER TABLE playable_plays DROP COLUMN jam_track_id;


-- then drop the jamtrack sequence, change it's type, and then set default to UUID
-- DROP SEQUENCE jam_tracks_next_seq;
ALTER TABLE jam_tracks ALTER COLUMN id TYPE VARCHAR(64);
ALTER TABLE jam_tracks ALTER COLUMN id SET DEFAULT uuid_generate_v4();

-- add back in all the constraints on the fk tables
ALTER TABLE jam_track_tracks ADD CONSTRAINT jam_track_tracks_jam_track_id_fkey FOREIGN KEY (jam_track_id) REFERENCES jam_tracks(id) ON DELETE CASCADE;
ALTER TABLE jam_track_tap_ins ADD CONSTRAINT jam_track_tap_ins_jam_track_id_fkey FOREIGN KEY (jam_track_id) REFERENCES jam_tracks(id) ON DELETE CASCADE;
ALTER TABLE jam_track_rights ADD CONSTRAINT jam_track_rights_jam_track_id_fkey FOREIGN KEY (jam_track_id) REFERENCES jam_tracks(id);
ALTER TABLE recordings ADD CONSTRAINT recordings_jam_track_id_fkey FOREIGN KEY (jam_track_id) REFERENCES jam_tracks(id);
