Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: ✨ migrate to postgres (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecxyzzy authored Nov 27, 2023
1 parent 5fc3ebe commit 625063f
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 263 deletions.
1 change: 0 additions & 1 deletion .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
CERTIFICATE_ARN: ${{ secrets.CERTIFICATE_ARN }}
DATABASE_URL: ${{ secrets.DATABASE_URL_PRODUCTION }}
DATABASE_URL_REGISTRAR_SCRAPER: ${{ secrets.DATABASE_URL_REGISTRAR_SCRAPER }}
DATABASE_URL_WEBSOC_SCRAPER: ${{ secrets.DATABASE_URL_WEBSOC_SCRAPER }}
HOSTED_ZONE_ID: ${{ secrets.HOSTED_ZONE_ID }}
NODE_ENV: production
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/v1/rest/week/+endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export const GET = createHandler(async (event, context, res) => {

try {
const parsedQuery = QuerySchema.parse(query);
if (!parsedQuery.year) {
if (!parsedQuery.hasParams) {
const [month, day, year] = new Date()
.toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
.split(",")[0]
.split("/")
.map((x) => parseInt(x, 10));
.map((x) => Number.parseInt(x, 10));

parsedQuery.year = year;
parsedQuery.month = month;
Expand Down
19 changes: 5 additions & 14 deletions apps/api/src/routes/v1/rest/week/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ export const QuerySchema = z
message: "The day provided is not valid for the month provided",
},
)
.or(
z.object({
year: z.undefined(),
month: z.undefined(),
day: z.undefined(),
}),
);

z.setErrorMap((issue, ctx) => ({
message:
issue.code === z.ZodIssueCode.invalid_union
? "All fields must either be provided or left blank"
: ctx.defaultError,
}));
.transform((params) => ({
hasParams: true,
...params,
}))
.or(z.null().transform(() => ({ year: -1, month: -1, day: -1, hasParams: false })));

export type Query = z.infer<typeof QuerySchema>;
26 changes: 5 additions & 21 deletions libs/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ generator client {
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
provider = "postgresql"
url = env("DATABASE_URL")
}

// Do not modify anything above this line unless you know what you are doing.
// The parts above this line are used to configure the Prisma Client directly
// and has nothing to do with the database schema.
// You probably do not need to modify it.

// Enums

Expand Down Expand Up @@ -86,23 +87,6 @@ model Course {
terms Json
}

model CoursePrereq {
courseId String
forCourseId String
@@id([courseId, forCourseId])
@@unique([courseId, forCourseId], name: "idx")
}

model CourseHistory {
ucinetid String
courseId String
term String
@@id([ucinetid, courseId, term])
@@unique([ucinetid, courseId, term], name: "idx")
}

model Instructor {
ucinetid String @id
name String
Expand Down
82 changes: 82 additions & 0 deletions libs/db/sql/access_control.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
-- This file is used to control access to the API database.
-- You probably do not need to modify this.
ALTER DEFAULT PRIVILEGES IN SCHEMA dev
GRANT
SELECT
ON TABLES TO public;

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT
SELECT
ON TABLES TO public;

CREATE ROLE api_staging_user LOGIN;

GRANT USAGE ON SCHEMA dev TO api_staging_user;

GRANT
SELECT
ON ALL TABLES IN SCHEMA dev TO api_staging_user;

GRANT USAGE,
SELECT
ON ALL SEQUENCES IN SCHEMA dev TO api_staging_user;

GRANT INSERT ON dev."CalendarTerm" TO api_staging_user;

CREATE ROLE api_prod_user LOGIN;

GRANT USAGE ON SCHEMA public TO api_prod_user;

GRANT
SELECT
ON ALL TABLES IN SCHEMA public TO api_prod_user;

GRANT USAGE,
SELECT
ON ALL SEQUENCES IN SCHEMA public TO api_prod_user;

GRANT INSERT ON public."CalendarTerm" TO api_prod_user;

CREATE ROLE api_websoc_scraper LOGIN;

GRANT USAGE ON SCHEMA public TO api_websoc_scraper;

GRANT USAGE,
SELECT
ON ALL SEQUENCES IN SCHEMA public TO api_websoc_scraper;

GRANT
SELECT
,
INSERT,
UPDATE,
DELETE ON public."WebsocEnrollmentHistory" TO api_websoc_scraper;

GRANT
SELECT
,
INSERT,
UPDATE,
DELETE ON public."WebsocSection" TO api_websoc_scraper;

GRANT
SELECT
,
INSERT,
UPDATE,
DELETE ON public."WebsocSectionInstructor" TO api_websoc_scraper;

GRANT
SELECT
,
INSERT,
UPDATE,
DELETE ON public."WebsocSectionMeeting" TO api_websoc_scraper;

GRANT
SELECT
,
INSERT,
UPDATE,
DELETE ON public."WebsocSectionMeetingBuilding" TO api_websoc_scraper;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"prettier": "3.1.0",
"prettier-plugin-packagejson": "2.4.6",
"prettier-plugin-prisma": "5.0.0",
"prettier-plugin-sql": "0.16.0",
"turbo": "1.10.16",
"typescript": "5.2.2",
"unconfig": "0.3.11"
Expand Down
Loading

0 comments on commit 625063f

Please sign in to comment.