Skip to content

Commit

Permalink
Created a layer of abstraction for the valid type checking in schema …
Browse files Browse the repository at this point in the history
…dumper. Now, connection handles the check for valid types so that each database can handle the changes individually.
  • Loading branch information
ranjaykrishna authored and tenderlove committed Mar 25, 2013
1 parent 7219c5f commit c2e2031
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
Expand Up @@ -599,6 +599,10 @@ def strict_mode?
self.class.type_cast_config_to_boolean(@config.fetch(:strict, true)) self.class.type_cast_config_to_boolean(@config.fetch(:strict, true))
end end


def valid_type?(type)
!native_database_types[type].nil?
end

protected protected


# MySQL is too stupid to create a temporary table for use subquery, so we have # MySQL is too stupid to create a temporary table for use subquery, so we have
Expand Down
Expand Up @@ -663,6 +663,10 @@ def use_insert_returning?
@use_insert_returning @use_insert_returning
end end


def valid_type?(type)
!native_database_types[type].nil?
end

protected protected


# Returns the version of the connected PostgreSQL server. # Returns the version of the connected PostgreSQL server.
Expand Down
Expand Up @@ -499,6 +499,10 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
rename_column_indexes(table_name, column_name, new_column_name) rename_column_indexes(table_name, column_name, new_column_name)
end end


def valid_type?(type)
true
end

protected protected
def select(sql, name = nil, binds = []) #:nodoc: def select(sql, name = nil, binds = []) #:nodoc:
exec_query(sql, name, binds) exec_query(sql, name, binds)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/schema_dumper.rb
Expand Up @@ -118,7 +118,7 @@ def table(table, stream)


# then dump all non-primary key columns # then dump all non-primary key columns
column_specs = columns.map do |column| column_specs = columns.map do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil? raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk next if column.name == pk
@connection.column_spec(column, @types) @connection.column_spec(column, @types)
end.compact end.compact
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
Expand Up @@ -16,6 +16,15 @@ def setup
eosql eosql
end end


def test_valid_column
column = @conn.columns('ex').find { |col| col.name == 'id' }
assert @conn.valid_type?(column.type)
end

def test_invalid_column
assert_not @conn.valid_type?(:foobar)
end

def test_client_encoding def test_client_encoding
assert_equal Encoding::UTF_8, @conn.client_encoding assert_equal Encoding::UTF_8, @conn.client_encoding
end end
Expand Down
Expand Up @@ -10,6 +10,15 @@ def setup
@connection.exec_query('create table ex(id serial primary key, number integer, data character varying(255))') @connection.exec_query('create table ex(id serial primary key, number integer, data character varying(255))')
end end


def test_valid_column
column = @connection.columns('ex').find { |col| col.name == 'id' }
assert @connection.valid_type?(column.type)
end

def test_invalid_column
assert_not @connection.valid_type?(:foobar)
end

def test_primary_key def test_primary_key
assert_equal 'id', @connection.primary_key('ex') assert_equal 'id', @connection.primary_key('ex')
end end
Expand Down
Expand Up @@ -25,6 +25,15 @@ def setup
@conn.intercepted = true @conn.intercepted = true
end end


def test_valid_column
column = @conn.columns('items').find { |col| col.name == 'id' }
assert @conn.valid_type?(column.type)
end

def test_invalid_column
assert @conn.valid_type?(:foobar)
end

def teardown def teardown
@conn.intercepted = false @conn.intercepted = false
@conn.logged = [] @conn.logged = []
Expand Down

0 comments on commit c2e2031

Please sign in to comment.