Skip to content

Canonical ID Schema Migration #1182

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

Merged
merged 1 commit into from
Jan 31, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

No description provided.

@googlebot googlebot added the cla: yes Override cla label Jan 30, 2020
@codecov
Copy link

codecov bot commented Jan 30, 2020

Codecov Report

❗ No coverage uploaded for pull request base (mrschmidt/rewritefieldvalue@9354294). Click here to learn what that means.
The diff coverage is 82.6%.

Flag Coverage Δ Complexity Δ
#FirebaseFirestore 62.27% <82.6%> (?) 2352 <4> (?)
#FirebaseFirestore_Ktx 41.17% <ø> (?) 0 <ø> (?)
Impacted Files Coverage Δ Complexity Δ
...le/firebase/firestore/local/SQLitePersistence.java 79% <80%> (ø) 24 <1> (?)
.../google/firebase/firestore/local/SQLiteSchema.java 86.66% <83.33%> (ø) 69 <3> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9354294...062a6c1. Read the comment docs.

@@ -530,6 +537,26 @@ private boolean tableContainsColumn(String table, String column) {
return columns;
}

private void rewriteCanonicalIds() {
new SQLitePersistence.Query(db, "SELECT target_id, target_proto FROM targets")
Copy link
Contributor

Choose a reason for hiding this comment

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

Dumb question: this file doesn't appear to employ a transaction anywhere. Doesn't db.execSQL auto-commit if you don't? It seems like it would be bad if this partially committed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TargetData targetData = serializer.decodeTargetData(targetProto);
String updatedCanonicalId = targetData.getTarget().getCanonicalId();
db.execSQL(
"UPDATE targets SET canonical_id = ? WHERE target_id = ?",
Copy link
Contributor

Choose a reason for hiding this comment

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

This migration can't be rolled back and has the potential to cause problems because target_id must be guaranteed unique (it's the primary key of the table).

If a user does roll back, will this actually cause a problem with duplicate target ids? I think probably not because the next target id is in target_globals and we're not changing that here, right?

One alternative that makes this slightly more rollback friendly would be to put the new target id in a new column and create an additional index on that column. I can't say I'm super excited about this prospect and likely would prefer to just document that this release includes a migration that can't be rolled-back from.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. The fix for this is actually pretty straightforward as we can always rewrite canonical IDs with the current format. The target IDs don't change and as as long the format in the schema matches the expected format at the current client version we should be safe. I modified the schema converter to support downgrades and updated the code to run this particular migration on both up- and downgrades.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm. I just realized that the new version of the schema converter is pretty dumb. It assumes that my shiny new code is shipped to old SDKs. What do you think about submitting the PR in its original form and letting LRU GC delete stale QueryData if the user chooses to downgrade?

Copy link
Contributor

Choose a reason for hiding this comment

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

Letting LRU GC do its thing SGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted the commit that pretended to add support for downgrades.

Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Jan 31, 2020
@schmidt-sebastian schmidt-sebastian merged commit d644fd6 into mrschmidt/rewritefieldvalue Jan 31, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/migration branch February 7, 2020 00:22
@firebase firebase locked and limited conversation to collaborators Mar 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants