ALTER TABLE rsvp_requests ADD COLUMN music_session_id VARCHAR(64) REFERENCES music_sessions(id);
ALTER TABLE rsvp_requests ADD COLUMN chosen boolean DEFAULT FALSE NOT NULL;

--UPDATE rsvp_requests set CHOSEN = TRUE, music_session_id = b.music_session_id FROM (SELECT music_sessions.id as music_session_id, rsvp_requests.id as rsvp_request_id FROM music_sessions JOIN rsvp_slots ON music_sessions.id = rsvp_slots.music_session_id JOIN rsvp_requests_rsvp_slots ON rsvp_requests_rsvp_slots.rsvp_slot_id = rsvp_slots.id JOIN rsvp_requests ON rsvp_requests.id = rsvp_requests_rsvp_slots.rsvp_request_id WHERE rsvp_requests_rsvp_slots.chosen  = TRUE  where music_sessions.created_at > '2020-01-01') b WHERE rsvp_requests.id = rsvp_request_id;

ALTER TABLE music_sessions ADD COLUMN friends_can_join boolean DEFAULT FALSE NOT NULL;
CREATE INDEX rsvp_request_music_session_id ON rsvp_requests USING btree (music_session_id);

DROP TRIGGER tsvectorupdate on music_sessions;
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
ON music_sessions FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger(description_tsv, 'public.jamenglish', description, name, genre_id);

UPDATE music_sessions set description = description where id in (select music_sessions.id from music_sessions inner join active_music_sessions on active_music_sessions.id = music_sessions.id);


------

create sequence connections_client_id_int_seq;
alter table connections add column client_id_int int not null default nextval('connections_client_id_int_seq');
--ALTER SEQUENCE connections_client_id_int_seq OWNED BY connections.id;

create sequence music_sessions_id_int_seq;
alter table music_sessions add column music_session_id_int int not null default nextval('music_sessions_id_int_seq');

