]> git.openstreetmap.org Git - rails.git/blobdiff - config/initializers/postgresql_adapter.rb
Merge pull request #24 from zerebubuth/routing-merge
[rails.git] / config / initializers / postgresql_adapter.rb
index c5a69a1d9baa3444ac16a3ddd12895bb5b399f26..9dcc34fcc05e0706fb2400dec008068cff458b56 100644 (file)
@@ -1,54 +1,33 @@
-module ActiveRecord
-  module ConnectionAdapters
-    class PostgreSQLAdapter
-      def pk_and_sequence_for(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, 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.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.classid       = '"pg_class"'::regclass
-            AND dep.refclassid    = '"pg_class"'::regclass
-            AND dep.refobjid      = '#{quote_table_name(table)}'::regclass
-        end_sql
+if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
+  module ActiveRecord
+    module ConnectionAdapters
+      class PostgreSQLAdapter
+        def initialize_type_map_with_enums(type_map)
+          OID.alias_type "format_enum", "text"
+          OID.alias_type "gpx_visibility_enum", "text"
+          OID.alias_type "note_status_enum", "text"
+          OID.alias_type "note_event_enum", "text"
+          OID.alias_type "nwr_enum", "text"
+          OID.alias_type "user_role_enum", "text"
+          OID.alias_type "user_status_enum", "text"
 
-        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).
-          result = query(<<-end_sql, 'PK and custom sequence')[0]
-            SELECT attr.attname,
-              CASE
-                WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
-                  substr(split_part(def.adsrc, '''', 2),
-                         strpos(split_part(def.adsrc, '''', 2), '.')+1)
-                ELSE split_part(def.adsrc, '''', 2)
-              END
-            FROM pg_class       t
-            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 = '#{quote_table_name(table)}'::regclass
-              AND cons.contype = 'p'
-              AND def.adsrc ~* 'nextval'
-          end_sql
+          initialize_type_map_without_enums(type_map)
         end
 
-        # [primary_key, sequence]
-        [result.first, result.last]
-      rescue
-        nil
+        alias_method_chain :initialize_type_map, :enums
+      end
+
+      class PostgreSQLColumn
+        def simplified_type_with_enum(field_type)
+          case field_type
+          when /_enum$/
+            :string
+          else
+            simplified_type_without_enum(field_type)
+          end
+        end
+
+        alias_method_chain :simplified_type, :enum
       end
     end
   end