Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rails 4 Support (Updated, w/ smdern's patches) + Ms SqlServer Support #37

Open
wants to merge 11 commits into from

4 participants

@D1plo1d

This patch includes smderns patches as well as my own. The uuid column type no longer collides with the Rails 4 uuid column type when it is defined in rails 4 postgres. This is only a partial fix however since rails 4 postgres uuid columns expect SecureRandom.uuid. IMO we should consider switching to that for compatibility.

MS SqlServer support is also included.

@brendandc brendandc referenced this pull request in idibon/activeuuid
Merged

Add support for ActiveRecord 4.x #1

@jashmenn
Owner

See #41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 20, 2013
  1. @smdern

    Update .rvmrc to ruby 2.0

    smdern authored
  2. @smdern
  3. @smdern
  4. @smdern

    Fix ActiveRecord specs

    smdern authored
Commits on Feb 3, 2014
  1. @D1plo1d
  2. @D1plo1d

    Merging smdern's patch

    D1plo1d authored
  3. @D1plo1d
Commits on Feb 10, 2014
  1. @D1plo1d

    Adding checks for rails 4 to postgres support. Rails 4 already includ…

    D1plo1d authored
    …es a uuid column so we should not attempt to override it.
  2. @D1plo1d
Commits on Feb 22, 2014
  1. @mahemoff

    Update patches.rb

    mahemoff authored
Commits on May 26, 2014
  1. @D1plo1d
This page is out of date. Refresh to see the latest.
View
2  .rvmrc
@@ -1 +1 @@
-rvm use 1.9.3@activeuuid --create
+rvm use 2.0.0@activeuuid --create
View
2  Gemfile
@@ -2,5 +2,3 @@ source "http://rubygems.org"
# Specify your gem's dependencies in activeuuid.gemspec
gemspec
-
-gem "activerecord"
View
5 activeuuid.gemspec
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
- s.add_development_dependency "activesupport"
+ s.add_development_dependency "activesupport", ">= 4.0"
s.add_development_dependency "database_cleaner"
s.add_development_dependency "forgery"
s.add_development_dependency "fabrication"
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "pg"
s.add_development_dependency "mysql2"
+ s.add_dependency "activerecord", ">= 4.0"
+
s.add_runtime_dependency "uuidtools"
- s.add_runtime_dependency "activerecord", '>= 3.1'
end
View
16 lib/activeuuid/patches.rb
@@ -8,7 +8,11 @@ module Migrations
def uuid(*column_names)
options = column_names.extract_options!
column_names.each do |name|
- type = @base.adapter_name.downcase == 'postgresql' ? 'uuid' : 'binary(16)'
+ type = case ActiveRecord::Base.connection.adapter_name.downcase
+ when 'postgresql' then 'uuid'
+ when 'sqlserver' then 'uniqueidentifier'
+ else 'binary(16)'
+ end
column(name, "#{type}#{' PRIMARY KEY' if options.delete(:primary_key)}", options)
end
end
@@ -34,7 +38,6 @@ def simplified_type_with_uuid(field_type)
end
alias_method_chain :type_cast, :uuid
- alias_method_chain :type_cast_code, :uuid
alias_method_chain :simplified_type, :uuid
end
end
@@ -103,15 +106,18 @@ def native_database_types_with_pguuid
end
def self.apply!
+ is_pg = defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
+ has_uuid_type = is_pg and Rails.version[0].to_i >= 4
+
ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionAdapters::Column.send :include, Column
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
+ ActiveRecord::ConnectionAdapters::Column.send :include, Column unless has_uuid_type
+ ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if is_pg and !has_uuid_type
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
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgreSQLQuoting if is_pg and !has_uuid_type
end
end
end
View
18 lib/activeuuid/uuid.rb
@@ -54,26 +54,26 @@ def self.parse_string(str)
module Arel
module Visitors
class DepthFirst < Arel::Visitors::Visitor
- def visit_UUIDTools_UUID(o)
- o.quoted_id
+ def visit_UUIDTools_UUID(object, attribute)
+ object.quoted_id
end
end
class MySQL < Arel::Visitors::ToSql
- def visit_UUIDTools_UUID(o)
- o.quoted_id
+ def visit_UUIDTools_UUID(object, attribute)
+ object.quoted_id
end
end
class SQLite < Arel::Visitors::ToSql
- def visit_UUIDTools_UUID(o)
- o.quoted_id
+ def visit_UUIDTools_UUID(object, attribute)
+ object.quoted_id
end
end
class PostgreSQL < Arel::Visitors::ToSql
- def visit_UUIDTools_UUID(o)
- "'#{o.to_s}'"
+ def visit_UUIDTools_UUID(object, attribute)
+ "'#{object.to_s}'"
end
end
end
@@ -114,7 +114,7 @@ def uuids(*attributes)
EOS
end
- def instantiate_with_uuid(record)
+ def instantiate_with_uuid(record, column_types)
uuid_columns.each do |uuid_column|
record[uuid_column] = UUIDTools::UUID.serialize(record[uuid_column]).to_s if record[uuid_column]
end
View
4 spec/lib/activerecord_spec.rb
@@ -30,6 +30,7 @@
adapters.sqlite3 { column.sql_type.should == 'binary(16)' }
adapters.mysql2 { column.sql_type.should == 'binary(16)' }
adapters.postgresql { column.sql_type.should == 'uuid' }
+ adapters.sqlserver { column.sql_type.should == 'uniqueidentifier' }
end
end
end
@@ -43,7 +44,7 @@
spec_for_adapter do |adapters|
adapters.sqlite3 { connection.change_column table_name, column_name, :uuid }
adapters.mysql2 { connection.change_column table_name, column_name, :uuid }
- # adapters.postgresql { connection.change_column table_name, column_name, :uuid }
+ adapters.postgresql { connection.change_column table_name, column_name, :uuid }
end
end
@@ -52,6 +53,7 @@
adapters.sqlite3 { column.sql_type.should == 'binary(16)' }
adapters.mysql2 { column.sql_type.should == 'binary(16)' }
adapters.postgresql { pending('postgresql can`t change column type to uuid') }
+ adapters.sqlserver { pending('sqlserver can`t change column type to uuid') }
end
end
end
View
2  spec/support/spec_for_adapter.rb
@@ -6,7 +6,7 @@ def initialize
@specs = {}
end
- [:sqlite3, :mysql2, :postgresql].each do |name|
+ [:sqlite3, :mysql2, :postgresql, :sqlserver].each do |name|
send :define_method, name do |&block|
@specs[name] = block
end
Something went wrong with that request. Please try again.