Search code examples
iossqlitecore-datarestorewal

Can a Core Data .sqlite backup without a WAL file be restored to operate in WAL mode?


Please be patient with me - I'm going to try to use the correct terminology in this question. I have an iOS app that has allowed the users to create backups of their Core Data backed .sqlite database for several years now. For some reason, way back when, I set the pragma option for journal_mode to "MEMORY", which means that each of the backups that have been made over the last several years do not have a WAL or SHM file. I am rewriting all of my database code, and I know that the current default journal_mode for iOS is "WAL". When I import a backup that was made with journal_mode "MEMORY", I would like to somehow end up with that file operating in WAL mode (I want all new backups to be the same - in WAL mode) but I am not sure if this is even possible. Can a single .sqlite that has never had a WAL or SHM file be forced to start creating them? I am trying to use the NSPersistentStore methods to create backups and to restore data now, and I remember reading that the options used throughout the process (i.e. the journal_mode) need to remain consistent (I will post the link for this if I can find it). I believe, because I do not have a WAL file, that I need to use the "MEMORY" or "DELETE" journal_mode for the import. However, I am not sure if this means that the file will forever have to function without a WAL file, or if there is something I can do to get it operating in WAL mode. I hope this makes some sense - it is a bit confusing to me, but I would appreciate any thoughts on how to approach this issue.


Solution

  • You can change the journal mode of a database at any time, as long as you have exclusive access; just open it, and execute the PRAGMA. (In fact, this is the only method to enable WAL mode.)

    Please note that only WAL mode is persistently stored in the database file; any other journal mode is a property of the connection; when a non-file WAL is opened, it is in DELETE mode by default.