Skip to content

Commit

Permalink
Quote table names when casting to regclass so that capitalized tables…
Browse files Browse the repository at this point in the history
… are supported. [#2418 state:resolved]

Signed-off-by: Tarmo Tänav <tarmo@itech.ee>
  • Loading branch information
woods authored and lifo committed Apr 21, 2009
1 parent cdcd638 commit 64b33b6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
Expand Up @@ -764,7 +764,7 @@ def pk_and_sequence_for(table) #:nodoc:
AND attr.attrelid = cons.conrelid
AND attr.attnum = cons.conkey[1]
AND cons.contype = 'p'
AND dep.refobjid = '#{table}'::regclass
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
end_sql

if result.nil? or result.empty?
Expand All @@ -783,7 +783,7 @@ def pk_and_sequence_for(table) #:nodoc:
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
WHERE t.oid = '#{quote_table_name(table)}'::regclass
AND cons.contype = 'p'
AND def.adsrc ~* 'nextval'
end_sql
Expand Down Expand Up @@ -1059,7 +1059,7 @@ def column_definitions(table_name) #:nodoc:
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '#{table_name}'::regclass
WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
end_sql
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -22,6 +22,11 @@ def test_schema_dump_excludes_sqlite_sequence
assert_no_match %r{create_table "sqlite_sequence"}, output
end

def test_schema_dump_includes_camelcase_table_name
output = standard_dump
assert_match %r{create_table "CamelCase"}, output
end

def assert_line_up(lines, pattern, required = false)
return assert(true) if lines.empty?
matches = lines.map { |line| line.match(pattern) }
Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/schema_test_postgresql.rb
Expand Up @@ -6,6 +6,7 @@ class SchemaTest < ActiveRecord::TestCase
SCHEMA_NAME = 'test_schema'
SCHEMA2_NAME = 'test_schema2'
TABLE_NAME = 'things'
CAPITALIZED_TABLE_NAME = 'Things'
INDEX_A_NAME = 'a_index_things_on_name'
INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
INDEX_A_COLUMN = 'name'
Expand All @@ -30,10 +31,15 @@ class Thing3 < ActiveRecord::Base
set_table_name 'test_schema."things.table"'
end

class Thing4 < ActiveRecord::Base
set_table_name 'test_schema."Things"'
end

def setup
@connection = ActiveRecord::Base.connection
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})"
@connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
Expand All @@ -52,6 +58,12 @@ def test_with_schema_prefixed_table_name
end
end

def test_with_schema_prefixed_capitalized_table_name
assert_nothing_raised do
assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{CAPITALIZED_TABLE_NAME}")
end
end

def test_with_schema_search_path
assert_nothing_raised do
with_schema_search_path(SCHEMA_NAME) do
Expand All @@ -74,21 +86,31 @@ def test_classes_with_qualified_schema_name
assert_equal 0, Thing1.count
assert_equal 0, Thing2.count
assert_equal 0, Thing3.count
assert_equal 0, Thing4.count

Thing1.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
assert_equal 1, Thing1.count
assert_equal 0, Thing2.count
assert_equal 0, Thing3.count
assert_equal 0, Thing4.count

Thing2.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
assert_equal 1, Thing1.count
assert_equal 1, Thing2.count
assert_equal 0, Thing3.count
assert_equal 0, Thing4.count

Thing3.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
assert_equal 1, Thing1.count
assert_equal 1, Thing2.count
assert_equal 1, Thing3.count
assert_equal 0, Thing4.count

Thing4.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
assert_equal 1, Thing1.count
assert_equal 1, Thing2.count
assert_equal 1, Thing3.count
assert_equal 1, Thing4.count
end

def test_raise_on_unquoted_schema_name
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -68,6 +68,10 @@ def create_table(*args, &block)
t.boolean :value
end

create_table "CamelCase", :force => true do |t|
t.string :name
end

create_table :categories, :force => true do |t|
t.string :name, :null => false
t.string :type
Expand Down

0 comments on commit 64b33b6

Please sign in to comment.