Skip to content
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

iOS Sqlite DB Backup and Restore #1391

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

heyalexchoi
Copy link

@heyalexchoi heyalexchoi commented Sep 12, 2022

Hey, just started using Watermelondb. Great work! I need to be able to backup and restore the db on iOS. Added the capability here. Did my best to follow guidelines and visible patterns. Please let me know if there's anything else I should do to help this get merged!

Related

#1012

@heyalexchoi heyalexchoi changed the title added readFromFile and writeToFile for db backup and restore iOS Sqlite DB Backup and Restore Sep 12, 2022
@alicja-mruk
Copy link

I think that we should add also android part

@heyalexchoi
Copy link
Author

I'm adding this functionality for an app that does not yet have an android component. I'm looking at the native code I've written, and it looks like it is shared between platforms. Is there more needed to make this work for android as well?

@radex
Copy link
Collaborator

radex commented Sep 21, 2022

no, at first glance, your code should work fine cross-platform 🎉

@batuhansahan
Copy link
Contributor

is this pr going to be merged, any update?

@mlecoq
Copy link
Contributor

mlecoq commented Jan 17, 2023

I confirm that it works on android

@heyalexchoi
Copy link
Author

@radex what is the best way to get this merged? aside from resolving conflicts.

@17Amir17
Copy link

Any updates?

@JoL0712
Copy link

JoL0712 commented May 1, 2023

Could we get this merged please?

@smisaacs
Copy link

+1

@KirillPirinen
Copy link

+1 really need this feature

@Stophface
Copy link

@heyalexchoi since this probably does not get merged and your changes are not that much, I am going to implement that in a fork of mine. CAn you give an example how you use this?

  1. How to backup the database
  2. How to read the backup into an existing database

Plus, is there a way to only save certain tables?

@suchoX
Copy link

suchoX commented Feb 26, 2024

Any update on this?

@heyalexchoi
Copy link
Author

@radex is this a feature you would be interested in accepting, if I rebase my branch, bringing it up to date and getting rid of the merge conflicts? Asking because I don't want to do that work unless you're interested in accepting.

@heyalexchoi
Copy link
Author

@heyalexchoi since this probably does not get merged and your changes are not that much, I am going to implement that in a fork of mine. CAn you give an example how you use this?

  1. How to backup the database
  2. How to read the backup into an existing database

Plus, is there a way to only save certain tables?

Backup:
await getDatabase().writeToFile(filePath)
Restore:
await getDatabase().readFromFile(filePath)

This does not support saving certain tables. It only backs up and restores the sqlite db to and from file. It is a pretty efficient operation, probably more so than selecting data to export, so feel free to give it a shot for your use case.

@denissdubinin
Copy link

Can confirm it works, using this for a while <3

@ademarsj
Copy link

Hello @radex, would you mind taking a look at this? It would be a great addition to WatermelonDB. Thank you!

@suchoX
Copy link

suchoX commented Aug 29, 2024

Any one help me on how to create a fork and use it? Looks like this PR wont be merged.

@NBaySellier
Copy link

Is this going to be merged?

@suchoX
Copy link

suchoX commented Sep 8, 2024

Looks like This will not be merged and I do not want to host my own Fork. Hence I wrote my own backup and restore using JSON. I then upload the JSON file to iCloud/Google Drive. Working fine.

The bottom code removes all existing entries when you restore coz it's my requirement. You can choose not to do this. Make sure to handle ID conflicts if necessary.
Also, This doens't consider Migration as I do not have major need for it. It still works with an old backup if your new schema just has a new Table, but existing table changes, I am not sure how it will handle.

  import * as RNFS from '@dr.pogodin/react-native-fs'
  
  export const backupDataToFile = async () => {
    try {
      const allData: Record<string, any[]> = {}
      const collections = database.collections.map
  
      for (const [collectionName, collection] of Object.entries(collections)) {
        const records = await collection.query().fetch()
        allData[collectionName] = records.map(record => record._raw) // Extract raw data
      }
  
      const jsonContent = JSON.stringify(allData, null, 2)
      const filePath = `${RNFS.CachesDirectoryPath}/watermelon_backup.json`
  
      await RNFS.writeFile(filePath, jsonContent, 'utf8')
      console.log('Database successfully backed up to:', filePath)
    } catch (error) {
      console.error('Failed to backup database:', error)
    }
  }
  
  export const restoreDataFromFile = async () => {
    try {
      const filePath = `${RNFS.CachesDirectoryPath}/watermelon_backup.json`
  
      const jsonContent = await RNFS.readFile(filePath, 'utf8')
      const allData = JSON.parse(jsonContent)
  
      await database.write(async () => {
        const collections = database.collections.map
        for (const collection of Object.values(collections)) {
          await collection.query().destroyAllPermanently()
        }
        for (const [collectionName, records] of Object.entries(allData)) {
          const collection = database.collections.get(collectionName)
          for (const record of records) {
            await collection.create(newRecord => {
              Object.assign(newRecord._raw, record) // Set raw data directly
            })
          }
        }
      })
  
      console.log('Database successfully restored from:', filePath)
    } catch (error) {
      console.error('Failed to restore database:', error)
    }
  }

@NBaySellier
Copy link

Sorry to tag @radex , but what could we do to help getting this merged?

@HurrellT
Copy link

Hi! I'm looking forward to this. Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.