28

Our firestore DB based model evolves naturally. Now we would like to update all existing documents to the new (implicit) schema.

Are there any tools supporting that or what are the best practices. I would love to have a concept comparable to the rails ActiveRecord migrations.

Mutual Exception
  • 1,240
  • 12
  • 27

2 Answers2

34

I couldn't find a firestore schema migration tool, so I wrote fireway. It's currently very simple (it doesn't support reversing a migration), but it's been enough for my use case.

Here's an example migration script:

// migrations/v0.0.1__example.js

module.exports.migrate = async ({firestore}) => {
    await firestore.collection('name').add({key: 'value'});
};

Then run fireway migrate to migrate your default project.

kevlened
  • 10,846
  • 4
  • 23
  • 17
10

Currently, for Firestore, you will have to write your own code to update all existing documents to the new (implicit) schema. I read a few weeks ago in a post, that Firestore team is working on making this easier in the future.

If your new schema require some changes in your entire database, you can also consider using Firestore import / export system, that allows you to dump your data into a GCS bucket. It is not in a JSON format as you probably expected but it is in a similar format as Cloud Datastore uses, so I think will help you solve this problem.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is there everything alright, can I help you with other informations? – Alex Mamo Nov 28 '18 at 09:41
  • It would be helpful if you could provide a link to the fitebase post you mentioned. – Mutual Exception Nov 29 '18 at 06:27
  • Also, since I am sure I am not the only one with this question, I hope someone who already solved this issue could provide some insight. For those reasons I didn't accept your answer. – Mutual Exception Nov 29 '18 at 06:29
  • [This](https://stackoverflow.com/questions/49164571/firebase-node-react-how-to-copy-files-from-one-firebase-project-to-another?rq=1) is the post. At that time even the import/export feature did not exist. Is about copying records but same rules apply in case o adding properties. – Alex Mamo Nov 29 '18 at 11:09
  • 1
    I had a similar issue when I needed to add a new property to a document named `available` of type boolean to all documents in a collection. So I solved this in the following way. I created a new version of the app and once a user opened the app, it addes this new property with the default value of true. In 3 weeks almost 85% of users had this new option available. So that's why I have answered your question because I has the same issue and I solved it this way. But a option like rails ActiveRecord currently does not exist. Hope you reconsider and accept my answer. – Alex Mamo Nov 29 '18 at 11:12