Skip to content

Instantly share code, notes, and snippets.

@rubypanther
Created February 25, 2011 10:31

Revisions

  1. rubypanther revised this gist Feb 25, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgresql_view_monkey.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ def table_exists?(name)
    table, schema = name.split('.', 2).reverse # avoid futzing with no schema case

    if name =~ /^"/ # Handle quoted table names
    table = name.gsub(/(^"|"$)/,'')
    table = name.gsub(/^"|"$/,'')
    schema = nil
    end

  2. rubypanther revised this gist Feb 25, 2011. 2 changed files with 2 additions and 2 deletions.
    File renamed without changes.
    4 changes: 2 additions & 2 deletions postgresql_view_monkey.rb
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ def table_exists?(name)

    not query(<<-SQL).blank?
    SELECT TRUE FROM pg_tables, pg_views
    WHERE (pg_tables.tablename = '#{tn}'
    OR pg_views.viewname = '#{tn}')
    WHERE (pg_tables.tablename = '#{table}'
    OR pg_views.viewname = '#{table}')
    #{schema ? "AND schemaname = '#{schema}'" : ''} LIMIT 1
    SQL
    end
  3. rubypanther revised this gist Feb 25, 2011. 2 changed files with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions app/contollers/application.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    class ApplicationController < ActionController::Base
    require 'postgresql_view_monkey'
    end
    File renamed without changes.
  4. rubypanther renamed this gist Feb 25, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. rubypanther created this gist Feb 25, 2011.
    22 changes: 22 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    module ActiveRecord
    module ConnectionAdapters
    class PostgreSQLAdapter
    def table_exists?(name)
    name = name.to_s
    table, schema = name.split('.', 2).reverse # avoid futzing with no schema case

    if name =~ /^"/ # Handle quoted table names
    table = name.gsub(/(^"|"$)/,'')
    schema = nil
    end

    not query(<<-SQL).blank?
    SELECT TRUE FROM pg_tables, pg_views
    WHERE (pg_tables.tablename = '#{tn}'
    OR pg_views.viewname = '#{tn}')
    #{schema ? "AND schemaname = '#{schema}'" : ''} LIMIT 1
    SQL
    end
    end
    end
    end