diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-04-29 22:18:08 -0500 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-04 14:14:26 +0200 |
commit | 242bcac17b2b8397bd1ff77a2bddc37fc6628939 (patch) | |
tree | 2a0b37122cde4fbea13dc48899274fef1b1315bd /activerecord | |
parent | 51c0b8a2ce18a3ffd8fb5402c76f5837d0976bb2 (diff) | |
download | rails-242bcac17b2b8397bd1ff77a2bddc37fc6628939.tar.gz rails-242bcac17b2b8397bd1ff77a2bddc37fc6628939.tar.bz2 rails-242bcac17b2b8397bd1ff77a2bddc37fc6628939.zip |
refactor, move `column_for` to `AbstractAdapter` for better reuse.
Diffstat (limited to 'activerecord')
4 files changed, 11 insertions, 17 deletions
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 |