Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing firebase data model (while multiple app versions are in production)

What's the best way to change the Firebase data model while you have multiple versions of your iOS app in production?

Since there's no 'application server' layer in the middle any changes in the database model could break older versions of the app.

Performance Related Example of the problem:

In version 1.0 I was naively keeping everything related to a post under '/posts/'. Now in version 2.0 I want to take Firebase's recommendation and add a '/user-post' endpoint to quickly list all posts for a given user.

People using version 1.0 of the iOS app are not writing any data to '/user-posts' since that endpoint didn't used to exist. People using version 2.0 therefore don't see any posts created by people using the old version of the app.

In theory I could create a server somewhere that listens for changes on '/post/' and adds them to '/user-posts' as well. That seems hard to maintain over time though if you have a lot of different versions of your app.

New Feature Example of the problem:

Lets say in version 1.0 of your mobile app you write new blog posts to '/posts/'. Now in version 2.0 of your app you introduce a Teams feature and all posts need to be in '/team/team-id/posts'.

People who haven't upgraded to version 2.0 will still be writing to '/posts'. Those posts won't be visible to people using version 2.0 who are reading from '/team/team-id/posts'.

I realize you could keep both endpoints simultaneously (and index /posts based on team ID) but over time this seems hard to maintain.

Traditional solutions:

If I were using something like Django or Express I'd do a database migration and then update the server-side endpoints for creating blogposts.

That would make changes in the database from the clients. I could in theory add an application-server tier to my architecture with Firebase, but that doesn't seem like it's recommended: https://firebase.googleblog.com/2013/03/where-does-firebase-fit-in-your-app.html

like image 408
Krishan Gupta Avatar asked Oct 31 '16 19:10

Krishan Gupta


People also ask

Can two apps use same Firebase database?

Yes, You can use the same firebase database in more than one android application as below: In the Project Overview section of Firebase Console add an android application. For adding this application first you need to give that package name. Download config file in JSON format.

Which method will write or replace data on a specified path in Firebase?

The set method will write or replace data on a specified path.

Can I use firestore and Realtime Database at the same time?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.


1 Answers

I would suggest you use Firebase Remote Config to show an alert via UIAlertController or different screen if an update is available. You could force the user to update to the current version and you don't have problems later because no posts with the old code can be created.

To answer your question: I would develop a different app, add it to the same Firebase project and then let this app convert all old data to the new data model. So you would do this one time after releasing the new version and the old user data is converted to the new data model and everything works smoothly. You could also have a property like databaseVersion for every object.

To prevent future problems you could have a general property named app-version in your Firebase Realtime Database. Before every post the app checks if there is a newer version. If not the user can add the post but if there is a newer version you could show an message/alert via UIAlertController

like image 97
mcd Avatar answered Sep 30 '22 05:09

mcd