Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created April 10, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/1b3ad1c4b327f49a0d59e6e94cff49b6 to your computer and use it in GitHub Desktop.
Save nolanlawson/1b3ad1c4b327f49a0d59e6e94cff49b6 to your computer and use it in GitHub Desktop.
Migrating from the original SQLite Plugin to SQLite Plugin 2
// Copy a database file from Library/LocalDatabase to Library/NoCloud.
// Note that you will need a Promise shim if you are targeting old versions of iOS/Android.
function migrateFromOriginalSQLitePlugin(dbName) {
if (/Android/.test(navigator.userAgent)) {
// no need to migrate on Android
return Promise.resolve();
}
var localDBDirName = cordova.file.dataDirectory.replace(/NoCloud\/$/, 'LocalDatabase/');
var sourceFileName = localDBDirName + dbName;
var targetDirName = cordova.file.dataDirectory;
return Promise.all([
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(sourceFileName, resolve, reject);
}),
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(targetDirName, resolve, reject);
})
]).then(function (files) {
var sourceFile = files[0];
var targetDir = files[1];
return new Promise(function (resolve, reject) {
targetDir.getFile(dbName, {}, resolve, reject);
}).then(function () {
console.log("file already copied");
}).catch(function () {
console.log("file doesn't exist, copying it");
return new Promise(function (resolve, reject) {
sourceFile.copyTo(targetDir, dbName, resolve, reject);
}).then(function () {
console.log("database file copied");
});
});
});
}
// usage
migrateFromOriginalSQLitePlugin('mydatabase.db').then(function () {
// success! :)
}).catch(function (err) {
// error :()
});
@brandoncorbin
Copy link

Nolan, once again thank you for all your amazing work. I've been trying to come up with a way to migrate from a local Sqlite database (not Sqlite Plugin) to Sqlite Plugin 2? Would this be my best starting point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment