Sha256: 0e3815da1e9edfa24f070c803a7a24348acddc7bc52357b8a94d3dc4ab0a9b56

Contents?: true

Size: 1.14 KB

Versions: 59

Compression:

Stored size: 1.14 KB

Contents

class AddJsonEqualityOperatorPatchToPostgres < ActiveRecord::Migration
  def up
    ActiveRecord::Base.connection.execute <<-SQL
      -- This creates a function named hashjson that transforms the
      -- json to texts and generates a hash
      CREATE OR REPLACE FUNCTION hashjson(
          json
      ) RETURNS INTEGER LANGUAGE SQL STRICT IMMUTABLE AS $$
          SELECT hashtext($1::text);
      $$;

      -- This creates a function named json_eq that checks equality (as text)
      CREATE OR REPLACE FUNCTION json_eq(
          json,
          json
      ) RETURNS BOOLEAN LANGUAGE SQL STRICT IMMUTABLE AS $$
          SELECT bttextcmp($1::text, $2::text) = 0;
      $$;

      -- This creates an operator from the equality function
      CREATE OPERATOR = (
          LEFTARG   = json,
          RIGHTARG  = json,
          PROCEDURE = json_eq
      );

      -- Finaly, this defines a new default JSON operator family with the
      -- operators and functions we just defined.
      CREATE OPERATOR CLASS json_ops
         DEFAULT FOR TYPE json USING hash AS
         OPERATOR 1  =,
         FUNCTION 1  hashjson(json);
    SQL
  end

  def down
  end
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
para-0.12.5 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.12.4 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.12.3 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.12.2 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.12.1 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.12.0 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.11.4 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.11.3 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.11.2 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.11.1 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.11.0 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.10.0 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.4 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.3.3 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.3.2 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.3.1 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.2 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.9.0 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.8.15 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb
para-0.8.14 db/migrate/20160304113055_add_json_equality_operator_patch_to_postgres.rb