Skip to content

Commit

Permalink
fix: change column migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
eranshmil committed Apr 1, 2019
1 parent 8c5d9f3 commit 57ddf9d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/migrations/1552670893473-first-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class FirstVersion1552670893473 implements MigrationInterface {
{
name: 'user_id',
type: 'varchar',
length: '255',
length: '30',
isNullable: false
},
{
Expand All @@ -101,19 +101,19 @@ export class FirstVersion1552670893473 implements MigrationInterface {
{
name: 'post_id',
type: 'varchar',
length: '255',
length: '30',
isNullable: false
},
{
name: 'comment_id',
type: 'varchar',
length: '255',
length: '30',
isNullable: true
},
{
name: 'reply_comment_id',
type: 'varchar',
length: '255',
length: '30',
isNullable: true
},
{
Expand All @@ -131,7 +131,7 @@ export class FirstVersion1552670893473 implements MigrationInterface {
{
name: 'description',
type: 'varchar',
length: '255',
length: '200',
isNullable: true
},
{
Expand Down
23 changes: 0 additions & 23 deletions src/migrations/1553657266252-add-report-timestamp.ts

This file was deleted.

73 changes: 73 additions & 0 deletions src/migrations/1553657266252-update-columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { MigrationInterface, QueryRunner, Table, TableColumn } from 'typeorm';

export class UpdateColumns1553657266252 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await this._createColumns(queryRunner);
await this._updateColumns(queryRunner);
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropColumn('user_statuses', 'reported_at');
await this._undoUpdateColumns(queryRunner);
}

private async _createColumns(queryRunner: QueryRunner) {
await queryRunner.addColumn(
'user_statuses',
new TableColumn({
name: 'reported_at',
type: 'timestamp',
isNullable: false,
default: 'now()'
})
);
}

private async _updateColumns(queryRunner: QueryRunner) {
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "user_id" TYPE varchar(255)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "username" TYPE varchar(255)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "post_id" TYPE varchar(255)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "comment_id" TYPE varchar(255)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "reply_comment_id" TYPE varchar(255)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "description" TYPE varchar(255)'
);

await queryRunner.query(
'ALTER TABLE "reporters" ALTER COLUMN "user_id" TYPE varchar(255)'
);
}

private async _undoUpdateColumns(queryRunner: QueryRunner) {
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "user_id" TYPE varchar(30)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "username" TYPE varchar(30)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "post_id" TYPE varchar(30)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "comment_id" TYPE varchar(30)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "reply_comment_id" TYPE varchar(30)'
);
await queryRunner.query(
'ALTER TABLE "user_statuses" ALTER COLUMN "description" TYPE varchar(200)'
);

await queryRunner.query('ALTER TABLE "reporters" ALTER COLUMN "user_id" TYPE varchar(30)');
}
}
2 changes: 1 addition & 1 deletion src/models/reporter.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Reporter {
@Column({ type: 'enum', enum: Platform })
public platform: Platform;

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

@Column({ name: 'reporter_key', type: 'varchar', length: 30, nullable: false })
Expand Down
2 changes: 1 addition & 1 deletion src/models/user-status.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class UserStatus {
@Column({ name: 'user_id', type: 'varchar', length: 255, nullable: false })
public userId: string;

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

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

0 comments on commit 57ddf9d

Please sign in to comment.