Permalink
Browse files

refactor, move `column_for` to `AbstractAdapter` for better reuse.

  • Loading branch information...
1 parent 51c0b8a commit 242bcac17b2b8397bd1ff77a2bddc37fc6628939 @senny senny committed Apr 30, 2014
@@ -390,6 +390,13 @@ def translate_exception(exception, message)
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
@@ -713,7 +713,7 @@ def change_column_sql(table_name, column_name, type, options = {})
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 @@ def supports_views?
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
@@ -187,10 +187,6 @@ def columns(table_name)
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]
@@ -495,11 +495,9 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
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

0 comments on commit 242bcac

Please sign in to comment.