Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d45f7c4
Perform monkey-patching after establishing connections
inbeom Oct 8, 2016
173e3ac
Add Travis test environments for Rails 5
inbeom Oct 8, 2016
3888d66
Remove deprecation warnings from table_exists?
inbeom Oct 8, 2016
d58de84
Override type_to_sql only of Mysql adapter
inbeom Oct 8, 2016
4b4d15e
Remove deprecation warning of alias_method_chain
inbeom Oct 8, 2016
93cfd6d
Discontinue supporting ruby 1.9.3
inbeom Oct 8, 2016
631c9e7
Remove a duplicate entry
inbeom Oct 8, 2016
9d99859
Perform all monkey-patching on connection establishments
inbeom Oct 9, 2016
c99796a
Refine Travis test environments
inbeom Oct 10, 2016
5db8273
Resolve double serialization issue in rails 4.2-postgresql
inbeom Oct 10, 2016
30a7549
Override postgresql type casting scheme in Rails 5
inbeom Oct 10, 2016
33b94fe
Support rails <= 4.1 where #data_source_exists? does not exist
inbeom Oct 10, 2016
529a0d1
Resolve activerecord-mysql2 version conflict
inbeom Oct 10, 2016
2ba2f83
Skip unnecessary tests against rails-head
inbeom Oct 10, 2016
ac82300
Resolve primary key constraint issue on mysql >= 5.7
inbeom Oct 10, 2016
07a5d5e
Merge pull request #1 from inbeom/rails5
inbeom Jan 14, 2017
72e1702
Update rbx to the latest version
inbeom Jan 14, 2017
bf5a6c2
Remove EOL-ed ruby 2.0.0
inbeom Jan 14, 2017
c58699a
Remove EOL-ed rails 3.1 and 3.2
inbeom Jan 14, 2017
a990651
Bump up runtime activerecord dependency
inbeom Jan 14, 2017
c8f871b
Refine jruby dependencies
inbeom Jan 14, 2017
3a3038d
Set rubies on CI to the latest releases
inbeom Jan 14, 2017
1ff86a7
Remove duplicate exclusion list
inbeom Jan 14, 2017
d0fcd14
Update RSpec
inbeom Jan 14, 2017
4a3a148
Exclude all ruby-2.1.10 tests against the primary Gemfile
inbeom Jan 14, 2017
3ff0a87
Fix specs to run again
inbeom Jan 14, 2017
19b48e6
Update rbx, jruby to travis-supported latest versions
inbeom Jan 14, 2017
abf9650
Add ruby 2.4.0
inbeom Jan 14, 2017
b8377cb
Change pending to skip and remove a commented out line
inbeom Jan 14, 2017
b94e225
Prepare active maintenance
inbeom Jan 14, 2017
8d5455d
Specify supported rails versions correctly
inbeom Jan 14, 2017
6312c41
Avoid testing on ruby-2.4.0 against rails 4.x
inbeom Jan 14, 2017
a7b16d4
Remove rbx and change jruby version
inbeom Jan 14, 2017
0be3404
Specify ruby platforms for legacy mysql gems
inbeom Jan 14, 2017
d5f6845
Ignore the default Gemfile for JRuby
inbeom Jan 14, 2017
07f7a8a
Allow CI failures w/ Rails 4.0 and 4.1 under JRuby
inbeom Jan 14, 2017
4d5609a
Merge pull request #3 from inbeom/drop-legacy-support
inbeom Jan 15, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ before_script:
- psql -c 'create database activeuuid_test;' -U postgres

rvm:
- 1.9.3
- 2.0.0
- 2.1.5
- 2.2.0
- rbx-2
- jruby
- 2.1.10
- 2.2.6
- 2.3.3
- 2.4.0
- jruby-9.1.5.0

gemfile:
- Gemfile
- gemfiles/Gemfile.rails-3-1
- gemfiles/Gemfile.rails-3-2
- gemfiles/Gemfile.rails-4-0
- gemfiles/Gemfile.rails-4-1
- gemfiles/Gemfile.rails-4-2
- gemfiles/Gemfile.rails-5-0
- gemfiles/Gemfile.rails-head

env:
Expand All @@ -27,21 +25,22 @@ env:
matrix:
allow_failures:
- gemfile: gemfiles/Gemfile.rails-head
- rvm: jruby-9.1.5.0
gemfile: gemfiles/Gemfile.rails-4-0
- rvm: jruby-9.1.5.0
gemfile: gemfiles/Gemfile.rails-4-1
exclude:
- rvm: 2.2.0
gemfile: gemfiles/Gemfile.rails-3-1
- rvm: 2.2.0
gemfile: gemfiles/Gemfile.rails-3-2
- rvm: jruby
gemfile: gemfiles/Gemfile.rails-3-1
env: DB=postgresql
- rvm: jruby
gemfile: gemfiles/Gemfile.rails-3-2
env: DB=postgresql
- rvm: jruby
- rvm: 2.1.10
gemfile: Gemfile
- rvm: 2.1.10
gemfile: gemfiles/Gemfile.rails-5-0
- rvm: 2.4.0
gemfile: gemfiles/Gemfile.rails-4-0
env: DB=postgresql
- rvm: jruby
- rvm: 2.4.0
gemfile: gemfiles/Gemfile.rails-4-1
env: DB=postgresql

- rvm: 2.4.0
gemfile: gemfiles/Gemfile.rails-4-2
- rvm: jruby-9.1.5.0
gemfile: Gemfile
- rvm: jruby-9.1.5.0
gemfile: gemfiles/Gemfile.rails-5-0
6 changes: 3 additions & 3 deletions README.mkd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/jashmenn/activeuuid.png)](http://travis-ci.org/jashmenn/activeuuid)
[![Build Status](https://travis-ci.org/inbeom/activeuuid.png)](http://travis-ci.org/inbeom/activeuuid)

# activeuuid

Expand Down Expand Up @@ -131,7 +131,7 @@ Add this to your `Gemfile`

gem "activeuuid"

Or get the code here: https://github.com/jashmenn/activeuuid
Or get the code here: https://github.com/inbeom/activeuuid


## References
Expand All @@ -144,7 +144,7 @@ Or get the code here: https://github.com/jashmenn/activeuuid
* [7] http://tools.ietf.org/html/rfc4122.html#appendix-C

## Dependencies
Rails ~> 3.1.0
Rails >= 4.0.0

## Authors

Expand Down
4 changes: 2 additions & 2 deletions activeuuid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 2.99.0"
s.add_development_dependency "rspec", "~> 3.5.0"
s.add_development_dependency "rspec-its"
s.add_development_dependency "activesupport"
s.add_development_dependency "database_cleaner"
Expand All @@ -35,5 +35,5 @@ Gem::Specification.new do |s|
end

s.add_runtime_dependency "uuidtools"
s.add_runtime_dependency "activerecord", '>= 3.1'
s.add_runtime_dependency "activerecord", '>= 4.0'
end
5 changes: 0 additions & 5 deletions gemfiles/Gemfile.rails-3-2

This file was deleted.

1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-4-0
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ source "http://rubygems.org"

gemspec path: '../'

gem 'mysql2', '~> 0.3.21', '~> 0.3.21', platforms: [:mri, :rbx]
gem 'activerecord', '~> 4.0.0'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-4-1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ source "http://rubygems.org"

gemspec path: '../'

gem 'mysql2', '~> 0.3.21', platforms: [:mri, :rbx]
gem 'activerecord', '~> 4.1.0'
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails-3-1 → gemfiles/Gemfile.rails-5-0
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source "http://rubygems.org"

gemspec path: '../'

gem 'activerecord', '~> 3.1.0'
gem 'activerecord', '~> 5.0.0'
79 changes: 54 additions & 25 deletions lib/activeuuid/patches.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require 'active_record'
require 'active_support/concern'

if ActiveRecord::VERSION::MAJOR == 4 and ActiveRecord::VERSION::MINOR == 2
if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) ||
(ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0)
module ActiveRecord
module Type
class UUID < Binary # :nodoc:
def type
:uuid
end

def serialize(value)
return if value.nil?
UUIDTools::UUID.serialize(value)
end

def cast_value(value)
UUIDTools::UUID.serialize(value)
end
Expand All @@ -24,7 +30,6 @@ class Uuid < Type::Value # :nodoc:
def type_cast_from_user(value)
UUIDTools::UUID.serialize(value) if value
end
alias_method :type_cast_from_database, :type_cast_from_user
end
end
end
Expand Down Expand Up @@ -164,36 +169,60 @@ def native_database_types_with_pguuid
end
end

module AbstractAdapter
extend ActiveSupport::Concern
module PostgresqlTypeOverride
def deserialize(value)
UUIDTools::UUID.serialize(value) if value
end

included do
def initialize_type_map_with_uuid(m)
initialize_type_map_without_uuid(m)
register_class_with_limit m, /binary\(16(,0)?\)/i, ::ActiveRecord::Type::UUID
end
alias_method :cast, :deserialize
end

module TypeMapOverride
def initialize_type_map(m)
super

alias_method_chain :initialize_type_map, :uuid
register_class_with_limit m, /binary\(16(,0)?\)/i, ::ActiveRecord::Type::UUID
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

if ActiveRecord::VERSION::MAJOR == 4 and ActiveRecord::VERSION::MINOR == 2
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
else
ActiveRecord::ConnectionAdapters::Column.send :include, Column
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
module MysqlTypeToSqlOverride
def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil)
type.to_s == 'uuid' ? 'binary(16)' : super
end
ArJdbc::MySQL::Column.send :include, MysqlJdbcColumn if defined? ArJdbc::MySQL::Column
end

ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
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
module ConnectionHandling
def establish_connection(_ = nil)
super

ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition

if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
if defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend TypeMapOverride
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend MysqlTypeToSqlOverride
end

ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid.prepend PostgresqlTypeOverride if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
elsif ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2
ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
else
ActiveRecord::ConnectionAdapters::Column.send :include, Column
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
end

ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
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
end
end

def self.apply!
ActiveRecord::Base.singleton_class.prepend ConnectionHandling
end
end
end
15 changes: 9 additions & 6 deletions lib/activeuuid/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module UUID
class_attribute :_uuid_generator, instance_writer: false
self._uuid_generator = :random

singleton_class.alias_method_chain :instantiate, :uuid
singleton_class.prepend Instantiation
before_create :generate_uuids_if_needed
end

Expand All @@ -128,15 +128,18 @@ def uuids(*attributes)
EOS
end

def instantiate_with_uuid(record, record_models = nil)
def uuid_columns
@uuid_columns ||= columns.select { |c| c.type == :uuid }.map(&:name)
end
end

module Instantiation
def instantiate(record, record_models = nil)
uuid_columns.each do |uuid_column|
record[uuid_column] = UUIDTools::UUID.serialize(record[uuid_column]).to_s if record[uuid_column]
end
instantiate_without_uuid(record)
end

def uuid_columns
@uuid_columns ||= columns.select { |c| c.type == :uuid }.map(&:name)
super(record)
end
end

Expand Down
16 changes: 10 additions & 6 deletions spec/lib/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

describe ActiveRecord::Base do
context '.connection' do
def table_exists?(connection, table_name)
connection.respond_to?(:data_source_exists?) ?
connection.data_source_exists?(table_name) :
connection.table_exists?(table_name)
end

let!(:connection) { ActiveRecord::Base.connection }
let(:table_name) { :test_uuid_field_creation }

before do
connection.drop_table(table_name) if connection.table_exists?(table_name)
connection.drop_table(table_name) if table_exists?(connection, table_name)
connection.create_table(table_name)
end

after do
connection.drop_table table_name
end

specify { connection.table_exists?(table_name).should be_truthy }
specify { table_exists?(connection, table_name).should be_truthy }

context '#add_column' do
let(:column_name) { :uuid_column }
Expand Down Expand Up @@ -43,15 +49,14 @@
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 }
end
end

it 'support changing type from string to uuid' do
spec_for_adapter do |adapters|
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.postgresql { skip('postgresql can`t change column type to uuid') }
end
end
end
Expand Down Expand Up @@ -105,7 +110,7 @@
describe UuidArticle do
let!(:article) { Fabricate :uuid_article }
let!(:id) { article.id }
let(:model) { described_class }
let(:model) { UuidArticle }
subject { model }

context 'model' do
Expand Down Expand Up @@ -218,4 +223,3 @@
its(:id) { should == uuid }
end
end

15 changes: 14 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@

ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.configurations = YAML::load(File.read(File.dirname(__FILE__) + "/support/database.yml"))
ActiveRecord::Base.establish_connection((ENV["DB"] || "sqlite3").to_sym)

require 'activeuuid'

ActiveRecord::Base.establish_connection((ENV["DB"] || "sqlite3").to_sym)

if ENV['DB'] == 'mysql'
if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR <= 1
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
elsif ActiveRecord::VERSION::MAJOR == 3
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
end
end

ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + "/support/migrate")
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, STDOUT)

Expand Down