-
Notifications
You must be signed in to change notification settings - Fork 386
Adding Native DB Types
If your database connection adapter doesn't support a datatype you need by default, you can try overriding the default datatype definitions with your own.
First, check to see what datatypes are already being extended in your adapter. For example, MySQL has it's adapter code as lib/arjdbc/mysql/adapter.rb, you'll find a NATIVE_DATABASE_TYPES
constant and method definition there.
In this case you can either update the NATIVE_DATABASE_TYPES
exported hash's content or simply monkey patch the updates in the native_database_types
methods, be aware that if it's not re-defined in an adapter you'll need to also look for a modify_types
method.
Sample crazy (use at your own risk) patch to support unsigned 64-bit integers (for both the primary key and other columns) and large text fields, one could do the following :
require 'arjdbc/mysql'
module ArJdbc
module MySQL
# @override
def native_database_types
NATIVE_DATABASE_TYPES.update({
:primary_key => "bigint unsigned DEFAULT NULL auto_increment PRIMARY KEY",
:decimal => { :name => "decimal" },
:longtext => { :name => "longtext" },
:ubigint => { :name => "bigint unsigned" },
})
end
end
end
with this in place, one can create a migration using the "new" datatypes :
class CreateWidgets < ActiveRecord::Migration
def self.up
create_table :widgets do |t|
# t.column :name, :string
t.column :footext, :longtext
t.column :barnum, :ubigint
t.column :boringnormal, :string
end
end
def self.down
drop_table :widgets
end
end
and after running rake db:migrate
, here's the table in the database :
mysql> describe widgets;
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| footext | longtext | YES | | NULL | |
| barnum | bigint(20) unsigned | YES | | NULL | |
| boringnormal | varchar(255) | YES | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)