From c84c64c5cf32b7cbfd027cbc8ac36ba06ecf511c Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:22:43 +0800 Subject: [PATCH] feat(rr): db migrations (#515) * 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 --- package.json | 1 + .../20221003052424-review-request-creation.js | 43 +++++++++++++++++ .../20221003123422-review-meta-creation.js | 46 +++++++++++++++++++ .../20221003130006-reviewer-creation.js | 43 +++++++++++++++++ .../20221007124138-create-review-status.js | 30 ++++++++++++ src/routes/v2/auth.js | 2 - src/services/infra/InfraService.ts | 8 +--- 7 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 src/database/migrations/20221003052424-review-request-creation.js create mode 100644 src/database/migrations/20221003123422-review-meta-creation.js create mode 100644 src/database/migrations/20221003130006-reviewer-creation.js create mode 100644 src/database/migrations/20221007124138-create-review-status.js diff --git a/package.json b/package.json index 5ff330b58..d0693fe23 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "prepare": "husky install", "version": "auto-changelog -p && git add CHANGELOG.md", "db:migrate": "source .env && npx sequelize-cli db:migrate", + "db:migrate:undo": "source .env && npx sequelize-cli db:migrate:undo", "jump:staging": "source .ssh/.env.staging && ssh -L 5433:$DB_HOST:5432 $SSH_USER@$SSH_HOST -i .ssh/isomercms-staging-bastion.pem", "db:migrate:staging": "source .ssh/.env.staging && npx sequelize-cli db:migrate", "jump:prod": "source .ssh/.env.prod && ssh -L 5433:$DB_HOST:5432 $SSH_USER@$SSH_HOST -i .ssh/isomercms-production-bastion.pem", diff --git a/src/database/migrations/20221003052424-review-request-creation.js b/src/database/migrations/20221003052424-review-request-creation.js new file mode 100644 index 000000000..541b7deea --- /dev/null +++ b/src/database/migrations/20221003052424-review-request-creation.js @@ -0,0 +1,43 @@ +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("review_requests", { + id: { + allowNull: false, + primaryKey: true, + type: Sequelize.BIGINT, + autoIncrement: true, + }, + requestor_id: { + type: Sequelize.BIGINT, + allowNull: false, + references: { + model: "users", + key: "id", + }, + }, + site_id: { + type: Sequelize.BIGINT, + allowNull: false, + references: { + model: "sites", + key: "id", + }, + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + }) + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("review_requests") + }, +} diff --git a/src/database/migrations/20221003123422-review-meta-creation.js b/src/database/migrations/20221003123422-review-meta-creation.js new file mode 100644 index 000000000..3812952de --- /dev/null +++ b/src/database/migrations/20221003123422-review-meta-creation.js @@ -0,0 +1,46 @@ +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("review_meta", { + id: { + allowNull: false, + primaryKey: true, + autoIncrement: true, + type: Sequelize.BIGINT, + }, + review_id: { + type: Sequelize.BIGINT, + allowNull: false, + references: { + model: "review_requests", + key: "id", + }, + }, + // The PR number stored by GitHub + pull_request_number: { + type: Sequelize.BIGINT, + allowNull: false, + }, + // The link to view this RR + review_link: { + unique: true, + type: Sequelize.STRING, + allowNull: false, + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + }) + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("review_meta") + }, +} diff --git a/src/database/migrations/20221003130006-reviewer-creation.js b/src/database/migrations/20221003130006-reviewer-creation.js new file mode 100644 index 000000000..a8e33f2ac --- /dev/null +++ b/src/database/migrations/20221003130006-reviewer-creation.js @@ -0,0 +1,43 @@ +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable("reviewers", { + request_id: { + allowNull: false, + primaryKey: true, + type: Sequelize.BIGINT, + references: { + model: "review_requests", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + reviewer_id: { + type: Sequelize.BIGINT, + allowNull: false, + primaryKey: true, + references: { + model: "users", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + }) + }, + + down: async (queryInterface) => { + await queryInterface.dropTable("reviewers") + }, +} diff --git a/src/database/migrations/20221007124138-create-review-status.js b/src/database/migrations/20221007124138-create-review-status.js new file mode 100644 index 000000000..d887f57a1 --- /dev/null +++ b/src/database/migrations/20221007124138-create-review-status.js @@ -0,0 +1,30 @@ +module.exports = { + up: async (queryInterface, Sequelize) => + queryInterface.sequelize.transaction(async (t) => { + await queryInterface.addColumn( + "review_requests", // name of Source model + "review_status", // name of column we're adding + { + type: Sequelize.ENUM, + values: ["OPEN", "MERGED", "CLOSED", "APPROVED"], + allowNull: false, + defaultValue: "OPEN", + transaction: t, + } + ) + }), + + down: async (queryInterface, _) => + queryInterface.sequelize.transaction(async (t) => { + await queryInterface.removeColumn( + "review_requests", // name of Source Model + "review_status", // name of column we want to remove + { transaction: t } + ) + // drop created enum + await queryInterface.sequelize.query( + "drop type enum_review_requests_review_status;", + { transaction: t } + ) + }), +} diff --git a/src/routes/v2/auth.js b/src/routes/v2/auth.js index a314d9a33..696de178d 100644 --- a/src/routes/v2/auth.js +++ b/src/routes/v2/auth.js @@ -7,8 +7,6 @@ const { attachReadRouteHandlerWrapper } = require("@middleware/routeHandler") const { FRONTEND_URL } = process.env const { isSecure } = require("@utils/auth-utils") -const { AuthError } = require("@errors/AuthError") - const AUTH_TOKEN_EXPIRY_MS = parseInt( process.env.AUTH_TOKEN_EXPIRY_DURATION_IN_MILLISECONDS, 10 diff --git a/src/services/infra/InfraService.ts b/src/services/infra/InfraService.ts index 8bc9becbf..ba6c362fe 100644 --- a/src/services/infra/InfraService.ts +++ b/src/services/infra/InfraService.ts @@ -5,7 +5,6 @@ import logger from "@root/logger/logger" import DeploymentsService from "@services/identity/DeploymentsService" import ReposService from "@services/identity/ReposService" import SitesService from "@services/identity/SitesService" -import UsersService from "@services/identity/UsersService" interface InfraServiceProps { sitesService: SitesService @@ -30,12 +29,7 @@ export default class InfraService { this.deploymentsService = deploymentsService } - createSite = async ( - submissionId: string, - creator: User, - siteName: string, - repoName: string - ) => { + createSite = async (creator: User, siteName: string, repoName: string) => { let site: Site | undefined // For error handling try { // 1. Create a new site record in the Sites table