Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactNative SQLite error: UNIQUE constraint failed: attach-store.digest #6037

Closed
craftzdog opened this issue Dec 20, 2016 · 5 comments
Closed
Labels

Comments

@craftzdog
Copy link

Hi, thanks for making such a great library!
I'd like to post an issue regarding pouchdb-adapter-websql-core.

Issue

Can't store multiple documents with same attachment data.
The replication process retrieves docs from _changes feed so multiple attachments are potentially saved at once.
If these have same md5 digest, it will produce an error: UNIQUE constraint failed: attach-store.digest.
Seems like saveAttachment is ignoring existing attachment but not working in this case:
https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-adapter-websql-core/src/bulkDocs.js#L302

I guess the reason is that attachments are stored in forEach loop where each process doesn't wait for storing the previous attachment:
https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-adapter-websql-core/src/bulkDocs.js#L212

Info

  • Environment: ReactNative
  • Platform: iOS
  • Adapter: SQLite Adapter for ReactNative (It's my module)
  • Server: CouchDB

Reproduce

Sync with multiple attachments with same md5digest.
I encountered this problem while making a SQLite adapter for ReactNative.

@nolanlawson
Copy link
Member

My suspicion is that this is related to the ReactNative SQLite adapter and not PouchDB. The reason I suggest that is that we have run our full test suite using:

node-websql
- WebSQL on Chrome and Safari
cordova-plugin-sqlite-2

However react-native-sqlite-storage is based on Cordova-sqlite-storage which in my experience has had many bugs and has often failed our test suite (it's largely why I wrote cordova-plugin-sqlite-2).

Could you please run your same test in another SQL-esque implementation (WebSQL, node-websql, cordova-plugin-sqlite-2) and see if the issue persists? If not, then I would guess it's related to react-native-sqlite-storage.

Alternatively, if you can find a test that fails in regular WebSQL, then please provide that test and we will fix it in pouchdb-adapter-websql-core asap. :) You may also be interested to see our existing attachment replication tests here.

@nolanlawson nolanlawson changed the title UNIQUE constraint failed: attach-store.digest ReactNative SQLite error: UNIQUE constraint failed: attach-store.digest Dec 21, 2016
@craftzdog
Copy link
Author

I made another SQLite module for react native which code base is borrowed from your cordova-plugin-sqlite-2.
It works pretty well!
https://github.com/noradaiko/react-native-sqlite-2

Thanks

@nolanlawson
Copy link
Member

nolanlawson commented Mar 23, 2017 via email

@nolanlawson
Copy link
Member

nolanlawson commented Mar 23, 2017

I added a mention of your project to the node-websql readme. 😃

By the way, do you still consider it "unstable for storing PouchDB's attachments" (as you say in the readme?). I've run PouchDB's attachment test suite against node-websql and cordova-plugin-sqlite-2 (both Android and iOS) and it passes, so it may be something wrong with the React Native bindings?

@craftzdog
Copy link
Author

I'm glad!

I mean that it's unstable with react-native-sqlite-storage.
I've tested storing attachments with react-native-sqlite-2 and UNIQUE constraint error didn't occur.
The difference from cordova-plugin-sqlite-2 is only bridging between JS and native.
There's also a little patch for avoiding Android's problem truncating string including \u0000.
I'd like to run the test suite but I have to learn testing RN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants