Skip to content

Commit

Permalink
feat(rr): add database models (#518)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
seaerchin authored Oct 20, 2022
1 parent 2196e93 commit 177891e
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/database/models/ReviewMeta.ts
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 68 additions & 0 deletions src/database/models/ReviewRequest.ts
Original file line number Diff line number Diff line change
@@ -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[]
}
24 changes: 24 additions & 0 deletions src/database/models/Reviewers.ts
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions src/database/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -73,4 +76,7 @@ export class User extends Model {
as: "sites_created",
})
sitesCreated?: Site[]

@HasMany(() => ReviewRequest)
reviewRequests?: ReviewRequest[]
}
3 changes: 3 additions & 0 deletions src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions src/tests/teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
}
Expand Down

0 comments on commit 177891e

Please sign in to comment.