Skip to content

Commit 7700cab

Browse files
author
hirsch88
committed
Add migration to the e2e tests
1 parent 282e758 commit 7700cab

File tree

9 files changed

+88
-37
lines changed

9 files changed

+88
-37
lines changed

src/api/models/Pet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export class Pet {
1818
public age: number;
1919

2020
@Column({
21+
name: 'user_id',
2122
nullable: true,
2223
})
2324
public userId: number;
2425

2526
@ManyToOne(type => User, user => user.pets)
26-
@JoinColumn({ name: 'userId' })
27+
@JoinColumn({ name: 'user_id' })
2728
public user: User;
2829

2930
public toString(): string {

src/api/repositories/PetRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { Pet } from '../models/Pet';
55
export class PetRepository extends Repository<Pet> {
66

77
/**
8-
* Find by userId is used for our data-loader to get all needed pets in one query.
8+
* Find by user_id is used for our data-loader to get all needed pets in one query.
99
*/
1010
public findByUserIds(ids: string[]): Promise<Pet[]> {
1111
return this.createQueryBuilder()
1212
.select()
13-
.where(`pet.userId IN (${ids.map(id => `'${id}'`).join(', ')})`)
13+
.where(`pet.user_id IN (${ids.map(id => `'${id}'`).join(', ')})`)
1414
.getMany();
1515
}
1616

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
import { MigrationInterface, QueryRunner } from 'typeorm';
1+
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
22

33
export class CreateUserTable1511105183653 implements MigrationInterface {
44

55
public async up(queryRunner: QueryRunner): Promise<any> {
6-
await queryRunner.query(`
7-
CREATE TABLE \`user\` (
8-
\`id\` varchar(255) NOT NULL PRIMARY KEY,
9-
\`first_name\` varchar(255) NOT NULL,
10-
\`last_name\` varchar(255) NOT NULL,
11-
\`email\` varchar(255) NOT NULL) ENGINE=InnoDB;`
12-
);
6+
const table = new Table('user', [
7+
{
8+
name: 'id',
9+
type: 'varchar',
10+
length: 255,
11+
isPrimary: true,
12+
isNullable: false,
13+
}, {
14+
name: 'first_name',
15+
type: 'varchar',
16+
length: 255,
17+
isPrimary: false,
18+
isNullable: false,
19+
}, {
20+
name: 'last_name',
21+
type: 'varchar',
22+
length: 255,
23+
isPrimary: false,
24+
isNullable: false,
25+
}, {
26+
name: 'email',
27+
type: 'varchar',
28+
length: 255,
29+
isPrimary: false,
30+
isNullable: false,
31+
},
32+
]);
33+
await queryRunner.createTable(table);
1334
}
1435

1536
public async down(queryRunner: QueryRunner): Promise<any> {
16-
await queryRunner.query(`DROP TABLE \`user\`;`);
37+
await queryRunner.dropTable('user');
1738
}
1839

1940
}
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
import { MigrationInterface, QueryRunner } from 'typeorm';
1+
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
22

33
export class CreatePetTable1512663524808 implements MigrationInterface {
44

55
public async up(queryRunner: QueryRunner): Promise<any> {
6-
await queryRunner.query(`
7-
CREATE TABLE \`pet\` (
8-
\`id\` varchar(255) NOT NULL PRIMARY KEY,
9-
\`name\` varchar(255) NOT NULL,
10-
\`age\` int(11) NOT NULL,
11-
\`userId\` varchar(255)) ENGINE=InnoDB;`
12-
);
6+
const table = new Table('pet', [
7+
{
8+
name: 'id',
9+
type: 'varchar',
10+
length: 255,
11+
isPrimary: true,
12+
isNullable: false,
13+
}, {
14+
name: 'name',
15+
type: 'varchar',
16+
length: 255,
17+
isPrimary: false,
18+
isNullable: false,
19+
}, {
20+
name: 'age',
21+
type: 'int',
22+
length: 11,
23+
isPrimary: false,
24+
isNullable: false,
25+
}, {
26+
name: 'user_id',
27+
type: 'varchar',
28+
length: 255,
29+
isPrimary: false,
30+
isNullable: true,
31+
},
32+
]);
33+
await queryRunner.createTable(table);
1334
}
1435

1536
public async down(queryRunner: QueryRunner): Promise<any> {
16-
await queryRunner.query(`DROP TABLE \`pet\`;`);
37+
await queryRunner.dropTable('pet');
1738
}
1839

1940
}

src/database/migrations/1512663990063-AddUserRelationToPetTable.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { MigrationInterface, QueryRunner } from 'typeorm';
1+
import { MigrationInterface, QueryRunner, TableForeignKey } from 'typeorm';
22

33
export class AddUserRelationToPetTable1512663990063 implements MigrationInterface {
44

5+
private tableForeignKey = new TableForeignKey(
6+
'fk_user_pet',
7+
['user_id'],
8+
['id'],
9+
'user',
10+
''
11+
);
12+
513
public async up(queryRunner: QueryRunner): Promise<any> {
6-
await queryRunner.query(`
7-
ALTER TABLE \`pet\`
8-
ADD CONSTRAINT \`fk_user_pet\`
9-
FOREIGN KEY (\`userId\`)
10-
REFERENCES \`user\`(\`id\`);`
11-
);
14+
await queryRunner.createForeignKey('pet', this.tableForeignKey);
1215
}
1316

1417
public async down(queryRunner: QueryRunner): Promise<any> {
15-
await queryRunner.query(`
16-
ALTER TABLE \`pet\`
17-
DROP FOREIGN KEY \`fk_user_pet\`;`
18-
);
18+
await queryRunner.dropForeignKey('pet', this.tableForeignKey);
1919
}
2020

2121
}

src/loaders/typeormLoader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const typeormLoader: MicroframeworkLoader = async (settings: Microframewo
1515
synchronize: env.db.synchronize,
1616
logging: env.db.logging,
1717
entities: env.app.dirs.entities,
18+
migrations: env.app.dirs.migrations,
1819
});
1920

2021
if (settings) {

test/e2e/api/users.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getFactory } from './../../../src/lib/seeds/index';
55
import { Factory } from './../../../src/lib/seeds/Factory';
66
import { User } from './../../../src/api/models/User';
77
import { bootstrapApp, BootstrapSettings } from '../utils/bootstrap';
8-
import { synchronizeDatabase, closeDatabase } from '../../utils/database';
8+
import { migrateDatabase, closeDatabase } from '../../utils/database';
99
import { fakeAuthenticationForUser } from '../utils/auth';
1010

1111

@@ -20,7 +20,7 @@ describe('/api/users', () => {
2020
let bruce: User;
2121
let authServer: nock.Scope;
2222
beforeAll(async () => settings = await bootstrapApp());
23-
beforeAll(async () => synchronizeDatabase(settings.connection));
23+
beforeAll(async () => migrateDatabase(settings.connection));
2424
beforeAll(async () => factory = getFactory(settings.connection));
2525
beforeAll(async () => bruce = await factory.runSeed<User>(CreateBruce));
2626
beforeAll(async () => authServer = fakeAuthenticationForUser(bruce, true));

test/integration/PetService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Connection } from 'typeorm';
33

44
import { Pet } from '../../src/api/models/Pet';
55
import { PetService } from './../../src/api/services/PetService';
6-
import { createDatabaseConnection, synchronizeDatabase, closeDatabase } from '../utils/database';
6+
import { createDatabaseConnection, migrateDatabase, closeDatabase } from '../utils/database';
77

88
describe('PetService', () => {
99

@@ -13,7 +13,7 @@ describe('PetService', () => {
1313

1414
let connection: Connection;
1515
beforeAll(async () => connection = await createDatabaseConnection());
16-
beforeEach(() => synchronizeDatabase(connection));
16+
beforeEach(() => migrateDatabase(connection));
1717

1818
// -------------------------------------------------------------------------
1919
// Tear down

test/utils/database.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ export const createDatabaseConnection = async (): Promise<Connection> => {
1010
database: env.db.database,
1111
logging: env.db.logging,
1212
entities: env.app.dirs.entities,
13+
migrations: env.app.dirs.migrations,
1314
});
1415
return connection;
1516
};
1617

17-
export const synchronizeDatabase = (connection: Connection) => {
18+
export const synchronizeDatabase = async (connection: Connection) => {
19+
await connection.dropDatabase();
1820
return connection.synchronize(true);
1921
};
2022

23+
export const migrateDatabase = async (connection: Connection) => {
24+
await connection.dropDatabase();
25+
return connection.runMigrations();
26+
};
27+
2128
export const closeDatabase = (connection: Connection) => {
2229
return connection.close();
2330
};

0 commit comments

Comments
 (0)