module Upsert::MergeFunction::Postgresql::ClassMethods

Public Instance Methods

clear(connection) click to toggle source
# File lib/upsert/merge_function/postgresql.rb, line 10
def clear(connection)
  # http://stackoverflow.com/questions/7622908/postgresql-drop-function-without-knowing-the-number-type-of-parameters
  connection.execute(%{
    CREATE OR REPLACE FUNCTION pg_temp.upsert_delfunc(text)
      RETURNS void AS
    $BODY$
    DECLARE
      _sql text;
    BEGIN
    FOR _sql IN
      SELECT 'DROP FUNCTION ' || quote_ident(n.nspname)
                       || '.' || quote_ident(p.proname)
                       || '(' || pg_catalog.pg_get_function_identity_arguments(p.oid) || ');'
      FROM   pg_catalog.pg_proc p
      LEFT   JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
      WHERE  p.proname = $1
      AND    pg_catalog.pg_function_is_visible(p.oid) -- you may or may not want this
    LOOP
      EXECUTE _sql;
    END LOOP;
    END;
    $BODY$
      LANGUAGE plpgsql;
  })
  connection.execute(%{SELECT proname FROM pg_proc WHERE proname LIKE '#{MergeFunction::NAME_PREFIX}%'}).each do |row|
    k = row['proname']
    next if k == 'upsert_delfunc'
    Upsert.logger.info %{[upsert] Dropping function #{k.inspect}}
    connection.execute %{SELECT pg_temp.upsert_delfunc('#{k}')}
  end
end