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

feat: Add dependency configuraiton for views #8240 #8261

Merged
merged 2 commits into from Oct 26, 2021

Conversation

zaro
Copy link
Contributor

@zaro zaro commented Oct 10, 2021

Description of change

See #8240 for detailed description of the problem.

The PR adds dependsOn option to @view decorator, where dependencies can be listed. Also use these dependencies to order drop/create view correctly when generating migrations

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

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 zaro marked this pull request as ready for review October 10, 2021 13:44
@imnotjames imnotjames added the hacktoberfest-accepted label hacktoberfest label Oct 10, 2021
@pleerock
Copy link
Member

What if we explicitly define order of entities in entities section of the connection options:

entities: [
  ViewA,
  ViewB
]

doesn't it solve the problem?

@zaro
Copy link
Contributor Author

zaro commented Oct 22, 2021

@pleerock No, the order in which entities/views are listed in the configuration options is irrelevant. What defines it currently w/o this PR is actually the import order ( literally the order in which the decorators are called). For a simple cases like with two views it's easy to reorder imports and get almost correct result ( almost because even if the creation order is correct, the drop order is always incorrect as it needs to be the reverse or creation, and currently it's not reversed). But once the number of views grow, it become a game of whack a mole to reorder the imports in such a way that it will produce migration that executes at least the CREATE in the right order and the DROP statements still need to be reordered manually.

Then there is the problem where views depend on each other, we literally have the following

A uses B
B uses C
C uses D
D uses a Entity

Currently changing column on the entity will correctly trigger drop and recreate of D, but not the rest of the views, so the migration always fails because you can't drop D w/o dropping C, B, A first.
The PR solves this also.

@pleerock
Copy link
Member

Okay... Then we might need to change current signature to factory function and design it as follows:

dependsOn: () => [ViewA]

To prevent possible circular reference errors.

@zaro
Copy link
Contributor Author

zaro commented Oct 22, 2021 via email

@pleerock
Copy link
Member

oh, I see you added "string entity name". If so, we might not need a function signature.

//await this.queryRunner.dropView(view);
droppedViews.push(view);
}
const viewDependencyChain = (view: View): View[] => {
Copy link
Member

@pleerock pleerock Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good, beside one small fact that code become a bit complex to review and understand, and it means worse for future maintenance. Can it be refactored to make it more simple and understandable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pleerock I added detailed comments on how dependencies are collected.
Also renamed some variables with obviously poorly chosen names :)

I hope this makes the intention more clear, because as algorithm, it's quite simple and I don't see what much can be changed. Maybe only collecting the dependencies in non recursive manner, but I don't believe it will become more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thank you for your efforts

Rename some variables in viewDependencyChain  in RdbmsSchemaBuilder and add more thorough comments, so its more readable.
@pleerock pleerock merged commit 2c861af into typeorm:master Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted label hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants