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("      SELECT attr.attname, name.nspname, seq.relname\n      FROM pg_class      seq,\n           pg_attribute  attr,\n           pg_depend     dep,\n           pg_namespace  name,\n           pg_constraint cons\n      WHERE seq.oid           = dep.objid\n        AND seq.relnamespace  = name.oid\n        AND seq.relkind       = 'S'\n        AND attr.attrelid     = dep.refobjid\n        AND attr.attnum       = dep.refobjsubid\n        AND attr.attrelid     = cons.conrelid\n        AND attr.attnum       = cons.conkey[1]\n        AND cons.contype      = 'p'\n        AND dep.refobjid      = '\#{table}'::regclass\n", 'PK and serial sequence')[0]

  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("    SELECT attr.attname, name.nspname, split_part(def.adsrc, '''', 2)\n    FROM pg_class       t\n    JOIN pg_namespace   name ON (t.relnamespace = name.oid)\n    JOIN pg_attribute   attr ON (t.oid = attrelid)\n    JOIN pg_attrdef     def  ON (adrelid = attrelid AND adnum = attnum)\n    JOIN pg_constraint  cons ON (conrelid = adrelid AND adnum = conkey[1])\n    WHERE t.oid = '\#{table}'::regclass\n      AND cons.contype = 'p'\n      AND def.adsrc ~* 'nextval'\n  end_sql\n  end\n  # check for existence of . in sequence name as in public.foo_sequence.  if it does not exist, return unqualified sequence\n  # We cannot qualify unqualified sequences, as rails doesn't qualify any table access, using the search path\n  # Commented out (DN):\n  # [result.first, result.last]\n\n  # Added (DN):\n  # The above consideration is irrelevant.  PostgreSQL\n  # databases always have tables in schemas, so specifying a schema\n  # (even if it is \"public\") is valid; and in the case where schemas\n  # *are* in use (using 'set_table_name' to set a schema on a model)\n  # the schema path is *required*, otherwise INSERTs are broken.\n\n  [ result[0], \"\#{result[1]}.\#{result[2]}\" ]\nrescue\n  nil\nend\n", 'PK and custom sequence')[0]

#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