class PGTrunk::Operations::CompositeTypes::CreateCompositeType

@private

Public Instance Methods

invert() click to toggle source
# File lib/pg_trunk/operations/composite_types/create_composite_type.rb, line 72
def invert
  DropCompositeType.new(**to_h)
end
to_sql(_version) click to toggle source
# File lib/pg_trunk/operations/composite_types/create_composite_type.rb, line 68
def to_sql(_version)
  [create_type, *create_comment, register_type].join(" ")
end

Private Instance Methods

create_comment() click to toggle source
# File lib/pg_trunk/operations/composite_types/create_composite_type.rb, line 85
def create_comment
  return if comment.blank?

  "COMMENT ON TYPE #{name.to_sql} IS $comment$#{comment}$comment$;"
end
create_type() click to toggle source
# File lib/pg_trunk/operations/composite_types/create_composite_type.rb, line 78
    def create_type
      <<~SQL.squish
        CREATE TYPE #{name.to_sql}
        AS (#{columns.reject(&:change).map(&:to_sql).join(',')});
      SQL
    end
register_type() click to toggle source
# File lib/pg_trunk/operations/composite_types/create_composite_type.rb, line 91
    def register_type
      <<~SQL.squish
        INSERT INTO pg_trunk (oid, classid)
          SELECT oid, 'pg_type'::regclass
          FROM pg_type
          WHERE typname = #{name.quoted}
            AND typnamespace = #{name.namespace}
            AND typtype = 'c'
        ON CONFLICT DO NOTHING;
      SQL
    end