Thie repository contains our PostgreSQL queries and Supabase Edge Functions
supabase/migrations: database migrations and SQL filessupabase/functions: Supabase edge functions
Supabase local development uses Docker to start and stop Supabase services. Make sure to have Docker running on your machine before continuing with the configuration.
Once you have Docker running, you can following the steps below to configure the local development environment. (source)
- Install Supabase CLI
- Initialize the project
supabase init - Start the Docker container
supabase start - Stop the Docker containers when not utilizing it
supabase stop
Although it is ideal to manage all database changes on one platform, during the process of figuring out Supabase, there were some aspects of our database that were created using the Supabase web console. Therefore, queries used to create tables, apply constraints, or apply RLS policies may not be found in this repository.
The SQL format of table definitions, RLS policies, and functions that are used, but not in this repository can be found in the web console. The only thing not available on the web console is the one procedure used in this project. The definition can be found in this file.
Supabase local developement was primarily used to write Postgres functions and procedures, not much table definitions and policies. What is not written in this repository can be found in the web console
supabase migration docs
Each .sql file is a migration file. This section includes information regarding the typical migration flow and on updating existing migration files.
-
Create a migration file
Each migration file was generated using the command below
supabase migration new <name> -
Make changes to the file and save
-
Reset the local database
supabase db reset -
Push the changes
Once changes are pushed, you will be able to see them on the web
supabase db push
If a small change to a .sql file was made (e.g. adding 1 condition to a SELECT query), you can utilize an existing migration file to push your changes instead of having to create a new migration file.
Each migration file has a timestamp associated, as can be seen from the .sql file names. For example, 20240409001420_tr_on_professor_response.sql. In this doc, the <timestamp> will be referred to as the timestamp of the migration file names.
-
Revert the migration
supabase migration repair <timestamp> --status reverted -
Make changes
-
Reset the local database
supabase db resetAlthough this step is not necessary, it is good practice to do so, as it makes sure the local and remote databases are in sync.
-
Push the changes
supabase db push --include-all--include-allmakes sure all the migrations are pushed to the remote database
- Supabase Edge Functions Official Docs
- Sending Emails with Resend
-
Configure Local Development CLI and make sure Docker is running
-
Create a function
supabase functions new <name>This will create a directory under
supabase/functions -
Open up
index.tsand write the function -
Deploy the function
supabase functions deploy <name>If you want to deploy all the functions, run
supabase functions deploy
TL;DR: Users get email notifications in the following cases:
- Professor email digest: The number of appeals they received over the past day/week/month depending on preferences
- Student/Grader interaction history notification: Students and Graders receive email notifications when a Professor replies to their appeal in the interaction history
- Student appeal notification: Students receive an email when the Professor closes their appeal
The Supabase edge functions sending the email notifications are defined under the supabase/functions directory. The edge functions are invoked by the Postgres extension pg_net, and the professor email digest emails are scheduled and configured using the pg_cron extension.
pg_netenables async http/https requests in in postgreSQL.pg_cronenables users to schedule jobs based on a cron schedule inside the database.
For the email-notification service, we use a combination of pg_net, pg_cron, Postgres Triggers, and Supabase edge functions to send notification emails based on a cron schedule. Professor users are able to update their cron schedule through the settings page of their GradeBoost accounts, which will update the email schedule to the their preferences.
It is also important to note that the procedure
proc_invoke_digest_emailcan only be viewed and edited through the code in this repo. It is not available to view/edit on the Supabase web console.
- How to invoke a supabase edge function with pg_net
- How to use pg_net with a trigger
- How to use pg_cron to run procedures
- Postgres docs on procedures
- Difference between procedures and functions in Postgres
- Event:
UPDATEon thePUBLIC."Professors".is_verifiedcolumn - Trigger:
UPDATEtrigger that callscron.schedule()to initiate cron schedule
cron.schedule() will add the email schedule to the table cron.jobs table in the cron schema. The default setting for email digests is 8:00 AM on every Monday.
- Event:
UPDATEon thePUBLIC.Professors.cron_jobcolumn - Trigger:
UPDATETrigger that callscron.alter_job()to change the cron job
Whenever a Professor sends a message through the interaction history to the Grader or the Student, they will get an email notification that a Professor has sent them a message.
- Event:
INSERTonPUBLIC.Messageswhererecipient_idis the Student's ID and thesender_idis the Professor's ID - Trigger:
INSERTTrigger that callson_professor_response, that invokessend-professor-response-notification
Students will be notified via email if their appeals are closed by the Professor.
- Event:
UPDATEon thePUBLIC.Appeals.is_opencolumn - Trigger:
UPDATETrigger that callson_close_appeal, that invokessend_appeal_closed_email




