Created
April 10, 2016 22:01
-
-
Save nolanlawson/1b3ad1c4b327f49a0d59e6e94cff49b6 to your computer and use it in GitHub Desktop.
Migrating from the original SQLite Plugin to SQLite Plugin 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 :() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?