Permalink
Browse files

Merge pull request #19 from oliamb/add_column

Support for connection.add_column and connection.change_column
  • Loading branch information...
2 parents 9208f01 + 3cb96f2 commit 71aef8379021e4c676aa63103594f29d59d6b84c @devinfoley devinfoley committed Nov 7, 2012
Showing with 101 additions and 0 deletions.
  1. +11 −0 lib/activeuuid/patches.rb
  2. +64 −0 spec/lib/activerecord_spec.rb
  3. +6 −0 spec/spec_helper.rb
  4. +20 −0 spec/support/spec_for_adapter.rb
@@ -1,3 +1,7 @@
+require 'active_record'
+require 'active_support/concern'
+
+
module ActiveUUID
module Patches
module Migrations
@@ -92,6 +96,12 @@ 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
@@ -103,6 +113,7 @@ def self.apply!
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
@@ -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 }
View
@@ -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
@@ -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 71aef83

Please sign in to comment.