Skip to content

Commit

Permalink
Add migration for phase history and set starting date for phase 1 of …
Browse files Browse the repository at this point in the history
…all villages
  • Loading branch information
Benjyhy committed Nov 13, 2024
1 parent b2252e9 commit 8306688
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions server/migrations/1731486908522-AddPhaseHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Table, TableForeignKey, TableIndex } from 'typeorm';
import type { MigrationInterface, QueryRunner } from 'typeorm';

export class AddCreatedAtColumnToStudentAndUserEntities1729236109909 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'phase_history',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
},
{
name: 'phase',
type: 'tinyint',
},
{
name: 'startingOn',
type: 'date',
},
{
name: 'endingOn',
type: 'date',
},
],
}),
);

await queryRunner.createIndex(
'phase_history',
new TableIndex({
name: 'IDX_PHASE_HISTORY',
columnNames: ['village', 'phase'],
isUnique: true,
}),
);

await queryRunner.createForeignKey(
'phase_history',
new TableForeignKey({
columnNames: ['villageId'],
referencedColumnNames: ['id'],
referencedTableName: 'village',
onDelete: 'CASCADE',
}),
);

const villages = await queryRunner.query(`SELECT id FROM village`);
const startingOn2024Phase1Date = '2024-09-30 00:00:00.000000';

for (const village of villages) {
await queryRunner.query(`INSERT INTO phase_history (villageId, phase, startingOn, endingOn) VALUES (?, ?, ?, ?)`, [
village.id,
1,
startingOn2024Phase1Date,
null,
]);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable('phase_history');
if (table) {
const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf('questionId') !== -1);
if (foreignKey) {
await queryRunner.dropForeignKey('phase_history', foreignKey);
await queryRunner.createForeignKey(
'phase_history',
new TableForeignKey({
columnNames: ['villageId'],
referencedColumnNames: ['id'],
referencedTableName: 'village',
onDelete: 'CASCADE',
}),
);
await queryRunner.dropIndex('phase_history', 'IDX_PHASE_HISTORY');
await queryRunner.dropTable('phase_history');
}
}
}
}

0 comments on commit 8306688

Please sign in to comment.