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

Fix import of saved json[b] fields for postgres #594

Conversation

stokarenko
Copy link
Contributor

@stokarenko stokarenko commented Feb 17, 2019

Well, activerecord-import silently corrupted a tons of the data on my server 💣
Fortunately, that was a staging one, and it's possible to revert this time 🍀 😃

How that happened:

  1. I have a pure jsonb postgres field - without default value (don't ask me why 😞), without serialize nor store_accessor definition on model layer.
  2. I use ActiveRecord.import with on_duplicate_key_update turned on.
  3. everything is fine when the new value comes there for jsonb field. But...
  4. if jsonb field was not changed - activerecord-import will take it from AR#read_attribute_before_type_cast. As a result in DB it will replace the original jsonb object with that object serialized to JSON twice. Postgres will silently accepts that, because such string is valid JSON string...
  5. Each time when import was called - the things becomes to be worse and worse for particular record 🔥, starting from normal {"a": "b"} JSON object, till "\"\\\"{\\\\\\\"a\\\\\\\": \\\\\\\"b\\\\\\\"}\\\"\"" and further! 🚀

The fix is similar to 2a6158d. It fixes pure json case as well.

@stokarenko stokarenko force-pushed the fix-the-import-of-saved-json-fields-for-postgres branch from 81b3f28 to e4875ea Compare February 17, 2019 15:07
@stokarenko
Copy link
Contributor Author

Travis failed due to the reason like travis-ci/travis-ci#7987
Build rerun could help I believe

@jkowens
Copy link
Collaborator

jkowens commented Feb 18, 2019

Looks like the Travis builds are working again. I'm just seeing some rubocop offenses now.

@stokarenko
Copy link
Contributor Author

@jkowens rubocop has been fixed! )

@stokarenko stokarenko force-pushed the fix-the-import-of-saved-json-fields-for-postgres branch from b40b64c to adcf6e1 Compare February 19, 2019 08:08
@jkowens
Copy link
Collaborator

jkowens commented Feb 19, 2019

Looks good, thanks!

@jkowens jkowens merged commit 7f07ab3 into zdennis:master Feb 19, 2019
@jkowens
Copy link
Collaborator

jkowens commented Feb 25, 2019

Released in v1.0.1!

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

Successfully merging this pull request may close these issues.

2 participants