Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7742 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Allow Native SQL Types in add_column

Reported by: Theory Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: 1.2.2
Severity: normal Keywords: sql, "native type", column, test
Cc: tomafro, jsgarvin

Description

I am able to create new tables with native types in migrations like this (note the use of :smallint):

create_table :foo, :force => true do |t|
  t.column :bar, :smallint
end

But I can't add a column with a native type:

add_column :foo, :smallint

The attached patch fixes this issue. Without the whitespace changes, it's simply:

       def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
-        native = native_database_types[type]
+        if native = native_database_types[type]
         column_type_sql = native.is_a?(Hash) ? native[:name] : native
         if type == :decimal # ignore limit, use precison and scale
           precision ||= native[:precision]
@@ -273,6 +273,9 @@
           column_type_sql << "(#{limit})" if limit
           column_type_sql
         end
+        else
+          column_type_sql = type
+        end
       end
 
       def add_column_options!(sql, options) #:nodoc:

Thanks!

Attachments

add_column_native_type.patch (1.9 kB) - added by Theory on 03/06/07 22:37:06.
Patch to support native SQL types in add_column.
add_column_native_type_with_test.patch (2.9 kB) - added by jsgarvin on 05/21/07 06:48:19.

Change History

03/06/07 22:37:06 changed by Theory

  • attachment add_column_native_type.patch added.

Patch to support native SQL types in add_column.

03/06/07 23:28:30 changed by tomafro

  • cc set to tomafro.
  • status changed from new to closed.
  • resolution set to untested.
  • summary changed from Patch: Allow Native SQL Types to [Patch] Allow Native SQL Types in add_column.

Looks pretty reasonable to me, but please add some unit tests to show it works. Also, please make it clear which database adapter you expect your tests to work against, as some may override type_to_sql. Any tests you attach will help catch this same issue across these adapters too.

05/21/07 06:48:19 changed by jsgarvin

  • attachment add_column_native_type_with_test.patch added.

05/21/07 06:53:25 changed by jsgarvin

  • cc changed from tomafro to tomafro, jsgarvin.

I just submitted a patch with Theory's fix that includes a test for MysqlAdapter. Without the fix, the test causes type_to_sql to throw an error. With the fix, the test passes.

Thanks to Josh Susser for an inspiring presentation at RailsConf earlier today that lead to this (my first) patch to Rails Source.

05/22/07 05:30:28 changed by jsgarvin

  • keywords changed from sql, "native type", column to sql, "native type", column, test.
  • status changed from closed to reopened.
  • resolution deleted.

05/24/07 01:08:16 changed by josh

  • summary changed from [Patch] Allow Native SQL Types in add_column to [PATCH] Allow Native SQL Types in add_column.

05/25/07 21:21:45 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6842]) Migrations: add_column supports custom column types. Closes #7742. First-patch cheers to jsgarvin\!