BEGIN;

UPDATE version SET minor = 7;

ALTER TABLE votes
	ALTER COLUMN requestUrl SET NOT NULL
	ALTER COLUMN userId SET NOT NULL;

CREATE OR REPLACE PROCEDURE clear_zero_votes()
	LANGUAGE SQL
	AS $$
		DELETE FROM requests WHERE NOT EXISTS
			(SELECT FROM votes WHERE requests.url = votes.requesturl);
	$$;

CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
	LANGUAGE SQL
	AS $$
		DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
		CALL update_scores();
		CALL clear_zero_votes();
	$$;

COMMIT;
