Skip to content

Instantly share code, notes, and snippets.

@maxlapshin
Created February 26, 2009 22:22
Show Gist options
  • Select an option

  • Save maxlapshin/71158 to your computer and use it in GitHub Desktop.

Select an option

Save maxlapshin/71158 to your computer and use it in GitHub Desktop.
class TeachPgToGroupConcat < ActiveRecord::Migration
def self.up
return unless adapter_name == "PostgreSQL"
execute <<-EOF
CREATE FUNCTION _group_concat(text, text, text)
RETURNS text AS $$
SELECT CASE
WHEN $2 IS NULL THEN $1
WHEN $1 IS NULL THEN $2
ELSE $1 operator(pg_catalog.||) $3 operator(pg_catalog.||) $2
END
$$ IMMUTABLE LANGUAGE SQL;
CREATE AGGREGATE group_concat(text, text) (
SFUNC = _group_concat,
STYPE = text
);
EOF
end
def self.down
return unless adapter_name == "PostgreSQL"
execute <<-EOF
DROP AGGREGATE group_concat(text, text);
DROP FUNCTION _group_concat(text, text, text)
EOF
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment