Skip to content

Add reversed actions when using reverse#87

Open
halfmatthalfcat wants to merge 19 commits into
nafg:masterfrom
halfmatthalfcat:master
Open

Add reversed actions when using reverse#87
halfmatthalfcat wants to merge 19 commits into
nafg:masterfrom
halfmatthalfcat:master

Conversation

@halfmatthalfcat

@halfmatthalfcat halfmatthalfcat commented Jun 25, 2020

Copy link
Copy Markdown

Reverses the Action order of operations when using reverse.

Closes #86

@halfmatthalfcat

Copy link
Copy Markdown
Author

Looks like this is breaking a ton of stuff...hmmmm

@halfmatthalfcat

Copy link
Copy Markdown
Author

So it looks like reverse is reversing within an operation (as evident by the tests) but it's not reversing when chaining operations

@nafg

nafg commented Jun 25, 2020

Copy link
Copy Markdown
Owner

Can you confirm that your test fails without the change and passes with it?

@halfmatthalfcat

halfmatthalfcat commented Jun 26, 2020

Copy link
Copy Markdown
Author

@nafg I think I actually identified the root cause as https://github.com/nafg/slick-migration-api/blob/master/src/main/scala/slick/migration/api/Dialect.scala#L184

Sorting by the Action sort value is forcing order regardless of the appended order of the actions.

Removing the .sortBy(_.order) fixed the issue without any further changes. I'm assuming this isn't the best fix though, maybe we can pass a reverse flag to migrate table to actually reverse the sort order and apply less weighted actions first.

@nafg

nafg commented Jun 26, 2020

Copy link
Copy Markdown
Owner

Thanks for investigating this. I had forgotten about sort, but it actually exists for this very purpose. So the solution is actually much simpler. The sort of things like DropForeignKey should be lower than things like DropTable. You should just be able to fiddle with the numbers in object Action. (I'd recommend reordering the lines as well, so that they are in increasing sort order, so that it's easy to see the order. That's how it is now as well, except that the sort and order are wrong as you've discovered.)

P.S. Why is GitHub showing a changed line in build.sbt?

@halfmatthalfcat

Copy link
Copy Markdown
Author

The alignment of all the dependency versions were correct except for that one haha.

@halfmatthalfcat

Copy link
Copy Markdown
Author

@nafg yeah so my proposed solution was to add a reverse to the migrateTable function and if you've reversed the table, it'll reverse the sort of the sql strings https://github.com/nafg/slick-migration-api/pull/87/files#diff-e597e55948ec9df1caeac6f1b01e9040R184

I'm using this locally right now and it works as expected. Does this solution seem good to you?

@coveralls

coveralls commented Jun 26, 2020

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 64.063% when pulling 92ab4f3 on halfmatthalfcat:master into 0f9193c on nafg:master.

@halfmatthalfcat

halfmatthalfcat commented Jun 26, 2020

Copy link
Copy Markdown
Author

Ok @nafg this should be good if you're ok with the solution. It looks like on every db except Postgres, you can't create, add and then remove all columns (there must be at least one). Not sure if that should be noted anywhere but this now acts as expected.

I added a test to that end that passes. I wasn't sure if there was much else you wanted added in the tests beyond this.

@nafg

nafg commented Jun 28, 2020

Copy link
Copy Markdown
Owner

Can you comment on this suggestion?

I had forgotten about sort, but it actually exists for this very purpose. So the solution is actually much simpler. The sort of things like DropForeignKey should be lower than things like DropTable. You should just be able to fiddle with the numbers in object Action. (I'd recommend reordering the lines as well, so that they are in increasing sort order, so that it's easy to see the order. That's how it is now as well, except that the sort and order are wrong as you've discovered.)

@halfmatthalfcat

Copy link
Copy Markdown
Author

@nafg Ah sorry I think I had mis-understood what you were originally talking about. I played around with the weights (sort values) and have verified locally it now works for me. Is this more of what you were thinking?

@nafg

nafg commented Jun 29, 2020 via email

Copy link
Copy Markdown
Owner

@halfmatthalfcat

Copy link
Copy Markdown
Author

It tests that a successful reversal of multiple chained migrations works. All of the other test examples, afaik, only test the reversal of one migration.

Due to db mechanics for the various kinds though, Postgres is the only db that can drop all columns. All the others throw and error stating that you must have at least one column present.

This test ensures that at least Postgres can do a compete reversal, as the others throw and we just continue on. I’m open to removing it though since really the only thing we’re testing is Postgres succeeding.

@nafg

nafg commented Jun 29, 2020 via email

Copy link
Copy Markdown
Owner

@halfmatthalfcat

Copy link
Copy Markdown
Author

We could detect a drop table and then just drop any drop column actions from the actions list...we probably could just drop all other actions since dropping the table is the most final action of all actions. I'm not sure that's the most prudent way to go about it though, seems somewhat brute-forcy.

I'm using Postgres personally, so I'm unaffected by the last column rule.

@nafg

nafg commented Jun 29, 2020 via email

Copy link
Copy Markdown
Owner

@halfmatthalfcat

Copy link
Copy Markdown
Author

Yeah I think that's a fine approach. So you'd detect whether a drop table action has been queued and then essentially remove any drop column actions and proceed as normal.

@nafg

nafg commented Jul 19, 2020

Copy link
Copy Markdown
Owner

Hi, do you plan to implement this?

@halfmatthalfcat

halfmatthalfcat commented Aug 12, 2020

Copy link
Copy Markdown
Author

@nafg I don't have a lot of time right now unfortunately. I found a solution I can live with in my project for now. If I find some time in the future, I'll be able to revisit this if it's not already fixed.

@nafg nafg force-pushed the master branch 7 times, most recently from 24176d9 to 4373aa5 Compare July 12, 2022 06:31
@anovstrup

Copy link
Copy Markdown

@nafg Hi Naftoli, I just ran into this issue as well. If you can help me understand what's still needed to get a solution over the finish line, I'd be happy to work on it.

@nafg

nafg commented Dec 11, 2022

Copy link
Copy Markdown
Owner

@anovstrup would you be interested in pair programming it with me?

IIRC it wouldn't be much work but I have to context-switch back into it

@anovstrup

Copy link
Copy Markdown

Sure! I probably won't be available until late this week (Thursday or Friday).

Sounds like this is what's left to implement, plus any additional testing that's needed?

Yeah I think that's a fine approach. So you'd detect whether a drop table action has been queued and then essentially remove any drop column actions and proceed as normal.

@nafg

nafg commented Dec 12, 2022

Copy link
Copy Markdown
Owner

Sure! I probably won't be available until late this week (Thursday or Friday).

Great, can you message me on Discord?

Sounds like this is what's left to implement, plus any additional testing that's needed?

I would have to refresh my memory ;)

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.

Reverse doesn't reverse order of operations

4 participants