Module: PGTrunk::CustomTypes

Defined in:
lib/pg_trunk/core/railtie/custom_types.rb

Overview

The module adds custom type casting

Constant Summary collapse

TYPE =

All custom types are typecasted to strings in Rails

ActiveRecord::ConnectionAdapters::PostgreSQL::OID::SpecializedString

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.knownObject



10
11
12
# File 'lib/pg_trunk/core/railtie/custom_types.rb', line 10

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

Instance Method Details

#enable_pg_trunk_type(oid:, name:) ⇒ Object



27
28
29
30
# File 'lib/pg_trunk/core/railtie/custom_types.rb', line 27

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

#enable_pg_trunk_typesObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pg_trunk/core/railtie/custom_types.rb', line 14

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

#valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pg_trunk/core/railtie/custom_types.rb', line 32

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