From 242bcac17b2b8397bd1ff77a2bddc37fc6628939 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 29 Apr 2014 22:18:08 -0500 Subject: refactor, move `column_for` to `AbstractAdapter` for better reuse. --- .../lib/active_record/connection_adapters/abstract_adapter.rb | 7 +++++++ .../active_record/connection_adapters/abstract_mysql_adapter.rb | 9 +-------- .../connection_adapters/postgresql/schema_statements.rb | 4 ---- .../lib/active_record/connection_adapters/sqlite3_adapter.rb | 8 +++----- 4 files changed, 11 insertions(+), 17 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 78343cf4f5..116b01814e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -390,6 +390,13 @@ module ActiveRecord def without_prepared_statement?(binds) !@prepared_statements || binds.empty? end + + def column_for(table_name, column_name) # :nodoc: + unless column = columns(table_name).detect { |c| c.name == column_name.to_s } + raise ActiveRecordError, "No such column: #{table_name}.#{column_name}" + end + column + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index ebce0c0460..9da86d27f5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -713,7 +713,7 @@ module ActiveRecord def rename_column_sql(table_name, column_name, new_column_name) options = { name: new_column_name } - if column = columns(table_name).find { |c| c.name == column_name.to_s } + if column = column_for(table_name, column_name) options[:default] = column.default options[:null] = column.null options[:auto_increment] = (column.extra == "auto_increment") @@ -757,13 +757,6 @@ module ActiveRecord version[0] >= 5 end - def column_for(table_name, column_name) - unless column = columns(table_name).find { |c| c.name == column_name.to_s } - raise "No such column: #{table_name}.#{column_name}" - end - column - end - def configure_connection variables = @config.fetch(:variables, {}).stringify_keys diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 1dc7a6f0fd..e7169bd357 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -187,10 +187,6 @@ module ActiveRecord end end - def column_for(table_name, column_name) #:nodoc: - columns(table_name).detect { |c| c.name == column_name.to_s } - end - # Returns the current database name. def current_database query('select current_database()', 'SCHEMA')[0][0] diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 2c6186774f..737f2daa63 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -495,11 +495,9 @@ module ActiveRecord end def rename_column(table_name, column_name, new_column_name) #:nodoc: - unless columns(table_name).detect{|c| c.name == column_name.to_s } - raise ActiveRecord::ActiveRecordError, "Missing column #{table_name}.#{column_name}" - end - alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s}) - rename_column_indexes(table_name, column_name, new_column_name) + column = column_for(table_name, column_name) + alter_table(table_name, rename: {column.name => new_column_name.to_s}) + rename_column_indexes(table_name, column.name, new_column_name) end protected -- cgit v1.2.3