Skip to content

Commit 57ddf9d

Browse files
committed
fix: change column migrations
1 parent 8c5d9f3 commit 57ddf9d

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

src/migrations/1552670893473-first-version.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class FirstVersion1552670893473 implements MigrationInterface {
8989
{
9090
name: 'user_id',
9191
type: 'varchar',
92-
length: '255',
92+
length: '30',
9393
isNullable: false
9494
},
9595
{
@@ -101,19 +101,19 @@ export class FirstVersion1552670893473 implements MigrationInterface {
101101
{
102102
name: 'post_id',
103103
type: 'varchar',
104-
length: '255',
104+
length: '30',
105105
isNullable: false
106106
},
107107
{
108108
name: 'comment_id',
109109
type: 'varchar',
110-
length: '255',
110+
length: '30',
111111
isNullable: true
112112
},
113113
{
114114
name: 'reply_comment_id',
115115
type: 'varchar',
116-
length: '255',
116+
length: '30',
117117
isNullable: true
118118
},
119119
{
@@ -131,7 +131,7 @@ export class FirstVersion1552670893473 implements MigrationInterface {
131131
{
132132
name: 'description',
133133
type: 'varchar',
134-
length: '255',
134+
length: '200',
135135
isNullable: true
136136
},
137137
{

src/migrations/1553657266252-add-report-timestamp.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { MigrationInterface, QueryRunner, Table, TableColumn } from 'typeorm';
2+
3+
export class UpdateColumns1553657266252 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<any> {
5+
await this._createColumns(queryRunner);
6+
await this._updateColumns(queryRunner);
7+
}
8+
9+
public async down(queryRunner: QueryRunner): Promise<any> {
10+
await queryRunner.dropColumn('user_statuses', 'reported_at');
11+
await this._undoUpdateColumns(queryRunner);
12+
}
13+
14+
private async _createColumns(queryRunner: QueryRunner) {
15+
await queryRunner.addColumn(
16+
'user_statuses',
17+
new TableColumn({
18+
name: 'reported_at',
19+
type: 'timestamp',
20+
isNullable: false,
21+
default: 'now()'
22+
})
23+
);
24+
}
25+
26+
private async _updateColumns(queryRunner: QueryRunner) {
27+
await queryRunner.query(
28+
'ALTER TABLE "user_statuses" ALTER COLUMN "user_id" TYPE varchar(255)'
29+
);
30+
await queryRunner.query(
31+
'ALTER TABLE "user_statuses" ALTER COLUMN "username" TYPE varchar(255)'
32+
);
33+
await queryRunner.query(
34+
'ALTER TABLE "user_statuses" ALTER COLUMN "post_id" TYPE varchar(255)'
35+
);
36+
await queryRunner.query(
37+
'ALTER TABLE "user_statuses" ALTER COLUMN "comment_id" TYPE varchar(255)'
38+
);
39+
await queryRunner.query(
40+
'ALTER TABLE "user_statuses" ALTER COLUMN "reply_comment_id" TYPE varchar(255)'
41+
);
42+
await queryRunner.query(
43+
'ALTER TABLE "user_statuses" ALTER COLUMN "description" TYPE varchar(255)'
44+
);
45+
46+
await queryRunner.query(
47+
'ALTER TABLE "reporters" ALTER COLUMN "user_id" TYPE varchar(255)'
48+
);
49+
}
50+
51+
private async _undoUpdateColumns(queryRunner: QueryRunner) {
52+
await queryRunner.query(
53+
'ALTER TABLE "user_statuses" ALTER COLUMN "user_id" TYPE varchar(30)'
54+
);
55+
await queryRunner.query(
56+
'ALTER TABLE "user_statuses" ALTER COLUMN "username" TYPE varchar(30)'
57+
);
58+
await queryRunner.query(
59+
'ALTER TABLE "user_statuses" ALTER COLUMN "post_id" TYPE varchar(30)'
60+
);
61+
await queryRunner.query(
62+
'ALTER TABLE "user_statuses" ALTER COLUMN "comment_id" TYPE varchar(30)'
63+
);
64+
await queryRunner.query(
65+
'ALTER TABLE "user_statuses" ALTER COLUMN "reply_comment_id" TYPE varchar(30)'
66+
);
67+
await queryRunner.query(
68+
'ALTER TABLE "user_statuses" ALTER COLUMN "description" TYPE varchar(200)'
69+
);
70+
71+
await queryRunner.query('ALTER TABLE "reporters" ALTER COLUMN "user_id" TYPE varchar(30)');
72+
}
73+
}

src/models/reporter.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class Reporter {
1010
@Column({ type: 'enum', enum: Platform })
1111
public platform: Platform;
1212

13-
@Column({ name: 'user_id', type: 'varchar', length: 30 })
13+
@Column({ name: 'user_id', type: 'varchar', length: 255 })
1414
public userId: string;
1515

1616
@Column({ name: 'reporter_key', type: 'varchar', length: 30, nullable: false })

src/models/user-status.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class UserStatus {
2222
@Column({ name: 'user_id', type: 'varchar', length: 255, nullable: false })
2323
public userId: string;
2424

25-
@Column({ type: 'varchar', length: 30, nullable: true })
25+
@Column({ type: 'varchar', length: 255, nullable: true })
2626
public username?: string;
2727

2828
@Column({ name: 'post_id', type: 'varchar', length: 255, nullable: false })

0 commit comments

Comments
 (0)