diff --git a/mpc/coordinator/database.sql b/mpc/coordinator/database.sql index 67e0163f28..d3149b5c10 100644 --- a/mpc/coordinator/database.sql +++ b/mpc/coordinator/database.sql @@ -1,3 +1,8 @@ +BEGIN; + +-- Default bucket for contributions upload +INSERT INTO storage.buckets(id, name, public) VALUES('contributions', 'contributions', false); + ----------- -- Queue -- ----------- @@ -22,26 +27,6 @@ CREATE POLICY view_all true ); --- Materialized ? -CREATE OR REPLACE VIEW current_queue_position AS - SELECT - CASE WHEN (SELECT cci.id FROM current_contributor_id cci) = auth.uid() THEN - 0 - ELSE - ( - SELECT COUNT(*) + 1 - FROM queue q - WHERE - -- Better score - q.score > (SELECT qq.score FROM queue qq WHERE qq.id = auth.uid()) - AND - -- Contribution round not started - NOT EXISTS (SELECT cs.id FROM contribution_status cs WHERE cs.id = q.id) - ) - END AS position; - -ALTER VIEW current_queue_position SET (security_invoker = on); - CREATE OR REPLACE FUNCTION min_score() RETURNS INTEGER AS $$ BEGIN RETURN (SELECT COALESCE(MIN(score) - 1, 1000000) FROM queue); @@ -66,7 +51,7 @@ CREATE TABLE contribution_status( expire timestamptz NOT NULL DEFAULT(now() + INTERVAL '30 minutes') ); -ALTER TABLE contribution ENABLE ROW LEVEL SECURITY; +ALTER TABLE contribution_status ENABLE ROW LEVEL SECURITY; ALTER TABLE contribution_status ADD FOREIGN KEY (id) REFERENCES queue(id); CREATE UNIQUE INDEX idx_contribution_status_id_expire ON contribution_status(id, expire); @@ -83,11 +68,13 @@ CREATE POLICY view_all ---------------------------- CREATE TABLE contribution_submitted( id uuid PRIMARY KEY, + object_id uuid NOT NULL, created_at timestamptz NOT NULL DEFAULT(now()) ); ALTER TABLE contribution_submitted ENABLE ROW LEVEL SECURITY; ALTER TABLE contribution_submitted ADD FOREIGN KEY (id) REFERENCES contribution_status(id); +ALTER TABLE contribution_submitted ADD FOREIGN KEY (object_id) REFERENCES storage.objects(id); CREATE POLICY view_all ON contribution_submitted @@ -150,6 +137,26 @@ CREATE OR REPLACE VIEW current_contributor_id AS ALTER VIEW current_contributor_id SET (security_invoker = on); +-- Materialized ? +CREATE OR REPLACE VIEW current_queue_position AS + SELECT + CASE WHEN (SELECT cci.id FROM current_contributor_id cci) = auth.uid() THEN + 0 + ELSE + ( + SELECT COUNT(*) + 1 + FROM queue q + WHERE + -- Better score + q.score > (SELECT qq.score FROM queue qq WHERE qq.id = auth.uid()) + AND + -- Contribution round not started + NOT EXISTS (SELECT cs.id FROM contribution_status cs WHERE cs.id = q.id) + ) + END AS position; + +ALTER VIEW current_queue_position SET (security_invoker = on); + -- The current payload is from the latest successfull contribution CREATE OR REPLACE VIEW current_payload_id AS SELECT COALESCE( @@ -181,9 +188,6 @@ BEGIN END $$ LANGUAGE plpgsql; --- On expiry, rotate the contributor -SELECT cron.schedule('update-contributor', '10 seconds', 'CALL set_next_contributor()'); - CREATE OR REPLACE FUNCTION can_upload(name varchar) RETURNS BOOLEAN AS $$ BEGIN RETURN ( @@ -234,9 +238,9 @@ CREATE POLICY allow_authenticated_contributor_download can_download(name) ); -CREATE OR REPLACE PROCEDURE set_contribution_submitted(queue_id uuid) AS $$ +CREATE OR REPLACE PROCEDURE set_contribution_submitted(queue_id uuid, object_id uuid) AS $$ BEGIN - INSERT INTO contribution_submitted(id) VALUES(queue_id); + INSERT INTO contribution_submitted(id, object_id) VALUES(queue_id, object_id); END $$ LANGUAGE plpgsql; @@ -265,7 +269,7 @@ BEGIN file_size := (NEW.metadata->>'size')::integer; CASE WHEN file_size = expected_payload_size() - THEN CALL set_contribution_submitted(uuid(NEW.owner_id)); + THEN CALL set_contribution_submitted(uuid(NEW.owner_id), NEW.id); ELSE RAISE EXCEPTION 'invalid file size, name: %, got: %, expected: %, meta: %', NEW.name, file_size, expected_payload_size(), NEW.metadata; END CASE; @@ -276,3 +280,8 @@ $$ LANGUAGE plpgsql; -- Rotate the current contributor whenever a contribution is done. CREATE TRIGGER contribution_payload_uploaded AFTER INSERT OR UPDATE ON storage.objects FOR EACH ROW EXECUTE FUNCTION set_contribution_submitted_trigger(); + +-- On expiry, rotate the contributor +SELECT cron.schedule('update-contributor', '10 seconds', 'CALL set_next_contributor()'); + +COMMIT;