Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ViewEntity dependency configuration #8240

Closed
zaro opened this issue Oct 3, 2021 · 3 comments
Closed

Support ViewEntity dependency configuration #8240

zaro opened this issue Oct 3, 2021 · 3 comments

Comments

@zaro
Copy link
Contributor

zaro commented Oct 3, 2021

Feature Description

Currently TypeORM doesn't support any way of specifying dependencies between views, which often leads to migrations being able to execute only after manual editing of the generated code.

The Problem

Suppose we have ViewA and ViewB. ViewB is using ViewA inside it's definition, so the normal order is to create first ViewA and then ViewB. Now if there is a change in ViewA, we can't execute it w/o first dropping ViewB, adjusting VIewA and then creating ViewB back. In typeorm if we edit only ViewA, the resulting migration will generate only changes for ViewA , and the migration will fail to execute because ViewB still holds a reference to ViewA and the DROP VIEW statement will fail. A workaround this will be to make also some trivial change to ViewB so it also ends up being updated in the migration. Unfortunately even this is not sufficient as the order in which these are generated is based on import order or something like this, so the DROP/CREATE statements often end up in the wrong order , e.g dropping ViewA before ViewB, and a manual edit of the migration file is needed.
With more views and dependencies the process can get quite cumbersome and error prone.

The Solution

Introduce new config for the VIewEntity decorator where we can explicitly list that a view is depending on another view and then generate the migration in the correct order based on this configuration.

Example

////////////////////////////////////////////////////////////
// viewA.ts
@ViewEntity({
    expression: `
      select * from tableA    
    `
})

export class ViewA{
//...
}
////////////////////////////////////////////////////////////
// viewB.ts

import {ViewA} from './viewA.ts';

@ViewEntity({
    expression: `
      select * from ViewA  ​ where column > 5
   `,
   dependsOn: [ViewA]
})
export class ViewB{
//...
}

Considered Alternatives

None that I could think of

Relevant Database Driver(s)

Not connected to specific drivers, affects all SQL databases that support views.

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
zaro pushed a commit to zaro/typeorm that referenced this issue Oct 10, 2021
Add dependsOn option to @view decorator, where dependencies can be listed. Also use these dependencies to order draop/create view correctly when generating migrations
zaro pushed a commit to zaro/typeorm that referenced this issue Oct 10, 2021
Add dependsOn option to @view decorator, where dependencies can be listed. Also use these dependencies to order draop/create view correctly when generating migrations
zaro pushed a commit to zaro/typeorm that referenced this issue Oct 10, 2021
Add dependsOn option to @view decorator, where dependencies can be listed. Also use these dependencies to order draop/create view correctly when generating migrations
pleerock pushed a commit that referenced this issue Oct 26, 2021
* feat: Add dependency configuraiton for views #8240

Add dependsOn option to @view decorator, where dependencies can be listed. Also use these dependencies to order draop/create view correctly when generating migrations

* fix: Make dropping views dependencies more readable

Rename some variables in viewDependencyChain  in RdbmsSchemaBuilder and add more thorough comments, so its more readable.

Co-authored-by: Svetlozar <ext-svetlozar@getitdone.co>
@mwanago
Copy link

mwanago commented Dec 5, 2021

@zaro Can this issue be closed, since you've merged the PR?
Cool work! 👍

@zaro
Copy link
Contributor Author

zaro commented Dec 7, 2021

@mwanago Yes, thanks for reminding me.

@zaro zaro closed this as completed Dec 7, 2021
@yanqic
Copy link

yanqic commented Jan 7, 2022

#8511 test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants