Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a uuid type in mysql abstract adapter.
Let use conn.add_column and conn.change_column in migration.
  • Loading branch information
oliamb committed Nov 7, 2012
1 parent 66c7bfb commit 3cb96f2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/activeuuid/patches.rb
@@ -1,3 +1,7 @@
require 'active_record'
require 'active_support/concern'


module ActiveUUID
module Patches
module Migrations
Expand Down Expand Up @@ -48,13 +52,20 @@ def type_cast_with_visiting(value, column = nil)
alias_method_chain :type_cast, :visiting
end
end

def self.update_native_database_types
if defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:uuid] = { :name => 'binary', :limit => 16 }
end
end

def self.apply!
ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
self.update_native_database_types
end
end
end
64 changes: 64 additions & 0 deletions spec/lib/activerecord_spec.rb
@@ -1,5 +1,69 @@
require 'spec_helper'

describe ActiveRecord::Base do
context '.connection' do
let!(:connection) { ActiveRecord::Base.connection }
let(:table_name) { :test_uuid_field_creation }

before :each do
connection.create_table table_name
connection.table_exists?(table_name).should be_true
end

after :each do
connection.drop_table table_name
end

context '#add_column' do
it 'support adding uuid column' do
connection.add_column table_name, :uuid_col, :uuid
connection.column_exists?(table_name, :uuid_col).should be_true
columns = connection.columns(table_name)
col = (columns.select {|c| c.name.to_sym == :uuid_col }).first
col.should_not be_nil

spec_for_adapter do |adapters|
adapters.sqlite { col.sql_type.should == 'uuid' }
adapters.mysql2 do
col.sql_type.should == 'binary(16)'
col.type.should == :binary
col.respond_to? :string_to_binary
end
end
end
end

context '#change_column' do
before :each do
connection.add_column table_name, :binary_col, :binary, :limit => 16
end

it 'support changing type from binary to uuid' do
col = (connection.columns(table_name).select {|c| c.name.to_sym == :binary_col}).first
col.should_not be_nil
spec_for_adapter do |adapters|
adapters.mysql2 do
col.type.should == :binary
col.sql_type.should == 'tinyblob'
end
end

connection.change_column table_name, :binary_col, :uuid

col = (connection.columns(table_name).select {|c| c.name.to_sym == :binary_col}).first
col.should_not be_nil
spec_for_adapter do |adapters|
adapters.mysql2 do
col.type.should == :binary
col.sql_type.should == 'binary(16)'
end
end
end
end

end
end

describe Article do
let!(:article) { Fabricate :article }
let(:id) { article.id }
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -38,4 +38,10 @@
config.after(:each) do
DatabaseCleaner.clean
end

def spec_for_adapter(&block)
switcher = ActiveUUID::SpecSupport::SpecForAdapter.new()
yield switcher
switcher.run(connection)
end
end
20 changes: 20 additions & 0 deletions spec/support/spec_for_adapter.rb
@@ -0,0 +1,20 @@
require 'activeuuid'

module ActiveUUID::SpecSupport
class SpecForAdapter
def initialize
@specs = {}
end

[:sqlite, :mysql2, :postgres].each do |name|
send :define_method, name do |&block|
@specs[name] = block
end
end

def run(connection)
name = connection.adapter_name.downcase.to_sym
@specs[name].call() if(@specs.include? name)
end
end
end

0 comments on commit 3cb96f2

Please sign in to comment.