Skip to content

Commit

Permalink
chore: rename updates table (#1824)
Browse files Browse the repository at this point in the history
* chore: rename updates table

* chore: add project id column to updates table
  • Loading branch information
ncomerci committed May 31, 2024
1 parent 183dd60 commit b88603e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/entities/Updates/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getMonthsBetweenDates } from '../../utils/date/getMonthsBetweenDates'
import { UpdateAttributes, UpdateStatus } from './types'

export default class UpdateModel extends Model<UpdateAttributes> {
static tableName = 'proposal_updates'
static tableName = 'project_updates'
static withTimestamps = false
static primaryKey = 'id'

Expand Down
9 changes: 9 additions & 0 deletions src/migrations/1717080580412_alter-updates-table-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { MigrationBuilder } from "node-pg-migrate"

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.renameTable('proposal_updates', 'project_updates')
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.renameTable('project_updates', 'proposal_updates')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { MigrationBuilder } from "node-pg-migrate"
import UpdateModel from "../entities/Updates/model"
import ProjectModel from "../back/models/Project"

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.addColumn(UpdateModel.tableName, {
project_id: {
type: 'TEXT',
notNull: false,
},
})
pgm.addConstraint(UpdateModel.tableName, 'fk_project_id', {
foreignKeys: {
columns: ['project_id'],
references: `${ProjectModel.tableName}(id)`,
},
})
const query = `
UPDATE ${UpdateModel.tableName}
SET project_id = p.id
FROM ${ProjectModel.tableName} p
WHERE ${UpdateModel.tableName}.proposal_id = p.proposal_id
`
pgm.sql(query)
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropConstraint(UpdateModel.tableName, 'fk_project_id', {ifExists: true})
pgm.dropColumn(UpdateModel.tableName, 'project_id')
}

0 comments on commit b88603e

Please sign in to comment.