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

fix: create typeorm_metatable when running migrations #4956

Merged
merged 14 commits into from Nov 14, 2021
Merged

fix: create typeorm_metatable when running migrations #4956

merged 14 commits into from Nov 14, 2021

Conversation

BitPatty
Copy link
Contributor

Proposed fix for #4923
When generating migrations for views a typeorm_metadata table is created in which the views select statements are stored during the migration. However, the table won't be created during the actual execution of the migration. Thus, running a migration on a database which doesn't have the typeorm_metadata table will eventually fail. To avoid that, the table is created during a migration
if necessary.

@pablosproject
Copy link

Any chances we got this merged any soon? Just ran into this issue and I think is important to have this in the library.

@GSKryachko
Copy link

I also would like to see it merged as soon as possible.

@pleerock
Copy link
Member

pleerock commented Dec 5, 2019

This PR isn't finished. For some reason @BitPatty added metadata table creation code into undoLastMigration method, I'm completely confused why

@BitPatty
Copy link
Contributor Author

BitPatty commented Dec 5, 2019

I'm just as confused on how this happened :/

@michaeldever
Copy link

This would be awesome, hitting my head against this in test for the last little while.

@coyoteecd
Copy link
Contributor

@pleerock can you merge and release this? I've run into this too

@imnotjames imnotjames self-requested a review October 3, 2020 03:02
@imnotjames
Copy link
Contributor

imnotjames commented Oct 9, 2020

Can you fix the conflict? Is it possible to write tests for this?

@pleerock
Copy link
Member

pleerock commented Oct 9, 2020

we don't have tests for cli commands yet.. if someone can contribute and start writing tests for CLI for the first time, this one is a good opportunity. If nobody wants, probably we can merge this one.

@BitPatty
Copy link
Contributor Author

BitPatty commented Oct 9, 2020

@imnotjames fixed the conflict

@imnotjames
Copy link
Contributor

Tests exercising the MigrationExecutor don't need to be using the CLI.

You can run migrations similar to this example:

https://github.com/typeorm/typeorm/blob/2f275816375a58c9ad136d5f02dc3508797152c2/test/github-issues/4697/issue-4697.ts

@BitPatty BitPatty requested a review from pleerock June 12, 2021 20:18
@BitPatty
Copy link
Contributor Author

@imnotjames Can you check the tests?

@imnotjames
Copy link
Contributor

@imnotjames Can you check the tests?

They're currently failing, it looks like?

@BitPatty
Copy link
Contributor Author

BitPatty commented Jun 16, 2021

The CI build had a timeout during service setup, it didn't run the tests => https://app.circleci.com/pipelines/github/typeorm/typeorm/2683/workflows/3d850679-0b7e-4944-a06a-dc09b42608aa/jobs/10722

@pleerock
Copy link
Member

pleerock commented Aug 4, 2021

@AlexMesser can you please review this PR?

@sizzlorox

This comment has been minimized.

@pleerock
Copy link
Member

Okay, I have to review it by myself.

*/
async createMetadataTableIfNecessary(): Promise<void> {
if (this.viewEntityToSyncMetadatas.length > 0) {
this.queryRunner = this.queryRunner || this.connection.createQueryRunner();
Copy link
Member

Choose a reason for hiding this comment

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

|| this.connection.createQueryRunner() this code can be very dangerous since its not clear here where it gets released. Use this.queryRunner if it's available, or pass it to createMetadataTableIfNecessary method from the places where you call this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a side note, with both the current implementation [1]/[2] as well as this PR the migration table is created when generating migrations. Is this intended behavior?

[1]

const sqlInMemory = await connection.driver.createSchemaBuilder().log();

[2]
async log(): Promise<SqlInMemory> {
this.queryRunner = this.connection.createQueryRunner();
try {
// TODO: typeorm_metadata table needs only for Views for now.
// Remove condition or add new conditions if necessary (for CHECK constraints for example).
if (this.viewEntityToSyncMetadatas.length > 0) {
await this.createTypeormMetadataTable();
}

Copy link
Member

Choose a reason for hiding this comment

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

the migration table is created when generating migrations. Is this intended behavior?

Probably it shouldn't be inside log method, since you are creating metadata table in executePendingMigrations, right?

Avoid creating the metatable when you don't intend to modify the database, such as when generating migrations
@TheNoim
Copy link
Contributor

TheNoim commented Nov 13, 2021

I just noticed this could also be relevant for my pr #6469, because it uses the typeorm_metadata table.

@AlexMesser AlexMesser merged commit b2c8168 into typeorm:master Nov 14, 2021
@AlexMesser
Copy link
Collaborator

thank you for contribution!

@issackr
Copy link

issackr commented Nov 23, 2021

@pleerock @BitPatty
Hi
I am getting this error while having my server that run migrations going up
typeorm_metadata table already exists
It looks like this behaviour started around the merging of this fix, do you think it might be connected?

Thanks

@BitPatty
Copy link
Contributor Author

You might have patched a migration manually to explicitly create the typeorm_metadata table. Check your migration files for any CREATE TABLE typeorm_metadata statements.

HeartPattern pushed a commit to HeartPattern/typeorm that referenced this pull request Nov 29, 2021
)

* fix: create typeorm_metatable when running migrations

* remove redundant check

* create typeorm_metadata table in MigrationExecutor

* remove redundant call to createMetadataTableIfNecessary

* move create metadata table logic

* add missing test (typeorm#4956)

* fix migration name

* pass query runner instance to metadata creation

* do not create metadata table in log()
Avoid creating the metatable when you don't intend to modify the database, such as when generating migrations
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this pull request Nov 29, 2021
)

* fix: create typeorm_metatable when running migrations

* remove redundant check

* create typeorm_metadata table in MigrationExecutor

* remove redundant call to createMetadataTableIfNecessary

* move create metadata table logic

* add missing test (typeorm#4956)

* fix migration name

* pass query runner instance to metadata creation

* do not create metadata table in log()
Avoid creating the metatable when you don't intend to modify the database, such as when generating migrations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet