Skip to content

Commit

Permalink
feat: migration file for study_fitbit_credentials table
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimozkn committed Feb 19, 2025
1 parent 9a80f8e commit 5f5b2b4
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 35 deletions.
3 changes: 0 additions & 3 deletions database/migration/20241218_fitbit_credentials.sql

This file was deleted.

64 changes: 64 additions & 0 deletions database/migration/20250219_fitbit_credentials.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--
-- Name: study_fitbit_credentials; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.study_fitbit_credentials (
study_id uuid NOT NULL PRIMARY KEY,
fitbit_credentials jsonb NOT NULL
);

ALTER TABLE public.study_fitbit_credentials OWNER TO postgres;

COMMENT ON TABLE public.study_fitbit_credentials IS 'Fitbit credentials for studies';

--
-- Name: "Enable read access for study participants for fitbit credentials and owners"; Type: POLICY; Schema: public; Owner: postgres
--

CREATE POLICY "Enable read access for study participants for fitbit credentials and owners"
ON public.study_fitbit_credentials
FOR SELECT
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
OR public.is_study_subject_of(auth.uid(), study_fitbit_credentials.study_id)
);

--
-- Name: "Study owners can manage their own fitbit credentials"; Type: POLICY; Schema: public; Owner: postgres
--

CREATE POLICY "Study owners can manage their own fitbit credentials"
ON public.study_fitbit_credentials
FOR ALL
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
)
WITH CHECK (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
);

--
-- Name: study_fitbit_credentials study_fitbit_credentials_studyId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.study_fitbit_credentials
ADD CONSTRAINT "study_fitbit_credentials_studyId_fkey"
FOREIGN KEY (study_id) REFERENCES public.study(id) ON DELETE CASCADE;

--
-- Name: study_fitbit_credentials; Type: ROW SECURITY; Schema: public; Owner: postgres
--

ALTER TABLE public.study_fitbit_credentials ENABLE ROW LEVEL SECURITY;
80 changes: 48 additions & 32 deletions database/studyu-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -645,38 +645,7 @@ CREATE TABLE public.study_fitbit_credentials (
ALTER TABLE public.study_fitbit_credentials OWNER TO postgres;


-- SELECT policy: Allow access if the user is either the study owner (or collaborator) or a study participant.
CREATE POLICY "Fitbit: study owner or participant can select"
ON public.study_fitbit_credentials
FOR SELECT
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
OR public.is_study_subject_of(auth.uid(), study_fitbit_credentials.study_id)
);

-- Modification policy: Allow INSERT, UPDATE, DELETE only if the user is the study owner (or collaborator).
CREATE POLICY "Fitbit: study owner can modify credentials"
ON public.study_fitbit_credentials
FOR ALL
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
)
WITH CHECK (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
);

COMMENT ON TABLE public.study_fitbit_credentials IS 'Fitbit credentials for studies';

--
-- Name: COLUMN study_invite.preselected_intervention_ids; Type: COMMENT; Schema: public; Owner: postgres
Expand Down Expand Up @@ -849,6 +818,10 @@ ALTER TABLE ONLY public.study_invite
ADD CONSTRAINT "study_invite_studyId_fkey" FOREIGN KEY (study_id) REFERENCES public.study(id) ON DELETE CASCADE;


--
-- Name: study_fitbit_credentials study_fitbit_credentials_studyId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.study_fitbit_credentials
ADD CONSTRAINT "study_fitbit_credentials_studyId_fkey" FOREIGN KEY (study_id) REFERENCES public.study(id) ON DELETE CASCADE;

Expand Down Expand Up @@ -1100,6 +1073,45 @@ CREATE POLICY "Joining a closed study should not be possible" ON public.study_su
AND study.status = 'closed'::public.study_status
));


--
-- Name: "Enable read access for study participants for fitbit credentials and owners"; Type: POLICY; Schema: public; Owner: postgres
--

CREATE POLICY "Enable read access for study participants for fitbit credentials and owners"
ON public.study_fitbit_credentials
FOR SELECT
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
OR public.is_study_subject_of(auth.uid(), study_fitbit_credentials.study_id)
);

--
-- Name: "Study owners can manage their own fitbit credentials"; Type: POLICY; Schema: public; Owner: postgres
--

CREATE POLICY "Study owners can manage their own fitbit credentials"
ON public.study_fitbit_credentials
FOR ALL
USING (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
)
WITH CHECK (
(
SELECT public.can_edit(auth.uid(), study)
FROM public.study
WHERE study.id = study_fitbit_credentials.study_id
)
);

--
-- Name: app_config; Type: ROW SECURITY; Schema: public; Owner: postgres
--
Expand Down Expand Up @@ -1136,6 +1148,10 @@ ALTER TABLE public.study_subject ENABLE ROW LEVEL SECURITY;

ALTER TABLE public.subject_progress ENABLE ROW LEVEL SECURITY;

--
-- Name: study_fitbit_credentials; Type: ROW SECURITY; Schema: public; Owner: postgres
--

ALTER TABLE public.study_fitbit_credentials ENABLE ROW LEVEL SECURITY;

--
Expand Down

0 comments on commit 5f5b2b4

Please sign in to comment.