Skip to content
This repository
Browse code

remove table quoting in primary_key method

* add/cleanup tests
  • Loading branch information...
commit 1d7c751bf703c729887e2d8a9ae104a8e6aef010 1 parent 019c263
Paul Gallagher authored June 11, 2011
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -864,7 +864,7 @@ def pk_and_sequence_for(table) #:nodoc:
864 864
 
865 865
       # Returns just a table's primary key
866 866
       def primary_key(table)
867  
-        row = exec_query(<<-end_sql, 'SCHEMA', [[nil, quote_table_name(table)]]).rows.first
  867
+        row = exec_query(<<-end_sql, 'SCHEMA', [[nil, table]]).rows.first
868 868
           SELECT DISTINCT(attr.attname)
869 869
           FROM pg_attribute attr
870 870
           INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
30  activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -74,6 +74,36 @@ def test_default_sequence_name_bad_table
74 74
           @connection.default_sequence_name('zomg')
75 75
       end
76 76
 
  77
+      def test_pk_and_sequence_for
  78
+        pk, seq = @connection.pk_and_sequence_for('ex')
  79
+        assert_equal 'id', pk
  80
+        assert_equal @connection.default_sequence_name('ex', 'id'), seq
  81
+      end
  82
+
  83
+      def test_pk_and_sequence_for_with_non_standard_primary_key
  84
+        @connection.exec_query('drop table if exists ex')
  85
+        @connection.exec_query('create table ex(code serial primary key)')
  86
+        pk, seq = @connection.pk_and_sequence_for('ex')
  87
+        assert_equal 'code', pk
  88
+        assert_equal @connection.default_sequence_name('ex', 'code'), seq
  89
+      end
  90
+
  91
+      def test_pk_and_sequence_for_returns_nil_if_no_seq
  92
+        @connection.exec_query('drop table if exists ex')
  93
+        @connection.exec_query('create table ex(id integer primary key)')
  94
+        assert_nil @connection.pk_and_sequence_for('ex')
  95
+      end
  96
+
  97
+      def test_pk_and_sequence_for_returns_nil_if_no_pk
  98
+        @connection.exec_query('drop table if exists ex')
  99
+        @connection.exec_query('create table ex(id integer)')
  100
+        assert_nil @connection.pk_and_sequence_for('ex')
  101
+      end
  102
+
  103
+      def test_pk_and_sequence_for_returns_nil_if_table_not_found
  104
+        assert_nil @connection.pk_and_sequence_for('unobtainium')
  105
+      end
  106
+
77 107
       def test_exec_insert_number
78 108
         insert(@connection, 'number' => 10)
79 109
 
22  activerecord/test/cases/adapters/postgresql/schema_test.rb
@@ -38,10 +38,6 @@ class Thing4 < ActiveRecord::Base
38 38
     set_table_name 'test_schema."Things"'
39 39
   end
40 40
 
41  
-  class PrimaryKeyTestHarness < ActiveRecord::Base
42  
-    set_table_name 'test_schema.pktest'
43  
-  end
44  
-
45 41
   def setup
46 42
     @connection = ActiveRecord::Base.connection
47 43
     @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@@ -189,7 +185,11 @@ def test_with_uppercase_index_name
189 185
   end
190 186
 
191 187
   def test_primary_key_with_schema_specified
192  
-    [ %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}"), %(#{SCHEMA_NAME}."#{PK_TABLE_NAME}"), %(#{SCHEMA_NAME}."#{PK_TABLE_NAME}")].each do |given|
  188
+    [
  189
+      %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}"),
  190
+      %(#{SCHEMA_NAME}."#{PK_TABLE_NAME}"),
  191
+      %(#{SCHEMA_NAME}.#{PK_TABLE_NAME})
  192
+    ].each do |given|
193 193
       assert_equal 'id', @connection.primary_key(given), "primary key should be found when table referenced as #{given}"
194 194
     end
195 195
   end
@@ -208,6 +208,18 @@ def test_primary_key_raises_error_if_table_not_found_on_schema_search_path
208 208
     end
209 209
   end
210 210
 
  211
+  def test_pk_and_sequence_for_with_schema_specified
  212
+    [
  213
+      %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}"),
  214
+      %(#{SCHEMA_NAME}."#{PK_TABLE_NAME}"),
  215
+      %(#{SCHEMA_NAME}.#{PK_TABLE_NAME})
  216
+    ].each do |given|
  217
+      pk, seq = @connection.pk_and_sequence_for(given)
  218
+      assert_equal 'id', pk, "primary key should be found when table referenced as #{given}"
  219
+      assert_equal "#{SCHEMA_NAME}.#{PK_TABLE_NAME}_id_seq", seq, "sequence name should be found when table referenced as #{given}"
  220
+    end
  221
+  end
  222
+
211 223
   def test_extract_schema_and_table
212 224
     {
213 225
       %(table_name)            => [nil,'table_name'],

0 notes on commit 1d7c751

Ivan Schneider

Why has quoting been removed here ? This causes the method to fail for tables with a name containing one or more capital letters.

Paul Gallagher

it's been a while since I looked at this (and I'm actually unsure whether this code made it into a release - need to check), but I remember the main reason was that by applying quoting within the pk method, it was breaking the ability to supply schema-qualified table names. i.e. :primary_key is assuming quoting has been applied if necessary on entry .. and this is what is done when the method is used internally.

Have you found some rails core that now breaks because of this? When I last looked at the code it was all green.

Please sign in to comment.
Something went wrong with that request. Please try again.