class PGTrunk::Operations::Views::CreateView

@private

Public Instance Methods

invert() click to toggle source
# File lib/pg_trunk/operations/views/create_view.rb, line 92
def invert
  irreversible!("replace_existing: true") if replace_existing
  DropView.new(**to_h)
end
to_sql(_version) click to toggle source
# File lib/pg_trunk/operations/views/create_view.rb, line 88
def to_sql(_version)
  [create_view, *create_comment, register_view].join(" ")
end

Private Instance Methods

create_comment() click to toggle source
# File lib/pg_trunk/operations/views/create_view.rb, line 108
def create_comment
  return if comment.blank?

  "COMMENT ON VIEW #{name.to_sql} IS $comment$#{comment}$comment$;"
end
create_view() click to toggle source
# File lib/pg_trunk/operations/views/create_view.rb, line 99
def create_view
  sql = "CREATE"
  sql << " OR REPLACE" if replace_existing
  sql << " VIEW #{name.to_sql}"
  sql << " AS (#{sql_definition})"
  sql << " WITH #{check.to_s.upcase} CHECK OPTION" if check.present?
  sql << ";"
end
register_view() click to toggle source
# File lib/pg_trunk/operations/views/create_view.rb, line 114
    def register_view
      <<~SQL.squish
        INSERT INTO pg_trunk (oid, classid)
          SELECT oid, 'pg_class'::regclass
          FROM pg_class
          WHERE relname = #{name.quoted}
            AND relnamespace = #{name.namespace}
            AND relkind = 'v'
        ON CONFLICT DO NOTHING;
      SQL
    end