|
| 1 | +import { MigrationInterface, QueryRunner, Table } from 'typeorm'; |
| 2 | + |
| 3 | +import { generateEnumQuery, getEnumName } from '../core'; |
| 4 | + |
| 5 | +export class CreateReportersTable1552670893473 implements MigrationInterface { |
| 6 | + public async up(queryRunner: QueryRunner): Promise<any> { |
| 7 | + await this._createEnums(queryRunner); |
| 8 | + await this._createTables(queryRunner); |
| 9 | + } |
| 10 | + |
| 11 | + public async down(queryRunner: QueryRunner): Promise<any> { |
| 12 | + await queryRunner.dropTable('user_statuses', true, true); |
| 13 | + await queryRunner.dropTable('reporters', true, true); |
| 14 | + await queryRunner.query(generateEnumQuery('drop', 'platform')); |
| 15 | + await queryRunner.query(generateEnumQuery('drop', 'status')); |
| 16 | + await queryRunner.query(generateEnumQuery('drop', 'reason')); |
| 17 | + } |
| 18 | + |
| 19 | + private async _createEnums(queryRunner: QueryRunner) { |
| 20 | + await queryRunner.query( |
| 21 | + generateEnumQuery('create', 'platform', ['TWITTER', 'FACEBOOK', 'INSTAGRAM']) |
| 22 | + ); |
| 23 | + await queryRunner.query( |
| 24 | + generateEnumQuery('create', 'status', [ |
| 25 | + 'REPORTED', |
| 26 | + 'IN_PROCESS', |
| 27 | + 'BOT', |
| 28 | + 'NOT_BOT', |
| 29 | + 'DUPLICATE' |
| 30 | + ]) |
| 31 | + ); |
| 32 | + await queryRunner.query( |
| 33 | + generateEnumQuery('create', 'reason', ['BOT', 'VIOLENCE', 'FAKE']) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + private async _createTables(queryRunner: QueryRunner) { |
| 38 | + await queryRunner.createTable( |
| 39 | + new Table({ |
| 40 | + name: 'reporters', |
| 41 | + columns: [ |
| 42 | + { |
| 43 | + name: 'id', |
| 44 | + type: 'int', |
| 45 | + isPrimary: true, |
| 46 | + isGenerated: true, |
| 47 | + generationStrategy: 'increment' |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'platform', |
| 51 | + type: getEnumName('platform'), |
| 52 | + isNullable: false |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'user_id', |
| 56 | + type: 'varchar', |
| 57 | + length: '30', |
| 58 | + isNullable: false |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'reporter_key', |
| 62 | + type: 'varchar', |
| 63 | + length: '30', |
| 64 | + isNullable: false, |
| 65 | + isUnique: true |
| 66 | + } |
| 67 | + ], |
| 68 | + uniques: [{ columnNames: ['platform', 'user_id'] }] |
| 69 | + }), |
| 70 | + true |
| 71 | + ); |
| 72 | + |
| 73 | + await queryRunner.createTable( |
| 74 | + new Table({ |
| 75 | + name: 'user_statuses', |
| 76 | + columns: [ |
| 77 | + { |
| 78 | + name: 'id', |
| 79 | + type: 'int', |
| 80 | + isPrimary: true, |
| 81 | + isGenerated: true, |
| 82 | + generationStrategy: 'increment' |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'platform', |
| 86 | + type: getEnumName('platform'), |
| 87 | + isNullable: false |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'user_id', |
| 91 | + type: 'varchar', |
| 92 | + length: '30', |
| 93 | + isNullable: false |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'post_id', |
| 97 | + type: 'varchar', |
| 98 | + length: '30', |
| 99 | + isNullable: false |
| 100 | + }, |
| 101 | + { |
| 102 | + name: 'comment_id', |
| 103 | + type: 'varchar', |
| 104 | + length: '30', |
| 105 | + isNullable: true |
| 106 | + }, |
| 107 | + { |
| 108 | + name: 'reply_comment_id', |
| 109 | + type: 'varchar', |
| 110 | + length: '30', |
| 111 | + isNullable: true |
| 112 | + }, |
| 113 | + { |
| 114 | + name: 'reasons', |
| 115 | + type: getEnumName('reason'), |
| 116 | + isArray: true, |
| 117 | + isNullable: false |
| 118 | + }, |
| 119 | + { |
| 120 | + name: 'status', |
| 121 | + type: getEnumName('status'), |
| 122 | + default: `'REPORTED'`, |
| 123 | + isNullable: false |
| 124 | + }, |
| 125 | + { |
| 126 | + name: 'description', |
| 127 | + type: 'varchar', |
| 128 | + length: '200', |
| 129 | + isNullable: true |
| 130 | + }, |
| 131 | + { |
| 132 | + name: 'reporter_key', |
| 133 | + type: 'varchar', |
| 134 | + length: '30', |
| 135 | + isNullable: false |
| 136 | + } |
| 137 | + ], |
| 138 | + uniques: [ |
| 139 | + { |
| 140 | + columnNames: ['platform', 'user_id', 'post_id', 'comment_id', 'reply_comment_id'] |
| 141 | + } |
| 142 | + ], |
| 143 | + foreignKeys: [ |
| 144 | + { |
| 145 | + columnNames: ['reporter_key'], |
| 146 | + referencedColumnNames: ['reporter_key'], |
| 147 | + referencedTableName: 'reporters', |
| 148 | + onDelete: 'CASCADE' |
| 149 | + } |
| 150 | + ] |
| 151 | + }), |
| 152 | + true, |
| 153 | + true |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments