Skip to content

Commit

Permalink
fix: Incorrect migration generated when multiple views are updated in…
Browse files Browse the repository at this point in the history
… a single migration (#7587)

* test: Add test for github issue 7586

* fix: Oddly indexed views are not dropped in migration (#7586)

Co-authored-by: Svetlozar <ext-svetlozar@getitdone.co>
  • Loading branch information
zaro and Svetlozar committed Apr 24, 2021
1 parent 7eb0327 commit 0b103dd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/schema-builder/RdbmsSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
}

protected async dropOldViews(): Promise<void> {
const droppedViews: Set<View> = new Set();
for (const view of this.queryRunner.loadedViews) {
const existViewMetadata = this.viewEntityToSyncMetadatas.find(metadata => {
const database = metadata.database && metadata.database !== this.connection.driver.database ? metadata.database : undefined;
Expand All @@ -427,8 +428,9 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {

// drop an old view
await this.queryRunner.dropView(view);
this.queryRunner.loadedViews.splice(this.queryRunner.loadedViews.indexOf(view), 1);
droppedViews.add(view);
}
this.queryRunner.loadedViews = this.queryRunner.loadedViews.filter(view => !droppedViews.has(view));
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/github-issues/7586/entity/Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Column, Entity, PrimaryGeneratedColumn} from "../../../../src";


@Entity()
export class TestEntity {
@PrimaryGeneratedColumn()
id: number

@Column("varchar")
type: string;
}
14 changes: 14 additions & 0 deletions test/github-issues/7586/entity/ViewA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ViewColumn, ViewEntity} from "../../../../src";

@ViewEntity({
expression: `
select * from test_entity -- V1 simlate view change with comment
`
})
export class ViewA {
@ViewColumn()
id: number

@ViewColumn()
type: string;
}
14 changes: 14 additions & 0 deletions test/github-issues/7586/entity/ViewB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ViewColumn, ViewEntity} from "../../../../src";

@ViewEntity({
expression: `
select * from test_entity -- V1 simlate view change with comment
`
})
export class ViewB {
@ViewColumn()
id: number

@ViewColumn()
type: string;
}
14 changes: 14 additions & 0 deletions test/github-issues/7586/entity/ViewC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ViewColumn, ViewEntity} from "../../../../src";

@ViewEntity({
expression: `
select * from test_entity -- V1 simlate view change with comment
`
})
export class ViewC {
@ViewColumn()
id: number

@ViewColumn()
type: string;
}
30 changes: 30 additions & 0 deletions test/github-issues/7586/issue-7586.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "reflect-metadata";
import {Connection} from "../../../src";
import {closeTestingConnections, createTestingConnections} from "../../utils/test-utils";
import {TestEntity} from "./entity/Test";
import {ViewA} from "./entity/ViewA";
import {ViewB} from "./entity/ViewB";
import {ViewC} from "./entity/ViewC";

describe("github issues > #7586 Oddly indexed views are not dropped in migration", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
entities: [TestEntity, ViewA, ViewB, ViewC],
}));
after(() => closeTestingConnections(connections));

it("should generate drop queries for all views", () => Promise.all(connections.map(async connection => {
const expectedDrops: RegExp[] = [];
for(const view of [ViewA, ViewB, ViewC]){
const metadata = connection.getMetadata(view);
metadata.expression = (metadata.expression as string)?.replace('V1', 'V2')
expectedDrops.push(new RegExp(`^DROP\\s+VIEW.*"${metadata.tableName}"`))
}
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.downQueries.filter(q => expectedDrops.find(expected => q.query.match(expected))).length.should.be.equal(expectedDrops.length);
sqlInMemory.upQueries.filter(q => expectedDrops.find(expected => q.query.match(expected))).length.should.be.equal(expectedDrops.length);
})));
});

0 comments on commit 0b103dd

Please sign in to comment.