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
Conversation
Any chances we got this merged any soon? Just ran into this issue and I think is important to have this in the library. |
I also would like to see it merged as soon as possible. |
This PR isn't finished. For some reason @BitPatty added metadata table creation code into |
I'm just as confused on how this happened :/ |
This would be awesome, hitting my head against this in test for the last little while. |
@pleerock can you merge and release this? I've run into this too |
Can you fix the conflict? Is it possible to write tests for this? |
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. |
@imnotjames fixed the conflict |
Tests exercising the You can run migrations similar to this example: |
@imnotjames Can you check the tests? |
They're currently failing, it looks like? |
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 |
@AlexMesser can you please review this PR? |
This comment has been minimized.
This comment has been minimized.
Okay, I have to review it by myself. |
*/ | ||
async createMetadataTableIfNecessary(): Promise<void> { | ||
if (this.viewEntityToSyncMetadatas.length > 0) { | ||
this.queryRunner = this.queryRunner || this.connection.createQueryRunner(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
typeorm/src/schema-builder/RdbmsSchemaBuilder.ts
Lines 116 to 125 in a92120a
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(); | |
} | |
There was a problem hiding this comment.
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
I just noticed this could also be relevant for my pr #6469, because it uses the |
thank you for contribution! |
You might have patched a migration manually to explicitly create the typeorm_metadata table. Check your migration files for any |
) * 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
) * 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
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 thetypeorm_metadata
table will eventually fail. To avoid that, the table is created during a migrationif necessary.