Skip to content

Commit

Permalink
Merge pull request #632 from hpi-studyu/chore/supabase-cli-seed
Browse files Browse the repository at this point in the history
Chore/supabase cli seed
  • Loading branch information
johannesvedder authored May 25, 2024
2 parents 87f9e92 + 0c9013c commit 019541f
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 19 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/e2e_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ name: E2E tests
on:
push:
paths:
- ".github/workflows/e2e_tests.yml"
- 'designer_v2/**'
- 'core/**'
- 'flutter_common/**'
workflow_dispatch:

concurrency:
group: ${{ github.ref }}-e2e-tests
cancel-in-progress: true

jobs:
drive_web:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -41,17 +38,15 @@ jobs:
dart pub global activate melos
melos bootstrap
- name: Prepare Supabase CI Seed
run: |
cp supabase/seed-ci.sql supabase/seed.sql
- name: Set up Supabase CLI
uses: supabase/setup-cli@v1
with:
version: latest

- name: Prepare database migrations
run: |
mkdir -p supabase/migrations
cp database/studyu-schema.sql supabase/migrations/0000000000001_studyu-schema.sql
cp database/app_config.sql.example supabase/migrations/0000000000002_app_config_mockup.sql
- name: Start Supabase
run: |
supabase start -x studio,imgproxy
Expand All @@ -61,8 +56,6 @@ jobs:
cp flutter_common/lib/envs/.env.local.example flutter_common/lib/envs/.env.local
LC_ALL=C sed -i.bak '/STUDYU_SUPABASE_PUBLIC_ANON_KEY/d' flutter_common/lib/envs/.env.local
echo "STUDYU_SUPABASE_PUBLIC_ANON_KEY=$(supabase status -o env | grep ANON_KEY | cut -d'=' -f2)" >> flutter_common/lib/envs/.env.local
LC_ALL=C sed -i.bak '/STUDYU_SUPABASE_URL/d' flutter_common/lib/envs/.env.local
echo "STUDYU_SUPABASE_URL=http://localhost:54321" >> flutter_common/lib/envs/.env.local
- name: Start Chrome Driver
run: |
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/supabase-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Test Supabase
on:
push:
paths:
- ".github/workflows/supabase-test.yml"
- "database/**"
workflow_dispatch:

Expand All @@ -25,18 +26,16 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Prepare database migrations
- name: Prepare Supabase CI Seed
run: |
mkdir -p supabase/migrations
cp database/studyu-schema.sql supabase/migrations/0000000000001_studyu-schema.sql
cp database/app_config.sql.example supabase/migrations/0000000000002_app_config_mockup.sql
cp supabase/seed-ci.sql supabase/seed.sql
- name: Set up Supabase CLI
uses: supabase/setup-cli@v1
with:
version: latest

- name: Initialize Supabase
- name: Start Supabase database
run: |
supabase db start
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion flutter_common/lib/envs/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
STUDYU_SUPABASE_URL=http://localhost:8082
STUDYU_SUPABASE_PUBLIC_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJhbm9uIiwKICAgICJpc3MiOiAic3VwYWJhc2UtZGVtbyIsCiAgICAiaWF0IjogMTY0MTc2OTIwMCwKICAgICJleHAiOiAxNzk5NTM1NjAwCn0.dc_X5iR_VP_qT0zsiyj_I_OZ2T9FtRU2BBNWN8Bu4GE
STUDYU_SUPABASE_PUBLIC_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
STUDYU_PROJECT_GENERATOR_URL=
STUDYU_APP_URL=http://localhost:8080
STUDYU_DESIGNER_URL=http://localhost:8081
2 changes: 1 addition & 1 deletion supabase/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project_id = "studyu-test"
[api]
enabled = true
# Port to use for the API URL.
port = 54321
port = 8082
# Schemas to expose in your API. Tables, views and stored procedures in this schema will get API
# endpoints. `public` is always included.
schemas = ["public", "graphql_public"]
Expand Down
1 change: 1 addition & 0 deletions supabase/migrations/0000000000001_studyu-schema.sql
67 changes: 67 additions & 0 deletions supabase/migrations/0000000000002_mockup-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
BEGIN;

-- We want to store all of this in the mockup schema to keep it
-- separate from any application data
CREATE SCHEMA IF NOT EXISTS mockup;

