dsi-icl/optimise

View on GitHub
packages/optimise-core/src/db/patient_pii.table.js

Summary

Maintainability
B
6 hrs
Test Coverage
import { tableMove, tableCopyBack } from '../utils/db-mover';

export const TABLE_NAME = 'PATIENT_PII';
export const PRIORITY = 2;
export default async (dbcon, version) => {
    switch (version) {
        case 1:
        case 2:
        case 3:
            await tableMove(TABLE_NAME, version);
            await dbcon().schema.createTable(TABLE_NAME, (table) => {
                table.increments('id').primary();
                table.integer('patient').notNullable().references('id').inTable('PATIENTS').onDelete('CASCADE');
                table.text('firstName').notNullable();
                table.text('surname').notNullable();
                table.text('fullAddress').notNullable();
                table.text('postcode').notNullable();
                table.text('createdTime').notNullable().defaultTo(dbcon().fn.now());
                table.integer('createdByUser').notNullable().references('id').inTable('USERS');
                table.text('deleted').notNullable().defaultTo('-');
                table.unique(['patient', 'deleted'], `UNIQUE_${Date.now()}_${TABLE_NAME}`);
            });
            await tableCopyBack(TABLE_NAME);
            break;
        case 16:
            await dbcon().schema.table(TABLE_NAME, (table) => {
                table.dropForeign('patient');
                table.foreign('patient').references('id').inTable('PATIENTS').onDelete('CASCADE');
            });
            break;
        case 17:
            await dbcon().schema.table(TABLE_NAME, (table) => {
                table.dropForeign('patient');
                table.foreign('patient').references('id').inTable('PATIENTS').onDelete('CASCADE');
            });
            break;
        case 18:
            await dbcon().schema.table(TABLE_NAME, (table) => {
                table.dropForeign('patient');
                table.foreign('patient').references('id').inTable('PATIENTS').onDelete('CASCADE');
            });
            break;
        default:
            break;
    }
};