From 177891e986a7a38ebe73c7d0262220c94d65158e Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Thu, 20 Oct 2022 15:16:42 +0800 Subject: [PATCH] feat(rr): add database models (#518) * chore(infraservice): remove unused prop * chore(routes/auth): remove unused var * build(package): add useful command to undo migration * feat(migrations): add migrations for review requests fix(db migratino): update property names * chore(db migration): add migration to add status col to rr * chore(db migrations): add required columns for seq creation * feat(db/models): add new db models for rr fix(db models): update db models fix(reviewmeat): update db model * chore(reviewrequest): update db model for seq * fix(reviewmeta): add annotation on db model * chore(reviewrequest.ts): add col to db model * fix(teardown): add enum dropping for tests teardown --- src/database/models/ReviewMeta.ts | 41 +++++++++++++++++ src/database/models/ReviewRequest.ts | 68 ++++++++++++++++++++++++++++ src/database/models/Reviewers.ts | 24 ++++++++++ src/database/models/User.ts | 6 +++ src/database/models/index.ts | 3 ++ src/tests/teardown.ts | 3 ++ 6 files changed, 145 insertions(+) create mode 100644 src/database/models/ReviewMeta.ts create mode 100644 src/database/models/ReviewRequest.ts create mode 100644 src/database/models/Reviewers.ts diff --git a/src/database/models/ReviewMeta.ts b/src/database/models/ReviewMeta.ts new file mode 100644 index 000000000..58d2eda89 --- /dev/null +++ b/src/database/models/ReviewMeta.ts @@ -0,0 +1,41 @@ +import { + DataType, + Column, + Model, + Table, + ForeignKey, +} from "sequelize-typescript" + +import { ReviewRequest } from "./ReviewRequest" +import { User } from "./User" + +@Table({ tableName: "review_meta" }) +// eslint-disable-next-line import/prefer-default-export +export class ReviewMeta extends Model { + @Column({ + autoIncrement: true, + primaryKey: true, + allowNull: false, + type: DataType.BIGINT, + }) + id!: number + + @ForeignKey(() => User) + reviewerId!: number + + @ForeignKey(() => ReviewRequest) + @Column + reviewId!: number + + @Column({ + allowNull: false, + type: DataType.BIGINT, + }) + pullRequestNumber!: number + + @Column({ + allowNull: false, + type: DataType.STRING, + }) + reviewLink!: string +} diff --git a/src/database/models/ReviewRequest.ts b/src/database/models/ReviewRequest.ts new file mode 100644 index 000000000..fac7c01d1 --- /dev/null +++ b/src/database/models/ReviewRequest.ts @@ -0,0 +1,68 @@ +import { + ForeignKey, + DataType, + Column, + Model, + Table, + BelongsTo, + BelongsToMany, + HasOne, +} from "sequelize-typescript" + +import { Site } from "@database/models/Site" +import { User } from "@database/models/User" + +import { Reviewer } from "./Reviewers" +import { ReviewMeta } from "./ReviewMeta" + +@Table({ tableName: "review_requests" }) +// eslint-disable-next-line import/prefer-default-export +export class ReviewRequest extends Model { + @Column({ + autoIncrement: true, + primaryKey: true, + allowNull: false, + type: DataType.BIGINT, + }) + id!: number + + @ForeignKey(() => User) + requestorId!: number + + // NOTE: Because this is a FK to User, + // when User is updated/deleted, + // the corresponding row in ReviewRequest will also be updated. + @BelongsTo(() => User, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + requestor!: User + + @ForeignKey(() => Site) + siteId!: number + + // See above comment wrt CASCADE + @BelongsTo(() => Site, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + site!: Site + + @HasOne(() => ReviewMeta) + reviewMeta!: ReviewMeta + + @Column({ + allowNull: false, + defaultValue: "OPEN", + type: DataType.ENUM("OPEN", "APPROVED", "MERGED", "CLOSED"), + }) + reviewStatus!: "OPEN" | "APPROVED" | "MERGED" | "CLOSED" + + @BelongsToMany(() => User, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + through: () => Reviewer, + as: "reviewers", + }) + reviewers!: User[] +} diff --git a/src/database/models/Reviewers.ts b/src/database/models/Reviewers.ts new file mode 100644 index 000000000..357fb6fe6 --- /dev/null +++ b/src/database/models/Reviewers.ts @@ -0,0 +1,24 @@ +import { + Column, + CreatedAt, + DataType, + ForeignKey, + Model, + Table, + UpdatedAt, +} from "sequelize-typescript" + +import { User } from "@database/models/User" + +import { ReviewRequest } from "./ReviewRequest" + +@Table({ tableName: "reviewers" }) +export class Reviewer extends Model { + @ForeignKey(() => User) + @Column + reviewerId!: number + + @ForeignKey(() => ReviewRequest) + @Column + requestId!: string +} diff --git a/src/database/models/User.ts b/src/database/models/User.ts index 185ccc0ed..9eca1b83c 100644 --- a/src/database/models/User.ts +++ b/src/database/models/User.ts @@ -13,7 +13,10 @@ import { import { Site } from "@database/models/Site" import { SiteMember } from "@database/models/SiteMember" +import { ReviewRequest } from "./ReviewRequest" + @Table({ tableName: "users", paranoid: true }) +// eslint-disable-next-line import/prefer-default-export export class User extends Model { @Column({ autoIncrement: true, @@ -73,4 +76,7 @@ export class User extends Model { as: "sites_created", }) sitesCreated?: Site[] + + @HasMany(() => ReviewRequest) + reviewRequests?: ReviewRequest[] } diff --git a/src/database/models/index.ts b/src/database/models/index.ts index db6d5e783..a8bacbc35 100644 --- a/src/database/models/index.ts +++ b/src/database/models/index.ts @@ -6,3 +6,6 @@ export * from "@database/models/AccessToken" export * from "@database/models/Repo" export * from "@database/models/Deployment" export * from "@database/models/IsomerAdmin" +export * from "@database/models/ReviewMeta" +export * from "@database/models/ReviewRequest" +export * from "@database/models/Reviewers" diff --git a/src/tests/teardown.ts b/src/tests/teardown.ts index 98b45c73c..413d58305 100644 --- a/src/tests/teardown.ts +++ b/src/tests/teardown.ts @@ -31,6 +31,9 @@ const teardownDb = async () => { await sequelize.query( 'DROP TYPE IF EXISTS "public"."enum_sites_job_status" CASCADE;' ) + await sequelize.query( + 'DROP TYPE IF EXISTS "public"."enum_review_requests_review_status" CASCADE;' + ) await sequelize.close() console.log("done, exiting...") }