2020-09-16 22:32:43 +03:00
|
|
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
|
|
|
import { decrypt, encrypt } from "../utils/crypt";
|
|
|
|
|
|
|
|
export class EncryptExistingMessages1600283341726 implements MigrationInterface {
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<any> {
|
|
|
|
// 1. Delete non-permanent messages
|
|
|
|
await queryRunner.query("DELETE FROM messages WHERE is_permanent = 0");
|
|
|
|
|
|
|
|
// 2. Encrypt all permanent messages
|
|
|
|
const messages = await queryRunner.query("SELECT id, data FROM messages");
|
|
|
|
for (const message of messages) {
|
2021-10-09 14:21:23 +03:00
|
|
|
const encryptedData = await encrypt(message.data);
|
2020-09-16 22:32:43 +03:00
|
|
|
await queryRunner.query("UPDATE messages SET data = ? WHERE id = ?", [encryptedData, message.id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<any> {
|
|
|
|
// Decrypt all messages
|
|
|
|
const messages = await queryRunner.query("SELECT id, data FROM messages");
|
|
|
|
for (const message of messages) {
|
2021-10-09 14:21:23 +03:00
|
|
|
const decryptedData = await decrypt(message.data);
|
2020-09-16 22:32:43 +03:00
|
|
|
await queryRunner.query("UPDATE messages SET data = ? WHERE id = ?", [decryptedData, message.id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|