Sha256: 9b203873585750ff16c73ffe316163d11bc47fbfe40d61b6b419073a9be9129e

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module AR
  module Check
    module Adapter
      def check_constraints(table)
        result = select_all <<~SQL
          SELECT c.conname  AS name,
                 pg_get_constraintdef(c.oid) AS expression,
                 t1.relname AS table
          FROM pg_constraint c
          JOIN pg_class t1 ON c.conrelid = t1.oid
          JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
          JOIN pg_namespace t2 ON c.connamespace = t2.oid
          WHERE c.contype = 'c'
            AND c.convalidated = TRUE
            AND t1.relname = '#{table}'
            AND t2.nspname = ANY (current_schemas(false))
        SQL

        result.to_a
      end

      def add_check(table, constraint_name, expression)
        sql = <<-SQL
          ALTER TABLE #{table}
          ADD CONSTRAINT #{quote_column_name("#{constraint_name}_on_#{table}")}
          CHECK (#{expression})
        SQL
        execute(sql)
      end

      def remove_check(table, constraint_name)
        sql = <<-SQL
          ALTER TABLE #{table}
          DROP CONSTRAINT #{quote_column_name("#{constraint_name}_on_#{table}")}
        SQL
        execute(sql)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar-check-0.2.2 lib/ar/check/adapter.rb