Created
June 22, 2020 01:20
-
-
Save nson22/9ed91277fd3d45346f8ad2915667e8cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; | |
export default class CreateCategories1592785085350 | |
implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'categories', | |
columns: [ | |
{ | |
name: 'id', | |
type: 'uuid', | |
isPrimary: true, | |
generationStrategy: 'uuid', | |
default: 'uuid_generate_v4()', | |
}, | |
{ | |
name: 'title', | |
type: 'varchar', | |
isUnique: true, | |
}, | |
{ | |
name: 'created_at', | |
type: 'timestamp', | |
default: 'now()', | |
}, | |
{ | |
name: 'updated_at', | |
type: 'timestamp', | |
default: 'now()', | |
}, | |
], | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropTable('categories'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
MigrationInterface, | |
QueryRunner, | |
Table, | |
TableForeignKey, | |
} from 'typeorm'; | |
export default class CreateTransactions1592785134812 | |
implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'transactions', | |
columns: [ | |
{ | |
name: 'id', | |
type: 'uuid', | |
isPrimary: true, | |
generationStrategy: 'uuid', | |
default: 'uuid_generate_v4()', | |
}, | |
{ | |
name: 'title', | |
type: 'varchar', | |
}, | |
{ | |
name: 'value', | |
type: 'numeric', | |
}, | |
{ | |
name: 'type', | |
type: 'varchar', | |
}, | |
{ | |
name: 'category_id', | |
type: 'uuid', | |
isNullable: true, | |
}, | |
{ | |
name: 'created_at', | |
type: 'timestamp', | |
default: 'now()', | |
}, | |
{ | |
name: 'updated_at', | |
type: 'timestamp', | |
default: 'now()', | |
}, | |
], | |
}), | |
); | |
await queryRunner.createForeignKey( | |
'transactions', | |
new TableForeignKey({ | |
name: 'TransactionCategory', | |
columnNames: ['category_id'], | |
referencedColumnNames: ['id'], | |
referencedTableName: 'categories', | |
onDelete: 'SET NULL', | |
onUpdate: 'CASCADE', | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropForeignKey('transactions', 'TransactionCategory'); | |
await queryRunner.dropTable('transactions'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "postgres", | |
"host": "localhost", | |
"port": 5432, | |
"username": "postgres", | |
"password": "docker", | |
"database": "gostack_desafio06", | |
"entities": ["./src/models/*.ts"], | |
"migrations": ["./src/database/migrations/*.ts"], | |
"cli": { | |
"migrationsDir": "./src/database/migrations" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment