Skip to content

Commit

Permalink
fix(postgres): postgres query runner to create materialized view (#4877)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackCuthbert authored and pleerock committed Oct 18, 2019
1 parent 13f06bf commit d744966
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
}

protected createViewSql(view: View): Query {
const materializedClause = view.materialized ? "" : "MATERIALIZED ";
const materializedClause = view.materialized ? "MATERIALIZED " : "";
if (typeof view.expression === "string") {
return new Query(`CREATE ${materializedClause}VIEW "${view.name}" AS ${view.expression}`);
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,10 +1739,13 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
}

protected createViewSql(view: View): Query {
const materializedClause = view.materialized ? "MATERIALIZED " : "";
const viewName = this.escapePath(view);

if (typeof view.expression === "string") {
return new Query(`CREATE VIEW ${this.escapePath(view)} AS ${view.expression}`);
return new Query(`CREATE ${materializedClause}VIEW ${viewName} AS ${view.expression}`);
} else {
return new Query(`CREATE VIEW ${this.escapePath(view)} AS ${view.expression(this.connection).getQuery()}`);
return new Query(`CREATE ${materializedClause}VIEW ${viewName} AS ${view.expression(this.connection).getQuery()}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema-builder/view/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class View {
const options: ViewOptions = {
name: driver.buildTableName(entityMetadata.tableName, entityMetadata.schema, entityMetadata.database),
expression: entityMetadata.expression!,
materialized: false
materialized: entityMetadata.tableMetadataArgs.materialized
};

return new View(options);
Expand Down
24 changes: 24 additions & 0 deletions test/functional/view-entity/postgres/entity/PostByCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Connection} from "../../../../../src";
import {ViewColumn} from "../../../../../src/decorator/columns/ViewColumn";
import {ViewEntity} from "../../../../../src/decorator/entity-view/ViewEntity";
import {Category} from "./Category";
import {Post} from "./Post";

@ViewEntity({
materialized: true,
expression: (connection: Connection) => connection.createQueryBuilder()
.select("category.name", "categoryName")
.addSelect("COUNT(post.id)", "postCount")
.from(Post, "post")
.innerJoin(Category, "category", "category.id = post.categoryId")
.groupBy("category.name")
})
export class PostByCategory {

@ViewColumn()
categoryName: string;

@ViewColumn()
postCount: number;

}
28 changes: 27 additions & 1 deletion test/functional/view-entity/postgres/view-entity-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {Connection} from "../../../../src";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Post} from "./entity/Post";
import {PostCategory} from "./entity/PostCategory";
import {PostByCategory} from "./entity/PostByCategory";

describe("view entity > postgres", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres", "cockroachdb"]
enabledDrivers: ["postgres"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
Expand All @@ -26,6 +27,31 @@ describe("view entity > postgres", () => {

})));

it("should not return data without refreshing the materialized view", () => Promise.all(connections.map(async connection => {
const queryRunner = connection.createQueryRunner();

const category1 = new Category();
category1.name = "Cars";
await queryRunner.manager.save(category1);

const post1 = new Post();
post1.name = "About BMW";
post1.categoryId = category1.id;
await queryRunner.manager.save(post1);

const emptyResult = await queryRunner.manager.find(PostByCategory);
emptyResult.length.should.be.equal(0);

await queryRunner.query("REFRESH MATERIALIZED VIEW post_by_category");
const resultWithData = await queryRunner.manager.find(PostByCategory);
resultWithData.length.should.be.equal(1);

expect(resultWithData[0].categoryName).to.eq(category1.name);
expect(Number(resultWithData[0].postCount)).to.eq(1);

await queryRunner.release();
})));

it("should correctly return data from View", () => Promise.all(connections.map(async connection => {

const category1 = new Category();
Expand Down

0 comments on commit d744966

Please sign in to comment.