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

Added destroyColumn migration step #1211

Closed
wants to merge 0 commits into from
Closed

Conversation

bmatasar
Copy link

I don't know how to add info about dropped columns in getSyncChanges

@radex
Copy link
Collaborator

radex commented Nov 14, 2021

Nice! Thank you for your contribution! Some quick thoughts:

  • getSyncChanges provides summary of migration changes via sync to backend. The purpose of that is to allow server to update client's database with tables and columns it could not previously sync as client had no way of expressing them. There's no reason to notify backend about removed columns or tables, since there's nothing extra for server to send to client to align the two schemas.
  • there's some formatting issues - try running yarn prettier on the codebase to fix

Copy link
Collaborator

@radex radex left a comment

Choose a reason for hiding this comment

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

Okay, I reviewed it, and have a few requests:

  • Please update docs-master/Advanced/Migrations.md
  • add an integration test to adapters/tests/commonTEsts.js - you'll notice that there are some migration tests there that actually test if new columns/tables are usable after the addition. You could modify them or add a new test that tests if destroyed columns really were destroyed
  • please add a note to changelog-unreleased

Otherwise, it all seems reasonable, thanks for your work!

@@ -181,6 +181,10 @@ describe('migration step functions', () => {
'type',
)
})
it('throws if destroyColumn() is malformed', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

pls update one of the tests above to test for the format of what destroyColumn returns

)
case 'destroy_column':
return addedColumns.filter(
({ table, name }) => step.table !== table || step.column !== name,
Copy link
Collaborator

Choose a reason for hiding this comment

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

this doesn't seem correct to me, and there are no tests for this. can you explain?

Copy link
Author

Choose a reason for hiding this comment

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

I had wrong understanding of getSyncChanges. I still have.

  • If there is an addColumns for 'table' and 'column' in an earlier step, a destroyColumn in a later step should cancel it out. That's why I unnested them and kept them as a single array.

@bmatasar
Copy link
Author

It looks like ALTER TABLE DROP COLUMN is not supported although the docs say it does. However, ALTER TABLE RENAME COLUMN is supported. I am stuck right now :)

@radex
Copy link
Collaborator

radex commented Nov 15, 2021

It looks like ALTER TABLE DROP COLUMN is not supported although the docs say it does

on which platform are you testing that?

@bmatasar
Copy link
Author

I have just executed yarn ci:check and it fails on my mac.

@radex
Copy link
Collaborator

radex commented Nov 15, 2021

oh no https://sqlite.org/changes.html this was only added in 3.35.0 :(

@radex
Copy link
Collaborator

radex commented Nov 15, 2021

you could go with something like this : https://stackoverflow.com/questions/5938048/delete-column-from-sqlite-table but you probably have to drop and recreate indices for it to work

@bmatasar
Copy link
Author

That's what I was afraid of. I would need to pass also the whole table description in order to do that. It makes the whole migration step really ugly.
What sqlite3 version is watermelondb supporting?

@radex
Copy link
Collaborator

radex commented Nov 15, 2021

@bmatasar Unfortunately, it's complicated. On iOS and Android we're using the system built-in version of sqlite, so we can't rely on newer sqlite features. On Android JSI we're bundling sqlite 3.36 https://github.com/Nozbe/WatermelonDB/blob/master/package.json#L48 because it's impossible to reuse system version of sqlite. We're also using better-sqlite3 for Node support of SQLite (this is a rare feature probably used by just two people + 🍉's testing setup), I'm not sure which version of sqlite3 it's building, you need to check.

I would prefer not to bundle sqlite3 on iOS due to codesize/performance concerns

@bmatasar
Copy link
Author

The problem is that in order to apply the solution with the duplication of the table I need the whole schema for the table. The statements are generated just from the migrations steps, so it will look more like a keepColumns step listing only the remaining columns.
I guess I need more time to think about it.

@radex
Copy link
Collaborator

radex commented Nov 15, 2021

yeah that's fair. I forgot about this, but this difficulty is why I never implemented dropping tables and columns in migrations - it shouldn't be very hard but it's not trivial

@bmatasar
Copy link
Author

Let's close the PR for now.

@bmatasar bmatasar closed this Nov 15, 2021
@radex
Copy link
Collaborator

radex commented Nov 16, 2021

I'm gonna re-open so the work done already doesn't get lost

@radex radex reopened this Nov 16, 2021
@radex
Copy link
Collaborator

radex commented Nov 16, 2021

@bmatasar read about the magic sqlite tables, I think you can get info about indices with pure SQL

@bmatasar
Copy link
Author

I looked into .schema and sqlite_schema but the problem is that during migration I can only prepare a set of commands to be issued. Basically I would have to write something like a little script and sqlite does not really have support for such a thing. Probably doing the migration with an unsafeSql step.

@KrisLau
Copy link
Contributor

KrisLau commented Feb 1, 2023

Any plans to add this in? Seems like it would be a pretty useful migration step

@vonovak
Copy link

vonovak commented Sep 22, 2023

just passing by, hope you don't mind the notification
I wanted to say that this should be good to integrate soon. ios 15 ships sqlite version 3.36.0

now with ios 17 being out, I believe requiring ios 15 won't be an issue 👍

@bmatasar
Copy link
Author

I would rather start a new PR based on #1654

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.

4 participants