Here’s the problem. A bunch of the post_custom_fields look like this:
"\"[\\\"[{\\\\\\\"direction\\\\\\\":\\\\\\\"up\\\\\\\",\\\\\\\"action\\\\\\\":\\\\\\\"create\\\\\\\",\\\\\\\"user_id\\\\\\\":51,\\\\\\\"created_at\\\\\\\":\\\\\\\"2018-11-02T14:21:49.785+00:00\\\\\\\"}]\\\",{\\\"direction\\\":\\\"up\\\",\\\"action\\\":\\\"create\\\",\\\"user_id\\\":61,\\\"created_at\\\":\\\"2018-11-15T18:49:43.875+00:00\\\"}]\"",
rather than, maybe, this
"[{\"direction\":\"up\",\"action\":\"create\",\"user_id\":6,\"created_at\":\"2018-07-18T16:37:42.446+00:00\"}]",
Some are worse:
"\"[\\\"[\\\\\\\"[\\\\\\\\\\\\\\\"[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"[{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"direc
tion\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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\\\\\\\\\\\\\\\\\\\\\\\\\\\"}]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"direction\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"up\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"action\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"create\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"user_id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":21,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"created_at\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2018-11-15T18:41:52.440+00:00\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}]\\\\\\\\\\\\\\\",{\\\\\\\\\\\\\\\"direction\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"up\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"action\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"destroy\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"user_id\\\\\\\\\\\\\\\":21,\\\\\\\\\\\\\\\"created_at\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"2018-11-15T18:41:56.158+00:00\\\\\\\\\\\\\\\"}]\\\\\\\",{\\\\\\\"direction\\\\\\\":\\\\\\\"up\\\\\\\",\\\\\\\"action\\\\\\\":\\\\\\\"create\\\\\\\",\\\\\\\"user_id\\\\\\\":3,\\\\\\\"created_at\\\\\\\":\\\\\\\"2019-05-17T17:21:51.066+00:00\\\\\\\"}]\\\",{\\\"direction\\\":\\\"up\\\",\\\"action\\\":\\\"create\\\",\\\"user_id\\\":43,\\\"created_at\\\":\\\"2019-06-01T14:10:48.056+00:00\\\"}]\"",
So I guess I’d need to recursively try to un-quote these a bunch of times until it got back to the right way? I guess I’ll see if I can google the Ruby un-quotify function. . . .
It looks like a real mess:
[24] pry(main)> too.value.gsub(/\\+/,"\\").undump
=> "[\"[\"[\"[\"[\"[\"[{\"direction\":\"up\",\"action\":\"create\",\"user_id\":161,\"created_at\":\"2018-11-14T01:41:18.071+00:00\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":21,\"created_at\":\"2018-11-15T18:41:37.215+00:00\"}]\",{\"direction\":\"up\",\"action\":\"destroy\",\"user_id\":21,\"created_at\":\"2018-11-15T18:41:44.680+00:00\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":21,\"created_at\":\"2018-11-15T18:41:52.440+00:00\"}]\",{\"direction\":\"up\",\"action\":\"destroy\",\"user_id\":21,\"created_at\":\"2018-11-15T18:41:56.158+00:00\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":3,\"created_at\":\"2019-05-17T17:21:51.066+00:00\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":43,\"created_at\":\"2019-06-01T14:10:48.056+00:00\"}]"
[25] pry(main)> one.value.gsub(/\\+/,"\\").undump
=> "[\"[{\"direction\":\"up\",\"action\":\"create\",\"user_id\":134,\"created_at\":\"2019-02-11T19:33:22.533+00:00\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":213,\"created_at\":\"2019-02-28T15:12:07.776+00:00\"}]"
I can do
SELECT
post_id,
jsonb_array_elements(value::jsonb) AS value
FROM post_custom_fields WHERE name = 'vote_history'
and value not like '\"[%';
discourse=> SELECT
post_id,
jsonb_array_elements(value::jsonb) AS value
FROM post_custom_fields WHERE name = 'vote_history'
and value not like '\"[%';
and get ~200 of ~250 entries, leaving a few that still look something like
1865 | "[\"[\\\"[{\\\\\\\"direction\\\\\\\":\\\\\\\"up\\\\\\\",\\\\\\\"action\\\\\\\":\\\\\\\"create\\\\\\\",\\\\\\\"user_id\\\\\\\":161,\\\\\\\"created_at\\\\\\\":\\\\\\\"2019-06-28T15:39:44.438+00:00\\\\\\\"}]\\\",{\\\"direction\\\":\\\"up\\\",\\\"action\\\":\\\"create\\\",\\\"user_id\\\":39,\\\"created_at\\\":\\\"2019-07-03T19:29:04.663+00:00\\\"}]\",{\"direction\":\"up\",\"action\":\"create\",\"user_id\":298,\"created_at\":\"2019-07-11T13:51:52.447+00:00\"}]"