Skip to content

Commit

Permalink
fix: synchronizing View with schema broken for oracle (#9602)
Browse files Browse the repository at this point in the history
* fix view+schema broken for oracle

* apply prettier

* fixed missing schema in loadViews();
added test (with .only)

* removed .only

Co-authored-by: James Jurach <James.Jurach@apiture.com>
Co-authored-by: Alex Messer <dmzt08@gmail.com>
  • Loading branch information
3 people committed Dec 29, 2022
1 parent 67973b4 commit 18b659d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2242,17 +2242,25 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
const currentDatabase = await this.getCurrentDatabase()
const currentSchema = await this.getCurrentSchema()

const viewNamesString = viewNames
.map((name) => "'" + name + "'")
.join(", ")
const viewsCondition = viewNames
.map((viewName) => this.driver.parseTableName(viewName))
.map(({ schema, tableName }) => {
if (!schema) {
schema = this.driver.options.schema || currentSchema
}

return `("T"."schema" = '${schema}' AND "T"."name" = '${tableName}')`
})
.join(" OR ")

let query =
`SELECT "T".* FROM ${this.escapePath(
this.getTypeormMetadataTableName(),
)} "T" ` +
`INNER JOIN "USER_OBJECTS" "O" ON "O"."OBJECT_NAME" = "T"."name" AND "O"."OBJECT_TYPE" IN ( 'MATERIALIZED VIEW', 'VIEW' ) ` +
`WHERE "T"."type" IN ( '${MetadataTableType.MATERIALIZED_VIEW}', '${MetadataTableType.VIEW}' )`
if (viewNamesString.length > 0)
query += ` AND "T"."name" IN (${viewNamesString})`
`WHERE "T"."type" IN ('${MetadataTableType.MATERIALIZED_VIEW}', '${MetadataTableType.VIEW}')`
if (viewsCondition.length > 0) query += ` AND ${viewsCondition}`

const dbViews = await this.query(query)
return dbViews.map((dbView: any) => {
const parsedName = this.driver.parseTableName(dbView["name"])
Expand Down Expand Up @@ -2848,9 +2856,11 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
const type = view.materialized
? MetadataTableType.MATERIALIZED_VIEW
: MetadataTableType.VIEW
const { schema, tableName } = this.driver.parseTableName(view)
return this.insertTypeormMetadataSql({
type: type,
name: view.name,
name: tableName,
schema: schema,
value: expression,
})
}
Expand Down
14 changes: 14 additions & 0 deletions test/github-issues/9601/entity/Foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "../../../../src"

@Entity({ name: "foo", schema: "SYSTEM" })
export class Foo {
@PrimaryGeneratedColumn({ name: "id" })
id: number

@UpdateDateColumn({ name: "updated_at" })
updatedAt: Date
}
13 changes: 13 additions & 0 deletions test/github-issues/9601/entity/FooView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DataSource, ViewColumn, ViewEntity } from "../../../../src"
import { Foo } from "./Foo"

@ViewEntity({
name: "foo_view",
schema: "SYSTEM",
expression: (connection: DataSource) =>
connection.createQueryBuilder(Foo, "foo").select(`foo.updatedAt`),
})
export class FooView {
@ViewColumn()
updatedAt: Date
}
43 changes: 43 additions & 0 deletions test/github-issues/9601/issue-9601.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import "reflect-metadata"
import { DataSource } from "../../../src/index"
import {
closeTestingConnections,
createTestingConnections,
} from "../../utils/test-utils"

describe("github issues > #9601 view+schema+synchronize broken for oracle", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: false,
dropSchema: true,
enabledDrivers: ["oracle"],
})),
)
after(() => closeTestingConnections(connections))

it("should recognize model changes", () =>
Promise.all(
connections.map(async (connection) => {
const sqlInMemory = await connection.driver
.createSchemaBuilder()
.log()
sqlInMemory.upQueries.length.should.be.greaterThan(0)
sqlInMemory.downQueries.length.should.be.greaterThan(0)
}),
))

it("should not generate queries when no model changes", () =>
Promise.all(
connections.map(async (connection) => {
await connection.driver.createSchemaBuilder().build()
const sqlInMemory = await connection.driver
.createSchemaBuilder()
.log()
sqlInMemory.upQueries.length.should.be.equal(0)
sqlInMemory.downQueries.length.should.be.equal(0)
}),
))
})

0 comments on commit 18b659d

Please sign in to comment.