From 869a172a8a6e2d2182f16e959f4a41fa10df133a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 7 Jun 2007 23:25:50 +0000 Subject: Migrations: raise if a column is duplicated. Closes #7345. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6961 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../connection_adapters/abstract/schema_definitions.rb | 3 ++- activerecord/test/migration_test.rb | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e56b12f274..b480050a5f 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Migrations: raise if a column is duplicated. #7345 [Jeremy McAnally, Josh Peek] + * Fixtures: correctly delete and insert fixtures in a single transaction. #8553 [Michael Schuerig] * Fixtures: people(:technomancy, :josh) returns both fixtures. #7880 [technomancy, Josh Peek] diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index b6c3949a5a..80f9e52f80 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -351,7 +351,8 @@ module ActiveRecord # There's a short-hand method for each of the type values declared at the top. And then there's # TableDefinition#timestamps that'll add created_at and updated_at as datetimes. def column(name, type, options = {}) - column = self[name] || ColumnDefinition.new(@base, name, type) + raise "You already defined column '#{name}'." if self[name] + column = ColumnDefinition.new(@base, name, type) column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] column.precision = options[:precision] column.scale = options[:scale] diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index d1e2059c9a..fa765a7017 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -807,6 +807,22 @@ if ActiveRecord::Base.connection.supports_migrations? end end + def test_should_disallow_duplicate_column_definition + assert_raises(ActiveRecord::StatementInvalid) do + Person.connection.add_column("people", "full_name", :string, :limit => 40) + Person.connection.add_column("people", "full_name", :text) + end + + assert_raises(RuntimeError) do + Person.connection.create_table :people_with_errors do |t| + t.column "full_name", :string, :limit => 40 + t.column "full_name", :text + end + end + + Person.reset_column_information + end + end end -- cgit v1.2.3