From 906ff07e71eade42ac3856591faea5ffe857b44f Mon Sep 17 00:00:00 2001
From: "travis.h.oneill@gmail.com" <travis.h.oneill@gmail.com>
Date: Wed, 17 Aug 2016 17:21:54 -0700
Subject: Added nil case handling to allow rollback migration in case of
 invalid column type     /activerecord/lib/active_record/connection_adapters  
   /abstract/schema_definitions.rb:306     type = type.to_sym

Changed to the following to handle nil case:
    type = type.to_sym if type

Added regression test for this case:
  /activerecord/test/cases/migration_test.rb:554
  if current_adapter?(:SQLite3Adapter)
    def test_allows_sqlite3_rollback_on_invalid_column_type
      Person.connection.create_table :something, force: true do |t|
        t.column :number, :integer
        t.column :name, :string
        t.column :foo, :bar
      end
      assert Person.connection.column_exists?(:something, :foo)
      assert_nothing_raised { Person.connection.remove_column :something, :foo, :bar }
      assert !Person.connection.column_exists?(:something, :foo)
      assert Person.connection.column_exists?(:something, :name)
      assert Person.connection.column_exists?(:something, :number)
    ensure
      Person.connection.drop_table :something, if_exists: true
    end
  end
---
 activerecord/CHANGELOG.md                               | 14 +++++++++++---
 .../connection_adapters/abstract/schema_definitions.rb  |  2 +-
 .../connection_adapters/abstract/schema_statements.rb   |  3 ++-
 activerecord/test/cases/migration_test.rb               | 17 +++++++++++++++++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 6b996fd6bd..1f6719972f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,11 @@
+*   Sqlite3 migrations to add a column to an existing table can now be
+    successfully rolled back when the column was given and invalid column
+    type.
+
+    Fixes #26087
+
+    *Travis O'Neill*
+
 *   Doing count on relations that contain LEFT OUTER JOIN Arel node no longer
     force a DISTINCT. This solves issues when using count after a left_joins.
 
@@ -57,8 +65,8 @@
     *Xavier Noria*
 
 *   Using `group` with an attribute that has a custom type will properly cast
-    the hash keys after calling a calculation method like `count`. 
-    
+    the hash keys after calling a calculation method like `count`.
+
     Fixes #25595.
 
     *Sean Griffin*
@@ -93,7 +101,7 @@
     *Sean Griffin*
 
 *   Ensure hashes can be assigned to attributes created using `composed_of`.
-    
+
     Fixes #25210.
 
     *Sean Griffin*
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 d9a799676f..ffde4f2c93 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -303,7 +303,7 @@ module ActiveRecord
       #   end
       def column(name, type, options = {})
         name = name.to_s
-        type = type.to_sym
+        type = type.to_sym if type
         options = options.dup
 
         if @columns_hash[name] && @columns_hash[name].primary_key?
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index afa0860707..45d782e45e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -1047,7 +1047,8 @@ module ActiveRecord
       end
 
       def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
-        if native = native_database_types[type.to_sym]
+        type = type.to_sym if type
+        if native = native_database_types[type]
           column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
 
           if type == :decimal # ignore limit, use precision and scale
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 76a4592ac5..151f3c8efd 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -551,6 +551,23 @@ class MigrationTest < ActiveRecord::TestCase
     end
   end
 
+  if current_adapter?(:SQLite3Adapter)
+    def test_allows_sqlite3_rollback_on_invalid_column_type
+      Person.connection.create_table :something, force: true do |t|
+        t.column :number, :integer
+        t.column :name, :string
+        t.column :foo, :bar
+      end
+      assert Person.connection.column_exists?(:something, :foo)
+      assert_nothing_raised { Person.connection.remove_column :something, :foo, :bar }
+      assert !Person.connection.column_exists?(:something, :foo)
+      assert Person.connection.column_exists?(:something, :name)
+      assert Person.connection.column_exists?(:something, :number)
+    ensure
+      Person.connection.drop_table :something, if_exists: true
+    end
+  end
+
   if current_adapter? :OracleAdapter
     def test_create_table_with_custom_sequence_name
       # table name is 29 chars, the standard sequence name will
-- 
cgit v1.2.3