Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add more options to column_exists? method #6913

Merged
merged 1 commit into from

2 participants

@lexmag

Default and null options

@carlosantoniodasilva

Can you please add some tests to ensure that works as expected and won't break in the future? Thanks.

@lexmag

Yes, i will add tests.

@lexmag lexmag Add more options to column_exists? method
Also fix failures in check options for nil
59e23e2
@lexmag

Done.

@carlosantoniodasilva carlosantoniodasilva merged commit 4d9be13 into from
@carlosantoniodasilva

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 30, 2012
  1. @lexmag

    Add more options to column_exists? method

    lexmag authored
    Also fix failures in check options for nil
This page is out of date. Refresh to see the latest.
View
10 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -68,10 +68,12 @@ def columns(table_name) end
# column_exists?(:suppliers, :name, :string, :limit => 100)
def column_exists?(table_name, column_name, type = nil, options = {})
columns(table_name).any?{ |c| c.name == column_name.to_s &&
- (!type || c.type == type) &&
- (!options[:limit] || c.limit == options[:limit]) &&
- (!options[:precision] || c.precision == options[:precision]) &&
- (!options[:scale] || c.scale == options[:scale]) }
+ (!type || c.type == type) &&
+ (!options.key?(:limit) || c.limit == options[:limit]) &&
+ (!options.key?(:precision) || c.precision == options[:precision]) &&
+ (!options.key?(:scale) || c.scale == options[:scale]) &&
+ (!options.key?(:default) || c.default == options[:default]) &&
+ (!options.key?(:null) || c.null == options[:null]) }
end
# Creates a new table with the name +table_name+. +table_name+ may either
View
18 activerecord/test/cases/migration/change_schema_test.rb
@@ -291,14 +291,20 @@ def test_column_exists_with_type
def test_column_exists_with_definition
connection.create_table :testings do |t|
- t.column :foo, :string, :limit => 100
- t.column :bar, :decimal, :precision => 8, :scale => 2
+ t.column :foo, :string, limit: 100
+ t.column :bar, :decimal, precision: 8, scale: 2
+ t.column :taggable_id, :integer, null: false
+ t.column :taggable_type, :string, default: 'Photo'
end
- assert connection.column_exists?(:testings, :foo, :string, :limit => 100)
- refute connection.column_exists?(:testings, :foo, :string, :limit => 50)
- assert connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
- refute connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
+ assert connection.column_exists?(:testings, :foo, :string, limit: 100)
+ refute connection.column_exists?(:testings, :foo, :string, limit: nil)
+ assert connection.column_exists?(:testings, :bar, :decimal, precision: 8, scale: 2)
+ refute connection.column_exists?(:testings, :bar, :decimal, precision: nil, scale: nil)
+ assert connection.column_exists?(:testings, :taggable_id, :integer, null: false)
+ refute connection.column_exists?(:testings, :taggable_id, :integer, null: true)
+ assert connection.column_exists?(:testings, :taggable_type, :string, default: 'Photo')
+ refute connection.column_exists?(:testings, :taggable_type, :string, default: nil)
end
def test_column_exists_on_table_with_no_options_parameter_supplied
Something went wrong with that request. Please try again.