-- anon and authenticated should have access to mockup schema
GRANT USAGE ON SCHEMA mockup TO anon, authenticated;
-- Don't allow public to execute any functions in the mockup schema
ALTER DEFAULT PRIVILEGES IN SCHEMA mockup REVOKE EXECUTE ON FUNCTIONS FROM public;
-- Grant execute to anon and authenticated for mockup
ALTER DEFAULT PRIVILEGES IN SCHEMA mockup GRANT EXECUTE ON FUNCTIONS TO anon, authenticated;

/*
* This function is used to create a user in the `auth.users` table.
*
* @param email - The email address of the user
* @param password - The password of the user
*
* @returns user_id - The UUID of the user in the `auth.users` table
*/
CREATE OR REPLACE FUNCTION mockup.create_user(
email text,
password text
) RETURNS uuid AS $$
declare
user_id uuid;
encrypted_pw text;
BEGIN
user_id := gen_random_uuid();
encrypted_pw := extensions.crypt(password, extensions.gen_salt('bf', 12));

INSERT INTO auth.users
(instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, recovery_sent_at, last_sign_in_at, raw_app_meta_data, raw_user_meta_data, created_at, updated_at, confirmation_token, email_change, email_change_token_new, recovery_token)
VALUES
('00000000-0000-0000-0000-000000000000', user_id, 'authenticated', 'authenticated', email, encrypted_pw, now() at time zone 'utc', now() at time zone 'utc', now() at time zone 'utc', '{"provider":"email","providers":["email"]}', '{}', now() at time zone 'utc', now() at time zone 'utc', '', '', '', '');

INSERT INTO auth.identities (id, user_id, provider_id, identity_data, provider, last_sign_in_at, created_at, updated_at)
VALUES
(gen_random_uuid(), user_id, user_id, format('{"sub":"%s","email":"%s"}', user_id::text, email)::jsonb, 'email', now() at time zone 'utc', now() at time zone 'utc', now() at time zone 'utc');

RETURN user_id;
END;
$$ LANGUAGE plpgsql;

/*
* This function is used to get a user by their email address.
*
* @param email_needle - The email of the user to get
*
* @returns user - The user object
*/
CREATE OR REPLACE FUNCTION mockup.get_user(
email_needle text
) RETURNS json AS $$
DECLARE
supabase_user json;
BEGIN
SELECT json_build_object('id', id, 'email', email, 'phone', phone, 'raw_user_meta_data', raw_user_meta_data) into supabase_user FROM auth.users WHERE email = email_needle LIMIT 1;
if supabase_user is null OR supabase_user -> 'id' IS NULL then
RAISE EXCEPTION 'User with email % not found', identifier;
end if;
RETURN supabase_user;
END;
$$ LANGUAGE plpgsql;

COMMIT;
6 changes: 6 additions & 0 deletions database/app_config.sql.example → supabase/seed-ci.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- Seed for CI environment to create a clean test environment

BEGIN;

