an-rahulpandey / cordova-plugin-dbcopy

Copy SQLite Database from www folder to default app database location
Apache License 2.0
89 stars 47 forks source link

remove is not working #34

Closed arkadyzalko closed 7 years ago

arkadyzalko commented 7 years ago

Hi,

I'm trying to replace database from ios app old version. Success function is called when I call sqlDB.remove but when I try to use sqlDB.copy to copy the new database file I receive:

016-10-25 08:47:11.813452 MyApp[592:107663] ERROR: [Sync]: Database copy error! {"message":"Error Domain=NSCocoaErrorDomain Code=516 \"“app.db” couldn’t be copied to “LocalDatabase” because an item with the same name already exists.\" UserInfo={NSSourceFilePathErrorKey=/var/containers/Bundle/Application/725E-4A7E-A8A4-0F7ED7930D34/MyApp.app/www/app.db, NSUserStringVariant=(\n    Copy\n), NSDestinationFilePath=/var/mobile/Containers/Data/Application/5298-4E04-9CAB-C44307FA7287/Library/LocalDatabase/app.db, NSFilePath=/var/containers/Bundle/Application/725E-4A7E-A8A4-0F7ED7930D34/MyApp.app/www/app.db, NSUnderlyingError=0x17404c570 {Error Domain=NSPOSIXErrorDomain Code=17 \"File exists\"}}","code":516}

My code:

var dbName = 'app.db',
      dbVersionKey = 'db:version',
      dbVersion = '1.0',
      dbCurrentVersion = $window.localStorage.getItem(dbVersionKey) || null;
if((ionic.Platform.isIOS() || ionic.Platform.isAndroid())) {
    if($window.plugins && $window.plugins.sqlDB) {
        var dbLocation = ionic.Platform.isIOS()? 2: 0;
        $window.plugins.sqlDB.copy(dbName, dbLocation,
            function copySuccess(){
                console.debug(': Database has been successfully copied.');
                $window.localStorage.setItem(dbVersionKey, dbVersion);
                deferred.resolve();
            }, function copyError() {
                if(dbCurrentVersion != dbVersion) {
                    console.debug(': Removing database...', dbCurrentVersion);
                    $window.plugins.sqlDB.remove(dbName, dbLocation,
                        function removedSuccess() {
                            console.debug('Database has been successfully deleted.');
                            $window.plugins.sqlDB.copy(dbName, dbLocation,
                                function copySuccess(){
                                    console.debug('Database has been successfully copied.');
                                    $window.localStorage.setItem(dbVersionKey, dbVersion);
                                    deferred.resolve();
                                }, function copyError(err) {
                                    console.error('Database copy error!', err);
                                    deferred.reject();
                                });
                        }, function removedError(err) {
                            console.error('Database remove error!', err);
                            deferred.reject();
                        });
                } else {
                    console.debug('Database file is already updated.');
                    deferred.resolve();
                }
            });
    } else {
        console.error('Missing sqlDB plugin.');
        deferred.reject();
    }
}
arkadyzalko commented 7 years ago

Sorry... my bad.

It's because sqlitePlugin.openDatabase is executing between remove and copy in another place of my project and SQLitePlugin creates a blank database.

I'm closing this issue

Thanks a lot!