Make #extract_schema_and_table an instance method in Utils

Also, move the utils test into its own test case.
This commit is contained in:
Daniel Schierbeck
2011-07-09 14:24:28 +02:00
parent 111968d402
commit a982443ae5
3 changed files with 21 additions and 16 deletions

View File

@@ -948,6 +948,8 @@ module ActiveRecord
end
module Utils
extend self
# Returns an array of <tt>[schema_name, table_name]</tt> extracted from +name+.
# +schema_name+ is nil if not specified in +name+.
# +schema_name+ and +table_name+ exclude surrounding quotes (regardless of whether provided in +name+)
@@ -958,7 +960,7 @@ module ActiveRecord
# * <tt>schema_name.table_name</tt>
# * <tt>schema_name."table.name"</tt>
# * <tt>"schema.name"."table name"</tt>
def self.extract_schema_and_table(name)
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end

View File

@@ -219,21 +219,6 @@ class SchemaTest < ActiveRecord::TestCase
end
end
def test_extract_schema_and_table
{
%(table_name) => [nil,'table_name'],
%("table.name") => [nil,'table.name'],
%(schema.table_name) => %w{schema table_name},
%("schema".table_name) => %w{schema table_name},
%(schema."table_name") => %w{schema table_name},
%("schema"."table_name") => %w{schema table_name},
%("even spaces".table) => ['even spaces','table'],
%(schema."table.name") => ['schema', 'table.name']
}.each do |given, expect|
assert_equal expect, ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils.extract_schema_and_table(given)
end
end
def test_current_schema
{
%('$user',public) => 'public',

View File

@@ -0,0 +1,18 @@
class PostgreSQLUtilsTest < ActiveSupport::TestCase
include ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
def test_extract_schema_and_table
{
%(table_name) => [nil,'table_name'],
%("table.name") => [nil,'table.name'],
%(schema.table_name) => %w{schema table_name},
%("schema".table_name) => %w{schema table_name},
%(schema."table_name") => %w{schema table_name},
%("schema"."table_name") => %w{schema table_name},
%("even spaces".table) => ['even spaces','table'],
%(schema."table.name") => ['schema', 'table.name']
}.each do |given, expect|
assert_equal expect, extract_schema_and_table(given)
end
end
end