willhlaw / node-firestore-backup-restore

Google Firebase Firestore backup and restore tool
91 stars 24 forks source link

Backup and restore a DocumentReference field from database to another one #9

Closed pmarconi closed 1 year ago

pmarconi commented 6 years ago

I currently get an error when wanting to restore a document that contains a document reference, using the command line

firestore-backup-restore --accountCredentials path/to/account/credentials/app1.json --backupPath /backups/myDatabase --restoreAccountCredentials path/to/restore/credentials/app2.json

In my tests, I get the following error

Error! Restoring to collection /User document Prp9Qpe5iU1Kzm2p44Ag - Error: Reference value app: "app1"
PathElement {
   type: "User"
   name: "Prp9Qpe5iU1Kzm2p44Ag"
}
  in different app partition
Error! Restoring to collection/User/Prp9Qpe5iU1Kzm2p44Ag/info document bWMsubqe8rBhDpu2ik1V - Error: Reference value app: "app1"
PathElement {
   type: "User"
   name: "Prp9Qpe5iU1Kzm2p44Ag"
}
  in different app partition

I think this error happens because we try to save a reference to a document belonging to another database

pobochiigo commented 6 years ago

@willhlaw is the enhancement in progress? it would be great to have it.

willhlaw commented 6 years ago

@VolodymyrPobochii @pmarconi, it was actually fixed in 1.2.1 on Feb 6.

I just confirmed. Document references are properly reconstructed when cloning from one firestore to another.

pobochiigo commented 6 years ago

@willhlaw Great! Thanks so much!

pobochiigo commented 6 years ago

@willhlaw I have just tried it out in versions 1.2.1, 1.3.1 and the error is still there. Any ideas?

Error! Restoring to collection /releases document ylMxpcTV13Tj5JzVbBP6 - Error: 3 INVALID_ARGUMENT: Reference value app: "arccompsdemo" PathElement { type: "versionCode" name: "kWuGZp1knaowVxgKgkxH" } in different app partition

pmarconi commented 6 years ago

hey @VolodymyrPobochii.
Yes, I tried this again with the latest changes in master, and I think you are right, the error still persists

There is a difference when a document is restored directly from the hard drive backup, being more specific, using the command:

firestore-backup-restore --backupPath backups-test --restoreAccountCredentials testing2-cred.json

and when you want to make a backup and restore a document at once, that is, using the command:

firestore-backup-restore --accountCredentials testing-cred.json --backupPath backups-test --restoreAccountCredentials testing2-cred.json

since the restoration in this second case is done trying to restore the document without being parsed before. See at backupCollection function on the index.js, it calls the restoreDocument function and this one try to save the document directly

I think the solution for this is change on the restoreDocument function (index.js) the set parameter like this constructFirestoreDocumentObject(constructDocumentObjectToBackup(document.data()), { firestore: restoreAccountDb }) instead of document.data()

Like this:

    !restoreAccountDb
      ? null
      : restoreAccountDb
          .collection(collectionName)
          .doc(document.id)
          .set(
            constructFirestoreDocumentObject(
              constructDocumentObjectToBackup(document.data()),
              { firestore: restoreAccountDb }
            )
          )

@VolodymyrPobochii If you need to go ahead with this, you can do the backup first, and then restore using separate commands, I think that should work, let me know how that works for you.

I will reopen this issue, and close the another one, to keep the discussion here

@willhlaw feel free to close this issue again

willhlaw commented 6 years ago

I'll accept a PR with an approach outlined above.