Sha256: 26bbfded5dfe6b97c3074ddfe2bcc7e1a607e3776c1e32c6468fd40da41e806c

Contents?: true

Size: 994 Bytes

Versions: 3

Compression:

Stored size: 994 Bytes

Contents

# frozen_string_literal: true

module PGTrunk
  # @private
  # The module adds custom type casting
  module CustomTypes
    # All custom types are typecasted to strings in Rails
    TYPE = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::SpecializedString

    def self.known
      @known ||= Set.new([])
    end

    def enable_pg_trunk_types
      execute(<<~SQL).each { |item| enable_pg_trunk_type(**item.symbolize_keys) }
        SELECT (
          CASE
          WHEN t.typnamespace = 'public'::regnamespace THEN t.typname
          ELSE t.typnamespace::regnamespace || '.' || t.typname
          END
        ) AS name, t.oid
        FROM pg_trunk e JOIN pg_type t ON t.oid = e.oid
        WHERE e.classid = 'pg_type'::regclass
      SQL
    end

    def enable_pg_trunk_type(oid:, name:)
      CustomTypes.known << name
      type_map.register_type(oid.to_i, TYPE.new(name.to_s))
    end

    def valid_type?(type)
      CustomTypes.known.include?(type.to_s) || super
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/core/railtie/custom_types.rb
pg_trunk-0.1.3 lib/pg_trunk/core/railtie/custom_types.rb
pg_trunk-0.1.2 lib/pg_trunk/core/railtie/custom_types.rb