Skip to content

Commit 64b33b6

Browse files
woodslifo
authored andcommitted
Quote table names when casting to regclass so that capitalized tables are supported. [#2418 state:resolved]
Signed-off-by: Tarmo Tänav <tarmo@itech.ee>
1 parent cdcd638 commit 64b33b6

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ def pk_and_sequence_for(table) #:nodoc:
764764
AND attr.attrelid = cons.conrelid
765765
AND attr.attnum = cons.conkey[1]
766766
AND cons.contype = 'p'
767-
AND dep.refobjid = '#{table}'::regclass
767+
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
768768
end_sql
769769

770770
if result.nil? or result.empty?
@@ -783,7 +783,7 @@ def pk_and_sequence_for(table) #:nodoc:
783783
JOIN pg_attribute attr ON (t.oid = attrelid)
784784
JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
785785
JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
786-
WHERE t.oid = '#{table}'::regclass
786+
WHERE t.oid = '#{quote_table_name(table)}'::regclass
787787
AND cons.contype = 'p'
788788
AND def.adsrc ~* 'nextval'
789789
end_sql
@@ -1059,7 +1059,7 @@ def column_definitions(table_name) #:nodoc:
10591059
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
10601060
FROM pg_attribute a LEFT JOIN pg_attrdef d
10611061
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
1062-
WHERE a.attrelid = '#{table_name}'::regclass
1062+
WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
10631063
AND a.attnum > 0 AND NOT a.attisdropped
10641064
ORDER BY a.attnum
10651065
end_sql

activerecord/test/cases/schema_dumper_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def test_schema_dump_excludes_sqlite_sequence
2222
assert_no_match %r{create_table "sqlite_sequence"}, output
2323
end
2424

25+
def test_schema_dump_includes_camelcase_table_name
26+
output = standard_dump
27+
assert_match %r{create_table "CamelCase"}, output
28+
end
29+
2530
def assert_line_up(lines, pattern, required = false)
2631
return assert(true) if lines.empty?
2732
matches = lines.map { |line| line.match(pattern) }

activerecord/test/cases/schema_test_postgresql.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class SchemaTest < ActiveRecord::TestCase
66
SCHEMA_NAME = 'test_schema'
77
SCHEMA2_NAME = 'test_schema2'
88
TABLE_NAME = 'things'
9+
CAPITALIZED_TABLE_NAME = 'Things'
910
INDEX_A_NAME = 'a_index_things_on_name'
1011
INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
1112
INDEX_A_COLUMN = 'name'
@@ -30,10 +31,15 @@ class Thing3 < ActiveRecord::Base
3031
set_table_name 'test_schema."things.table"'
3132
end
3233

34+
class Thing4 < ActiveRecord::Base
35+
set_table_name 'test_schema."Things"'
36+
end
37+
3338
def setup
3439
@connection = ActiveRecord::Base.connection
3540
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
3641
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
42+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})"
3743
@connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
3844
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
3945
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
@@ -52,6 +58,12 @@ def test_with_schema_prefixed_table_name
5258
end
5359
end
5460

61+
def test_with_schema_prefixed_capitalized_table_name
62+
assert_nothing_raised do
63+
assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{CAPITALIZED_TABLE_NAME}")
64+
end
65+
end
66+
5567
def test_with_schema_search_path
5668
assert_nothing_raised do
5769
with_schema_search_path(SCHEMA_NAME) do
@@ -74,21 +86,31 @@ def test_classes_with_qualified_schema_name
7486
assert_equal 0, Thing1.count
7587
assert_equal 0, Thing2.count
7688
assert_equal 0, Thing3.count
89+
assert_equal 0, Thing4.count
7790

7891
Thing1.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
7992
assert_equal 1, Thing1.count
8093
assert_equal 0, Thing2.count
8194
assert_equal 0, Thing3.count
95+
assert_equal 0, Thing4.count
8296

8397
Thing2.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
8498
assert_equal 1, Thing1.count
8599
assert_equal 1, Thing2.count
86100
assert_equal 0, Thing3.count
101+
assert_equal 0, Thing4.count
87102

88103
Thing3.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
89104
assert_equal 1, Thing1.count
90105
assert_equal 1, Thing2.count
91106
assert_equal 1, Thing3.count
107+
assert_equal 0, Thing4.count
108+
109+
Thing4.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
110+
assert_equal 1, Thing1.count
111+
assert_equal 1, Thing2.count
112+
assert_equal 1, Thing3.count
113+
assert_equal 1, Thing4.count
92114
end
93115

94116
def test_raise_on_unquoted_schema_name

activerecord/test/schema/schema.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def create_table(*args, &block)
6868
t.boolean :value
6969
end
7070

71+
create_table "CamelCase", :force => true do |t|
72+
t.string :name
73+
end
74+
7175
create_table :categories, :force => true do |t|
7276
t.string :name, :null => false
7377
t.string :type

0 commit comments

Comments
 (0)