Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for connection.add_column and connection.change_column #19

Merged
merged 1 commit into from

4 participants

@oliamb

This patch make it possible to use 'add_column' and 'change_column' in migration by defining a uuid column type in MySQL abstract adapter.

As a side change, I added an helper to use when the spec are not the same on every adapter. Hope it helps.

Might fix issue #4

@oliamb oliamb Added a uuid type in mysql abstract adapter.
Let use conn.add_column and conn.change_column in migration.
3cb96f2
@pyromaniac

.select {|c| c.name.to_sym == :binary_col}).first can be replaced with .detect {|c| c.name.to_sym == :binary_col})

Thanks for the tip. I was not aware of this shortcut.

@jashmenn
Owner

nice. i'll try to pull this later today

@devinfoley devinfoley merged commit 71aef83 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 7, 2012
  1. @oliamb

    Added a uuid type in mysql abstract adapter.

    oliamb authored
    Let use conn.add_column and conn.change_column in migration.
This page is out of date. Refresh to see the latest.
View
11 lib/activeuuid/patches.rb
@@ -1,3 +1,7 @@
+require 'active_record'
+require 'active_support/concern'
+
+
module ActiveUUID
module Patches
module Migrations
@@ -48,6 +52,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
@@ -55,6 +65,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
View
64 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 }
View
6 spec/spec_helper.rb
@@ -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
View
20 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
Something went wrong with that request. Please try again.