CREATE TABLE arses (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    name VARCHAR(200) NOT NULL UNIQUE NOT NULL,
    id_int SMALLINT UNIQUE NOT NULL,
    ip VARCHAR(200) UNIQUE NOT NULL,
    active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

alter table arses alter column name TYPE varchar(200);
ALTER TABLE arses DROP COLUMN ip;
ALTER TABLE arses DROP COLUMN id_int;
--ALTER TABLE arses DROP COLUMN provider;
--ALTER TABLE arses DROP COLUMN provider_instance_id;
ALTER TABLE arses ADD COLUMN provider varchar(20) NOT NULL default 'gcp';

create sequence arses_id_int_seq;
alter table arses add column id_int int not null default nextval('arses_id_int_seq');

ALTER TABLE arses ADD COLUMN ip VARCHAR(200);
ALTER TABLE arses ADD COLUMN username VARCHAR(200) default 'smoketest';
ALTER TABLE arses ADD COLUMN password VARCHAR(200) default 'foolishcharmer';
ALTER TABLE arses ADD COLUMN port int default 3478;

------

ALTER TABLE generic_state ADD COLUMN top_message VARCHAR(100000);

ALTER TABLE users ADD COLUMN beta BOOLEAN default FALSE;
ALTER TABLE arses ADD COLUMN beta BOOLEAN default FALSE;


ALTER TABLE generic_state ADD COLUMN event_page_top_logo_url VARCHAR(100000) DEFAULT '/assets/event/eventbrite-logo.png';

ALTER TABLE schools ADD CONSTRAINT schools_name_uniqkey UNIQUE (name);
ALTER TABLE schools ADD COLUMN school_tag VARCHAR(100) UNIQUE;
ALTER TABLE active_music_sessions ADD COLUMN school_id INT;
ALTER TABLE active_music_sessions ADD COLUMN is_platform_instructor BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE active_music_sessions ADD CONSTRAINT active_music_sessions_school_id_fkey FOREIGN KEY (school_id) REFERENCES schools(id);
ALTER TABLE music_sessions ADD COLUMN school_id INT;
ALTER TABLE music_sessions ADD CONSTRAINT music_sessions_school_id_fkey FOREIGN KEY (school_id) REFERENCES schools(id);
ALTER TABLE music_sessions ADD COLUMN is_platform_instructor BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE recordings ADD COLUMN school_id INT;
ALTER TABLE recordings ADD COLUMN is_platform_instructor BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE feeds ADD COLUMN school_id INT;
ALTER TABLE feeds ADD COLUMN is_platform_instructor BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE users ADD COLUMN is_platform_instructor BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE users ADD COLUMN import_source varchar(50);
ALTER TABLE users ADD COLUMN license_start date;
ALTER TABLE users ADD COLUMN license_end date;


CREATE INDEX users_is_a_student_idx ON users((1)) WHERE is_a_student;
CREATE INDEX users_is_a_teacher_idx ON users((1)) WHERE is_a_teacher;
CREATE INDEX users_is_platform_instructor_idx ON users((1)) WHERE is_platform_instructor;
CREATE INDEX ams_is_platform_instructor_idx ON active_music_sessions((1)) WHERE is_platform_instructor;
CREATE INDEX sms_is_platform_instructor_idx ON music_sessions((1)) WHERE is_platform_instructor;
CREATE INDEX users_schood_id_idx ON users USING btree (school_id);
CREATE INDEX feeds_schood_id_idx ON feeds USING btree (school_id);
CREATE INDEX feeds_is_platform_instructor_idx ON feeds((1)) WHERE is_platform_instructor;

ALTER TABLE arses ADD COLUMN country VARCHAR(200);
ALTER TABLE arses ADD COLUMN city VARCHAR(200);
ALTER TABLE arses ADD COLUMN latitude NUMERIC(15,10);
ALTER TABLE arses ADD COLUMN longitude NUMERIC(15,10);
ALTER TABLE arses ADD COLUMN subdivision VARCHAR(200);
ALTER TABLE arses ADD COLUMN continent VARCHAR(200);


ALTER TABLE users ADD COLUMN recurly_subscription_id VARCHAR(100) DEFAULT NULL;
ALTER TABLE users ADD COLUMN recurly_token VARCHAR(200) DEFAULT NULL;
ALTER TABLE users ADD COLUMN recurly_subscription_state VARCHAR(20) DEFAULT NULL;
ALTER TABLE users ADD COLUMN subscription_plan_code VARCHAR(100) DEFAULT NULL;
ALTER TABLE users ADD COLUMN desired_plan_code VARCHAR(100) DEFAULT NULL;
ALTER TABLE users ADD COLUMN admin_override_plan_code VARCHAR(100) DEFAULT NULL;
ALTER TABLE users ADD COLUMN admin_override_ends_at DATE;
ALTER TABLE users ADD COLUMN admin_override_reason VARCHAR;
ALTER TABLE users ADD COLUMN desired_plan_code_set_at TIMESTAMP;
ALTER TABLE users ADD COLUMN subscription_plan_code_set_at TIMESTAMP;
ALTER TABLE users ADD COLUMN subscription_last_checked_at TIMESTAMP;
ALTER TABLE users ADD COLUMN subscription_sync_code VARCHAR;
ALTER TABLE users ADD COLUMN subscription_sync_msg VARCHAR;
ALTER TABLE users ADD COLUMN client_fingerprint VARCHAR(255);
ALTER TABLE users ADD COLUMN is_past_due BOOLEAN DEFAULT FALSE;
CREATE INDEX subscription_sync_code_user_index ON users USING btree(subscription_sync_code);
ALTER TABLE users ADD COLUMN subscription_trial_ends_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE users ADD COLUMN subscription_plan_reason varchar(20);

--UPDATE users set subscription_trial_ends_at = (CURRENT_TIMESTAMP + '30 days'::interval), subscription_plan_code = 'jamsubgold';
UPDATE users set subscription_trial_ends_at = '2021-01-01', subscription_plan_code = 'jamsubplatinum';
UPDATE users set stored_credit_card = true where recurly_code is not null;


-- CREATE TABLE subscriptions (
--     id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
--     name VARCHAR(200) UNIQUE NOT NULL UNIQUE NOT NULL,
--     play_time_per_session_mins INT DEFAULT NULL,
--     play_time_per_month_mins INT DEFAULT NULL,
--     can_record BOOLEAN DEFAULT TRUE,
--     audio_max_bitrate INT DEFAULT NULL,
--     save_as_wave BOOLEAN DEFAULT FALSE,
--     pro_audio BOOLEAN DEFAULT FALSE,
--     video_resolution VARCHAR(50) DEFAULT NULL,
--     broadcasting_type VARCHAR(50) DEFAULT NULL,
--     music_lessons VARCHAR(50) DEFAULT NULL,
--     support VARCHAR(50) DEFAULT NULL,
--     max_players_per_session INT DEFAULT NULL,
--     created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
--     updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-- );


--CREATE INDEX msuh_user_id ON music_sessions_user_history((1)) WHERE is_a_student;

-- alreday on WWW
CREATE INDEX msuh_user_id ON music_sessions_user_history USING btree (user_id);
CREATE INDEX msuh_created_at ON music_sessions_user_history USING btree (created_at);

CREATE INDEX bands_musicians_user_id_idx ON bands_musicians USING btree (user_id);

ALTER TABLE generic_state ADD COLUMN connection_policy VARCHAR;

-- user syncs
CREATE INDEX recorded_tracks_user_id_idx ON recorded_tracks USING btree (user_id);
CREATE INDEX recorded_tracks_recording_id_idx ON recorded_tracks USING btree (recording_id);
CREATE INDEX recorded_backing_tracks_user_id_idx ON recorded_backing_tracks USING btree (user_id);
CREATE INDEX recorded_backing_tracks_recording_id_idx ON recorded_backing_tracks USING btree (recording_id);
CREATE INDEX mixes_recording_id_idx ON mixes USING btree (recording_id);
CREATE INDEX msuh_id_idx ON music_sessions_user_history USING btree (id);
CREATE INDEX quick_mixes_user_id_idx ON quick_mixes USING btree (user_id);
CREATE INDEX recorded_videos_user_id_idx ON recorded_videos USING btree (user_id);
CREATE INDEX music_sessions_user_id_idx ON music_sessions USING btree (user_id);

ALTER TABLE users ADD COLUMN used_current_month INT;
ALTER TABLE users ADD COLUMN used_month_play_time INT;

ALTER TABLE bands ADD COLUMN school_id INT;