Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration Firestore replace field

Let's say that I have a document like this:

Document {
    tags: list<Int> {0,1,2}
}

I want to change it to this:

Document {
    tags: list<String> {SEASON, TRAINING, TOURNAMENT}
}

I have active users which uses the list of ints, How do I create a migration in Firestore for this problem?

One solution I have in mind is to make 2 migrations:

  1. For creating a new tags called tagsStrings.
  2. For deleting all users who still have tags.

But can I make it in 1?

I was unable to find documentation for this, on https://cloud.google.com/firestore/docs/manage-data/move-data

Thanks in advance

like image 993
J.norberg Avatar asked Oct 18 '25 22:10

J.norberg


1 Answers

Firestore does not have a "migration" like SQL databases. The only way to modify data in existing documents, in bulk, is to:

  1. Query for the documents to change
  2. Iterate the results
  3. Update each document with new values

Each one of these tasks should be straightforward.

You might also consider lazily updating each document as each are individually read during the normal course of your app's usage. So, if your app reads a document in the old format, immediately update it to the new format.

It's often helpful to have a dedicated field in each document to indicate which version of data that's contained within. So, initially set v=1 in each document, assign v=2 to mean that the document has strings instead of numbers for tags, then use that number to determine which documents have yet to be migrated.

like image 198
Doug Stevenson Avatar answered Oct 21 '25 11:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!