Skip to content

Commit a1e68c4

Browse files
committed
Setting up Migrations
1 parent d2ccb73 commit a1e68c4

File tree

6 files changed

+90
-10
lines changed

6 files changed

+90
-10
lines changed

reminder.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,31 @@ nest g class coffees/dto/create-coffee.dto --no-spec
2323

2424
/* Generate Entity Class */
2525
nest g class coffees/entities/flavor.entity --no-spec
26+
27+
28+
// Creating a TypeOrm Migration
29+
npx typeorm migration:create src/migrations/CoffeeRefactor
30+
// CoffeeRefactor being the NAME we are giving "this" migration
31+
32+
33+
/* RUNNING MIGRATIONS */
34+
35+
/**
36+
* 💡 Remember 💡
37+
* You must BUILD your Nest project (so that everything is output to the `/dist/` folder,
38+
* before a Migration can run, it needs compilated files.
39+
*/
40+
41+
// Compile project first
42+
npm run build
43+
44+
// Run migration(s)
45+
npx typeorm migration:run -d dist/typeorm-cli.config
46+
47+
// REVERT migration(s)
48+
npx typeorm migration:revert -d dist/typeorm-cli.config
49+
50+
// Let TypeOrm generate migrations (for you)
51+
npx typeorm migration:generate src/migrations/SchemaSync -d dist/typeorm-cli.config
52+
53+

src/coffees/entities/coffee.entity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ export class Coffee {
1313
id: number;
1414

1515
@Column()
16-
name: string;
16+
title: string;
1717

1818
@Column()
1919
brand: string;
2020

21+
@Column({ nullable: true })
22+
description: string;
23+
2124
@Column({ default: 0 })
2225
recommendations: number;
2326

src/events/entities/event.entity.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
1+
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
22

3+
@Index(['name', 'type'])
34
@Entity()
45
export class Event {
5-
@PrimaryGeneratedColumn()
6-
id: number;
6+
@PrimaryGeneratedColumn()
7+
id: number;
78

8-
@Column()
9-
type: string;
9+
@Column()
10+
type: string;
1011

11-
@Column()
12-
name: string;
12+
@Index()
13+
@Column()
14+
name: string;
1315

14-
@Column('json')
15-
payload: Record<string, any>;
16+
@Column('json')
17+
payload: Record<string, any>;
1618
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class CoffeeRefactor1675676028883 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<any> {
5+
await queryRunner.query(
6+
`ALTER TABLE "coffee" RENAME COLUMN "name" TO "title"`,
7+
);
8+
}
9+
10+
public async down(queryRunner: QueryRunner): Promise<any> {
11+
await queryRunner.query(
12+
`ALTER TABLE "coffee" RENAME COLUMN "title" TO "name"`,
13+
);
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class SchemaSync1675678328747 implements MigrationInterface {
4+
name = 'SchemaSync1675678328747'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "coffee" ADD "description" character varying`);
8+
await queryRunner.query(`ALTER TABLE "coffee" ADD "recommendations" integer NOT NULL DEFAULT '0'`);
9+
}
10+
11+
public async down(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(`ALTER TABLE "coffee" DROP COLUMN "recommendations"`);
13+
await queryRunner.query(`ALTER TABLE "coffee" DROP COLUMN "description"`);
14+
}
15+
16+
}

typeorm-cli.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Coffee } from 'src/coffees/entities/coffee.entity';
2+
import { Flavor } from 'src/coffees/entities/flavor.entity';
3+
import { CoffeeRefactor1675676028883 } from 'src/migrations/1675676028883-CoffeeRefactor';
4+
import { SchemaSync1675678328747 } from 'src/migrations/1675678328747-SchemaSync';
5+
import { DataSource } from 'typeorm';
6+
7+
export default new DataSource({
8+
type: 'postgres',
9+
host: 'localhost',
10+
port: 5432,
11+
username: 'postgres',
12+
password: 'admin',
13+
database: 'iluvcoffee',
14+
entities: [Coffee, Flavor],
15+
migrations: [CoffeeRefactor1675676028883, SchemaSync1675678328747],
16+
});

0 commit comments

Comments
 (0)