INSERT INTO public.app_config (id, app_min_version, app_privacy, app_terms, designer_privacy, designer_terms, imprint, contact, analytics)
VALUES (
'prod',
Expand All @@ -10,3 +14,5 @@ VALUES (
'{ "email": "email@example.com", "phone": "1235678", "website": "example.com", "organization": "example" }',
'{ "dsn": "example", "enabled": false, "samplingRate": 0 }'
);

COMMIT;
70 changes: 70 additions & 0 deletions supabase/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
BEGIN;

INSERT INTO public.app_config (id, app_min_version, app_privacy, app_terms, designer_privacy, designer_terms, imprint, contact, analytics)
VALUES (
'prod',
'2.6.0',
'{ "de": "example.com", "en": "example.com" }',
'{ "de": "example.com", "en": "example.com" }',
'{ "de": "example.com", "en": "example.com" }',
'{ "de": "example.com", "en": "example.com" }',
'{ "de": "example.com", "en": "example.com" }',
'{ "email": "email@example.com", "phone": "1235678", "website": "example.com", "organization": "example" }',
'{ "dsn": "example", "enabled": false, "samplingRate": 0 }'
);

-- Seed data

DO $$
DECLARE
email text := 'user1@studyu.health';
password text := 'user1pass';
user_id UUID;
BEGIN
user_id := mockup.create_user(email, password);

INSERT INTO public.study (
contact,
title,
description,
icon_name,
published,
registry_published,
questionnaire,
eligibility_criteria,
observations,
interventions,
consent,
schedule,
report_specification,
results,
created_at,
updated_at,
user_id,
participation,
result_sharing,
collaborator_emails
) VALUES(
'{"email":"example@example.com","phone":"0123456789","website":"https://studyu.health","researchers":"StudyU Researcher","organization":"StudyU","institutionalReviewBoard":"This study has not been submitted to the IRB Board. It is for illustration purposes of StudyU only.","institutionalReviewBoardNumber":"N/A"}',
'Public demo study of user1',
'This is a Demo Study. This study helps you find out which treatment is more effective for you.',
'accountHeart',
-- published
true,
-- registry_published
true,
'[{"id": "recent_back_pain", "type": "boolean", "prompt": "Have you had back pain in the last 12 weeks?", "rationale": ""}, {"id": "f70386c8-f517-4e62-a5fa-ca4badfd4b60", "type": "boolean", "prompt": "Are you pregnant?", "rationale": ""}, {"id": "afeac253-4bfe-47fe-9384-4236ded1bd50", "type": "choice", "prompt": "Does any of the following apply to you and has not been examined by a doctor yet?", "choices": [{"id": "start_of_symptoms_after_spinal_surgery", "text": "Start of symptoms after spinal surgery"}, {"id": "start_of_symptoms_after_diagnosis_of_cancer", "text": "Start of symptoms after diagnosis of cancer"}, {"id": "unexpected_significant_weight_loss", "text": "Unexpected significant weight loss"}, {"id": "start_of_symptoms_after_trauma", "text": "Start of symptoms after trauma"}, {"id": "accompanying_numbness_of_your_legs", "text": "Accompanying numbness of your legs"}], "multiple": true, "rationale": "This question is asked to ensure that you are not suffering from any critical illness."}]','[{"id": "b47d07d8-eb98-4fce-86ab-945bc7c2f2d0", "condition": {"type": "choice", "target": "recent_back_pain", "choices": [true]}}, {"id": "acb368b0-0ca2-496d-98e1-be9fefbe5e89", "condition": {"type": "choice", "target": "f70386c8-f517-4e62-a5fa-ca4badfd4b60", "choices": [false]}}, {"id": "d7c3445e-b5b1-43d1-93a6-5637e0cfd44f", "condition": {"type": "choice", "target": "afeac253-4bfe-47fe-9384-4236ded1bd50", "choices": []}}]','[{"id": "rate_your_day", "type": "questionnaire", "title": "Rate your day", "footer": "", "header": "", "schedule": {"reminders": ["19:00"], "completionPeriods": [{"id": "50d114f3-e692-4283-9610-17e23edf8f70", "lockTime": "20:00", "unlockTime": "8:00"}]}, "questions": [{"id": "pain", "step": 1, "type": "visualAnalogue", "prompt": "Rate your pain.", "initial": 0, "maximum": 10, "minimum": 0, "maximumColor": 4294901760, "minimumColor": 4294967295, "maximumAnnotation": "Well, guess I die now", "minimumAnnotation": "no pain"}, {"id": "painkillers", "type": "boolean", "prompt": "Have you taken any painkillers in the last 24 hours?"}, {"id": "sleep", "step": 1, "type": "visualAnalogue", "prompt": "Rate your sleep.", "initial": 0, "maximum": 10, "minimum": 0, "maximumColor": 4294901760, "minimumColor": 4294967295, "maximumAnnotation": "terrible", "minimumAnnotation": "no problems"}, {"id": "mood", "step": 1, "type": "annotatedScale", "prompt": "Rate your mood.", "initial": 5, "maximum": 10, "minimum": 0, "annotations": [{"value": 0, "annotation": "☠"}, {"value": 5, "annotation": "😐"}, {"value": 10, "annotation": "😀"}]}]}]','[{"id": "willow_bark_tea", "icon": "coffee", "name": "Willow-Bark tea", "tasks": [{"id": "drink_tea", "type": "checkmark", "title": "Drink a cup of Willow-Bark tea twice a day.", "schedule": {"reminders": ["18:00"], "completionPeriods": [{"id": "54d114f3-e692-4283-9610-17e23edf8f70", "lockTime": "20:00", "unlockTime": "6:00"}]}}], "description": "Willow bark contains powerful anti-inflamatory compunds such as flavonoids and salicin that help relieve the pain."}, {"id": "arnika", "icon": "leaf", "name": "Arnika", "tasks": [{"id": "apply_arnika", "type": "checkmark", "title": "Apply a dime sized amount of Arnica gel to your lower back and massage for 10 mins.", "schedule": {"reminders": ["18:00"], "completionPeriods": [{"id": "55d114f3-e692-4283-9610-17e23edf8f70", "lockTime": "20:00", "unlockTime": "6:00"}]}}], "description": "Arnika gel has been proven to soothe muscle soreness, strain and reduce swelling when rubbed on the affected area."}, {"id": "warming_pad", "icon": "car-seat-heater", "name": "Warming Pad", "tasks": [{"id": "use_pad", "type": "checkmark", "title": "Apply warming pad to your lower back for 5 minutes.", "schedule": {"reminders": ["18:00"], "completionPeriods": [{"id": "56d114f3-e692-4283-9610-17e23edf8f70", "lockTime": "20:00", "unlockTime": "6:00"}]}}], "description": "Applying a warming pad is a quick and easy way to soothe sore muscles and joints."}]','[{"id": "Need", "title": "Why Consent Is Needed", "iconName": "featureSearch", "description": "We need your explicit consent because you are going to enroll to a research study. Therefore, we have to provide you all the information that could potentially have an impact on your decision whether or not you take part in the study. This is study-specific so please go through it carefully. Participation is entirely voluntary."}, {"id": "Risk_benefit", "title": "Risks & Benefits", "iconName": "signCaution", "description": "The main risks to you if you choose to participate are allergic reactions to one of the interventions you are going to apply. If you feel uncomfortable, suffer from itching or rash please pause the study until you have seen a doctor. It is important to know that you may not get any benefit from taking part in this research. Others may not benefit either. However, study results may help you to understand if one of the offered interventions has a positive effect on one of the investigated observations for you."}, {"id": "Data", "title": "Data Handling & Use", "iconName": "databaseExport", "description": "By giving your consent you accept that researchers are allowed to process anonymized data collected from you during this study for research purposes. If you stop being in the research study, already transmitted information may not be removed from the research study database because re-identification is not possible. It will continue to be used to complete the research analysis."}, {"id": "Issues", "title": "Issues to Consider", "iconName": "mapClock", "description": "For being able to use your results for research we need you to actively participate for the indicated minimum study duration. After reaching this you will be able to unlock results but we encourage you to take part at least until you reach the recommended level on the progress bar. Otherwise it might be the case that results can indeed be used for research but are not meaningful for you personally. Note that if you decide to take part in this research study you will be responsible for buying the needed aids."}, {"id": "Rights", "title": "Participant Rights", "iconName": "gavel", "description": "You may stop taking part in this research study at any time without any penalty. If you have any questions, concerns, or complaints at any time about this research, or you think the research has harmed you, please contact the office of the research team. You can find the contact details in your personal study dashboard."}, {"id": "Future", "title": "Future Research", "iconName": "binoculars", "description": "The purpose of this research study is to help you find the most effective supporting agent or behavior. The aim is not to treat your symptom causally. In a broader perspective the purpose of this study is also to get insights which group of persons benefits most from which intervention."}]','{"sequence": "alternating", "phaseDuration": 7, "numberOfCycles": 2, "sequenceCustom": "ABAB", "includeBaseline": true}','{"primary": {"id": "average", "type": "average", "title": "Average", "aggregate": "day", "description": "Average", "resultProperty": {"task": "rate_your_day", "property": "pain"}}, "secondary": []}',
'[]',
'2021-04-13 18:19:49.000',
'2023-05-11 12:46:06.018',
-- user_id
user_id,
-- participation ('open', 'invite')
'open',
-- result_sharing ('public', 'organization', 'private')
'public',
'{}'
);
END $$;

COMMIT;
File renamed without changes.

0 comments on commit 019541f

Please sign in to comment.