-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Bug] Upgrade from version 0.22.3 to 0.23-beta2 failed database migration #2074
Comments
I think I have fixed this in #2076, do you have the ability to test it? It is some tech debts from the early days of non structured migrations that keep biting us. |
In some sense the change in #2076 works in that migration does not fail. It does mean that there is now a difference in the structure of the |
I tried as an alternate fix the following code vs only running automigrate if the table does not exist:
and it solved this problem, and did not delete my existing routes (other than the ones that were linked to nodes that did not exist), and I ended up with the correct foreign keys created. |
apologise, when I posted my reply to this PR I thought I was posting on #2063, so indeed, this has not been fixed, my apologies. Too late on a friday... Thank you for investigating, I'll take this and amend the PR. |
This fix breaks #2063, so I will have to investigate. The whole initial migration is incredibly fragile and horrible, but untangling it is next to impossible considering we dont have databases to verify with. Do you have an unupgraded 0.22 database you could email me that I can test with? |
Yes, I have a development one that I have a backup of. I'll update it to remove any sensitive information and send it over today. FWIW, I'm having trouble seeing how the AutoMigrate command would drop rows as described in #2063 - (https://github.com/go-gorm/gorm/blob/v1.25.10/migrator/migrator.go#L121) doesn't seem to have any logic that would drop rows |
Could you by any chance create the dev one as sqlite? it would make it infinitely easier to make it part of a unit test. And it is our recommended database. I also do not understand why its dropped, but I'm gonna pay back the missing techdebt by adding tests for migrations, if not only to make sure we dont regress. |
I've converted it to SQLite and made this integration test: I am not sure if it ended up correctly, but at least now I got two failing tests. I'll try to continue as I have time, but please feel free to have a look. |
@mpoindexter I ended up with a combination, now the migration will use your code to clean up, and then only run the automigrate conditionally. The foreign keys will not be "created" retroactively, and I am not sure if that is a common thing for the db engine to sort out. For now, I wont spent more time on this as its more of a inconvenience and the code does not rely on fk or cascading deletes so its a minor issue. |
@kradalby I think there is still a problem with how it has been done. I spent a while tracing down why the
The implication of this, is that with the change done in #2076 the routes rows are not dropped for existing databases, since the automigration is not done that actually creates the constraints with the ON DELETE CASCADE. For new databases, the constraint will still be created, and the next time you drop a column from the There is a way to deal with this in sqlite: foreign key processing can be turned off temporarily using a pragma and then turned back on. The GORM migrator does this for alter column (https://github.com/glebarez/sqlite/blob/master/migrator.go#L79), but not for drop column/create constraint/drop constraint. I am not sure why that is and it seems like a bug. sqlite provides guidance on how to perform these migrations here: https://www.sqlite.org/lang_altertable.html - see section 7. Based on that, I would propose that the right way to solve the problem is to:
I have tested this locally, and both test cases you added pass with my changes. Let me know if you would like me to submit a PR for this. I think following this approach will make life a lot easier in the future since you won't have to worry about two different upgrade paths (from a DB that has the constraints created on |
Is this a support request?
Is there an existing issue for this?
Current Behavior
Upgrading from 0.22.3 to 0.23-beta2 failed on database migration with this error:
Expected Behavior
Migration completes successfully
Steps To Reproduce
Have an existing database where there are rows in the
routes
table that reference nodes that no longer exist in thenodes
table. Not sure how it happened, but seems like I had a number of these, seems like older versions of the code could cause this to happen.Running
delete from routes where node_id not in (select id from nodes);
and then attempting to upgrade again solved the issue, but ideally this would be done as part of the migration scripts.Environment
Runtime environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: