Module: Goldberg::PostgreSQL

Defined in:
lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/model.rb

Overview

Fixes the “pk_and_sequence_for” method in the PostgreSQL adapter, to include namespace support.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/model.rb', line 42

def self.included(base)
  base.class_eval do
    alias_method_chain :pk_and_sequence_for, :goldberg
    alias_method_chain :quote_column_name, :goldberg
    alias_method_chain :quote_table_name, :goldberg
  end
end

Instance Method Details

#pk_and_sequence_for_with_goldberg(table) ⇒ Object

(From vendor/rails/activerecord/lib/active_record/connection_adapters/ postgresql_adapter.rb)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/model.rb', line 54

def pk_and_sequence_for_with_goldberg(table)
  # First try looking for a sequence with a dependency on the
  # given table's primary key.
  result = query(<<-end_sql, 'PK and serial sequence')[0]
      SELECT attr.attname, name.nspname, seq.relname
      FROM pg_class      seq,
           pg_attribute  attr,
           pg_depend     dep,
           pg_namespace  name,
           pg_constraint cons
      WHERE seq.oid           = dep.objid
        AND seq.relnamespace  = name.oid
        AND seq.relkind       = 'S'
        AND attr.attrelid     = dep.refobjid
        AND attr.attnum       = dep.refobjsubid
        AND attr.attrelid     = cons.conrelid
        AND attr.attnum       = cons.conkey[1]
        AND cons.contype      = 'p'
        AND dep.refobjid      = '#{table}'::regclass
end_sql

  if result.nil? or result.empty?
    # If that fails, try parsing the primary key's default value.
    # Support the 7.x and 8.0 nextval('foo'::text) as well as
    # the 8.1+ nextval('foo'::regclass).
    # TODO: assumes sequence is in same schema as table.
    result = query(<<-end_sql, 'PK and custom sequence')[0]
    SELECT attr.attname, name.nspname, split_part(def.adsrc, '''', 2)
    FROM pg_class       t
    JOIN pg_namespace   name ON (t.relnamespace = name.oid)
    JOIN pg_attribute   attr ON (t.oid = attrelid)
    JOIN pg_attrdef     def  ON (adrelid = attrelid AND adnum = attnum)
    JOIN pg_constraint  cons ON (conrelid = adrelid AND adnum = conkey[1])
    WHERE t.oid = '#{table}'::regclass
      AND cons.contype = 'p'
      AND def.adsrc ~* 'nextval'
  end_sql
  end
  # check for existence of . in sequence name as in public.foo_sequence.  if it does not exist, return unqualified sequence
  # We cannot qualify unqualified sequences, as rails doesn't qualify any table access, using the search path
  # Commented out (DN):
  # [result.first, result.last]

  # Added (DN):
  # The above consideration is irrelevant.  PostgreSQL
  # databases always have tables in schemas, so specifying a schema
  # (even if it is "public") is valid; and in the case where schemas
  # *are* in use (using 'set_table_name' to set a schema on a model)
  # the schema path is *required*, otherwise INSERTs are broken.

  [ result[0], "#{result[1]}.#{result[2]}" ]
rescue
  nil
end

#quote_column_name_with_goldberg(name) ⇒ Object



109
110
111
112
113
114
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/model.rb', line 109

def quote_column_name_with_goldberg(name)
  prefix = 'goldberg.'
  name.respond_to?('[]'.to_sym) &&
    name[0 ... prefix.length] == prefix ? name :
    quote_column_name_without_goldberg(name)
end

#quote_table_name_with_goldberg(name) ⇒ Object



116
117
118
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/model.rb', line 116

def quote_table_name_with_goldberg(name)
  quote_column_name_with_goldberg(name)
end