Skip to content

Instantly share code, notes, and snippets.

@leandro
Created November 17, 2022 14:43
Show Gist options
  • Save leandro/b8297ab82d3bd0768a56f42ada011397 to your computer and use it in GitHub Desktop.
Save leandro/b8297ab82d3bd0768a56f42ada011397 to your computer and use it in GitHub Desktop.
Findings about collation on PostgreSQL + Rails and its varying behaviors between OSs
# ==============================================================================
# On development's rails console (BSD based OS - MacOS):
# ------------------------------------------------------------------------------
a = %Q|SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"},{"a":"3","b":"FOO"},{"a":"4","b":"BOring"}]') AS x(a INT, b TEXT) ORDER BY b;|
ActiveRecord::Base.connection.execute(a).to_a
# [
# [0] {
# "a" => 4,
# "b" => "BOring"
# },
# [1] {
# "a" => 3,
# "b" => "FOO"
# },
# [2] {
# "a" => 2,
# "b" => "bar"
# },
# [3] {
# "a" => 1,
# "b" => "foo"
# }
# ]
b = %Q|SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"},{"a":"3","b":"FOO"},{"a":"4","b":"BOring"}]') AS x(a INT, b TEXT) ORDER BY b COLLATE "und-x-icu";|
ActiveRecord::Base.connection.execute(b).to_a
# [
# [0] {
# "a" => 2,
# "b" => "bar"
# },
# [1] {
# "a" => 4,
# "b" => "BOring"
# },
# [2] {
# "a" => 1,
# "b" => "foo"
# },
# [3] {
# "a" => 3,
# "b" => "FOO"
# }
# ]
# ==============================================================================
# ==============================================================================
# On productions's rails console (Linux based OS):
# ------------------------------------------------------------------------------
a = %Q|SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"},{"a":"3","b":"FOO"},{"a":"4","b":"BOring"}]') AS x(a INT, b TEXT) ORDER BY b;|
ActiveRecord::Base.connection.execute(a).to_a
# [
# [0] {
# "a" => 2,
# "b" => "bar"
# },
# [1] {
# "a" => 4,
# "b" => "BOring"
# },
# [2] {
# "a" => 1,
# "b" => "foo"
# },
# [3] {
# "a" => 3,
# "b" => "FOO"
# }
# ]
b = %Q|SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"},{"a":"3","b":"FOO"},{"a":"4","b":"BOring"}]') AS x(a INT, b TEXT) ORDER BY b COLLATE "und-x-icu";|
ActiveRecord::Base.connection.execute(b).to_a
# [
# [0] {
# "a" => 2,
# "b" => "bar"
# },
# [1] {
# "a" => 4,
# "b" => "BOring"
# },
# [2] {
# "a" => 1,
# "b" => "foo"
# },
# [3] {
# "a" => 3,
# "b" => "FOO"
# }
# ]
# ==============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment