ForerunnerDB icon indicating copy to clipboard operation
ForerunnerDB copied to clipboard

DB.drop() is not actually dropping DB

Open htqbuu opened this issue 9 years ago • 16 comments

Hi Irrelon,

We control stale data by date. So everytime user access our web app, we check the current date to drop DB and then create a new fresh DB.

if (current_date != localStorage.get("dbCreatedDate"){
  var oldDB = FDB.db("DBName");
  oldDB.on("drop", function(){
            DB.save("dbVer", Config.database.version);
            DB.save("dbCreatedDate", current_date);
        })
  FDB.db("DBName").drop();
}
var db = FDB.db("DBName"); //create new fresh one ??

When we use db to load collections, we still get the old data of collection. Could you advice us

Thanks, Karma

htqbuu avatar Sep 20 '16 09:09 htqbuu

On github you can denote an area of code by wrapping it in three backticks ```

If you are writing js code you can format the code with three backticks and js ```js

Remember to add three backticks on a new line at the end of your code too :)

To answer your question, I believe the docs on the main page in the section: https://github.com/Irrelon/ForerunnerDB#dropping-and-persistent-storage are incorrect. You should pass true to db.drop so that it tells the db to specifically delete persistent data:

db.drop(true);

Could you try that? There are unit tests covering this exact scenario and they are passing so I'm confident it should work. If it works I will update the documentation to correctly include a boolean flag when dropping.

Irrelon avatar Sep 20 '16 09:09 Irrelon

Thanks for your quick response. I tried db.drop(true) but when I check on Developer Tools of chrome, I can see the data in indexedDB. May it be caused by other places referring to FDB ?

htqbuu avatar Sep 20 '16 10:09 htqbuu

Just checking you refreshed the indexedDB data (bottom left of chrome tools) after the drop?

screen shot 2016-09-20 at 11 45 46

I'm guessing you did but it doesn't refresh in realtime so worth checking before I go down the rabbit hole :)

Irrelon avatar Sep 20 '16 10:09 Irrelon

Hi Irrelon,

Yes I refreshed the indexedDB data right after dropping DB. But I tried another way. After dropping DB, I tried to load collection again and I can found data in that collection:

db = FDB.db("DBName");
    db.collection("WeeklyFollowUp").load(function(err, tableStats, metaStats){
                    $log.debug("loaded weekkkkk",tableStats);
    });

htqbuu avatar Sep 20 '16 11:09 htqbuu

Hmmm. What version of ForerunnerDB are you using? db.version()

Irrelon avatar Sep 20 '16 11:09 Irrelon

I am using Version 1.3.867.

For your information, I am using ForerunnerDB add-on for Chrome. May it cause the issue ?

htqbuu avatar Sep 20 '16 11:09 htqbuu

The add-on won't cause an issue, I have it too :)

If you look at the unit test that covers this functionality maybe you can spot something that is different?

Here are the lines that deal with checking a dropped database no longer contains data after a .load() call: https://github.com/Irrelon/ForerunnerDB/blob/master/js/unitTests/tests/testsPersist.js#L229-L281

Irrelon avatar Sep 20 '16 11:09 Irrelon

I'm also running some more manual tests to see if I can identify a case where it doesn't work...

Irrelon avatar Sep 20 '16 11:09 Irrelon

I found the difference. The unit test for this function is the intermediate logic. That means DB starts with some collections, persist then drop DB. While my case is: refreshing the webapp, FDB starts from the beginning and drop DB.

I will investigate more and let you know

Thanks

htqbuu avatar Sep 20 '16 11:09 htqbuu

Ahh that is interesting. I will also investigate and see if I can reproduce the error based on what you've said.

Irrelon avatar Sep 20 '16 11:09 Irrelon

Just put together this test and ran in console:

var fdb = new ForerunnerDB(),
    db = fdb.db('test');

db.collection('test');

db.load(function () {
    console.log('Loaded', arguments);

    console.log('Current data', db.collection('test').find());
    db.collection('test').insert({_id: 0, test: true});
    console.log('Current data', db.collection('test').find());
    db.save(function () {
        console.log('Saved', arguments);
        db.drop(true, function () {
            console.log('Dropped');
            console.log('Current data', db.collection('test').find());

            // Generate a new database instance
            db = fdb.db('test');
        });
    });
});

On refresh the data has gone from indexeddb. I am using the absolute latest build though.

Irrelon avatar Sep 20 '16 13:09 Irrelon

I created test.html with code below:

<script type="text/javascript" src="fdb-core+persist.js"></script>
<script>
var fdb = new ForerunnerDB(),
    db = fdb.db('test');

db.collection('test');

db.load(function () {
    console.log('Loaded', arguments);

    console.log('Current data', db.collection('test').find());
    db.collection('test').insert({_id: 0, test: true});
    console.log('Current data', db.collection('test').find());
    db.save(function () {
        console.log('Saved', arguments);
        db.drop(true, function () {
            console.log('Dropped');
            console.log('Current data', db.collection('test').find());

            // Generate a new database instance
            db = fdb.db('test');
        });
    });
});
</script>

But there is error:

TypeError: obj.keys is not a function
$main   @   fdb-core+persist.js:11999
function    @   fdb-core+persist.js:11989
Overload.callExtend @   fdb-core+persist.js:10747
(anonymous function)    @   fdb-core+persist.js:10681
(anonymous function)    @   test.html:8

htqbuu avatar Sep 21 '16 04:09 htqbuu

I think you need to update your forerunner to the latest version. I think this will solve your problem. Could you give it a go and let me know? :)

Irrelon avatar Sep 21 '16 06:09 Irrelon

Firstly, I changed to new version 1.3.909. The js error go away.

Secondly, I found the problem. DB cannot be dropped/loaded without knowing in advance which collections it have. So, actually it is collection itself is dropped/loaded not the DB.

I made a test. First, I stored test collection data into indexedDB by FDB. Then, I use two testing logic below:

This code cannot drop real data in indexedDB

var fdb = new ForerunnerDB(),
    db = fdb.db('test');
db.drop(true);
db = fdb.db('test');

This code can drop real data in indexedDB

var fdb = new ForerunnerDB(),
    db = fdb.db('test');
db.collection('test');
db.drop(true);
db = fdb.db('test');

What I expect is FDB should be able to know which collection the DB have from indexedDB. And Whole DB should be dropped/loaded without declaring first the collections.

htqbuu avatar Sep 21 '16 10:09 htqbuu

Hmm yes that is a very good point. I think what is missing from the persistence plugin is when it saves and loads it checks for a collection manifest so it knows ahead of time what should be loaded. It is an easy change (I think).

At least you have a fix for now. I will schedule work on the plugin to make this happen.

Irrelon avatar Sep 21 '16 10:09 Irrelon

Thanks a lot for your effort

htqbuu avatar Sep 21 '16 10:09 htqbuu