Skip to content
Browse files

Merge pull request #20 from pyromaniac/master

Migration and specs fixes
  • Loading branch information...
2 parents 71aef83 + acdb010 commit 423bc583b6e76f57ce105793e6bd069b2a5886c7 @jashmenn committed
Showing with 99 additions and 87 deletions.
  1. +29 −27 lib/activeuuid/patches.rb
  2. +2 −2 lib/activeuuid/uuid.rb
  3. +63 −54 spec/lib/activerecord_spec.rb
  4. +2 −1 spec/spec_helper.rb
  5. +3 −3 spec/support/spec_for_adapter.rb
View
56 lib/activeuuid/patches.rb
@@ -18,28 +18,24 @@ module Column
extend ActiveSupport::Concern
included do
- def uuid?
- sql_type == 'binary(16)'
- end
-
def type_cast_with_uuid(value)
- if uuid?
- UUIDTools::UUID.serialize(value)
- else
- type_cast_without_uuid(value)
- end
+ return UUIDTools::UUID.serialize(value) if type == :uuid
+ type_cast_without_uuid(value)
end
def type_cast_code_with_uuid(var_name)
- if uuid?
- "UUIDTools::UUID.serialize(#{var_name})"
- else
- type_cast_code_without_uuid(var_name)
- end
+ return "UUIDTools::UUID.serialize(#{var_name})" if type == :uuid
+ type_cast_code_without_uuid(var_name)
+ end
+
+ def simplified_type_with_uuid(field_type)
+ return :uuid if field_type == 'binary(16)'
+ simplified_type_without_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
@@ -47,9 +43,12 @@ module PostgreSQLColumn
extend ActiveSupport::Concern
included do
- def uuid?
- sql_type == 'uuid'
+ def simplified_type_with_pguuid(field_type)
+ return :uuid if field_type == 'uuid'
+ simplified_type_without_pguuid(field_type)
end
+
+ alias_method_chain :simplified_type, :pguuid
end
end
@@ -58,17 +57,22 @@ module Quoting
included do
def quote_with_visiting(value, column = nil)
- value = UUIDTools::UUID.serialize(value) if column && column.uuid?
+ value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid
quote_without_visiting(value, column)
end
def type_cast_with_visiting(value, column = nil)
- value = UUIDTools::UUID.serialize(value) if column && column.uuid?
+ value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid
type_cast_without_visiting(value, column)
end
+ def native_database_types_with_uuid
+ @native_database_types ||= native_database_types_without_uuid.merge(uuid: { name: 'binary', limit: 16 })
+ end
+
alias_method_chain :quote, :visiting
alias_method_chain :type_cast, :visiting
+ alias_method_chain :native_database_types, :uuid
end
end
@@ -77,7 +81,7 @@ module PostgreSQLQuoting
included do
def quote_with_visiting(value, column = nil)
- if column && column.uuid?
+ if column && column.type == :uuid
value = UUIDTools::UUID.serialize(value)
value = value.to_s if value.is_a? UUIDTools::UUID
end
@@ -85,21 +89,20 @@ def quote_with_visiting(value, column = nil)
end
def type_cast_with_visiting(value, column = nil)
- if column && column.uuid?
+ if column && column.type == :uuid
value = UUIDTools::UUID.serialize(value)
value = value.to_s if value.is_a? UUIDTools::UUID
end
type_cast_without_visiting(value, column)
end
+ def native_database_types_with_pguuid
+ @native_database_types ||= native_database_types_without_pguuid.merge(uuid: { name: 'uuid' })
+ end
+
alias_method_chain :quote, :visiting
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 }
+ alias_method_chain :native_database_types, :pguuid
end
end
@@ -113,7 +116,6 @@ 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
4 lib/activeuuid/uuid.rb
@@ -112,7 +112,7 @@ def instantiate_with_uuid(record)
end
def uuid_columns
- @uuid_columns ||= columns.select(&:uuid?).map(&:name)
+ @uuid_columns ||= columns.select { |c| c.type == :uuid }.map(&:name)
end
end
@@ -133,7 +133,7 @@ def create_uuid
def generate_uuids_if_needed
primary_key = self.class.primary_key
- if self.class.columns_hash[primary_key].uuid?
+ if self.class.columns_hash[primary_key].type == :uuid
send("#{primary_key}=", create_uuid) unless send("#{primary_key}?")
end
end
View
117 spec/lib/activerecord_spec.rb
@@ -4,63 +4,58 @@
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
+
+ before do
+ connection.drop_table(table_name) if connection.table_exists?(table_name)
+ connection.create_table(table_name)
end
-
- after :each do
+
+ after do
connection.drop_table table_name
end
-
+
+ specify { connection.table_exists?(table_name).should be_true }
+
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
-
+ let(:column_name) { :uuid_column }
+ let(:column) { connection.columns(table_name).detect { |c| c.name.to_sym == column_name } }
+
+ before { connection.add_column table_name, column_name, :uuid }
+
+ specify { connection.column_exists?(table_name, column_name).should be_true }
+ specify { column.should_not be_nil }
+
+ it 'should have proper sql type' do
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
+ adapters.sqlite3 { column.sql_type.should == 'binary(16)' }
+ adapters.mysql2 { column.sql_type.should == 'binary(16)' }
+ adapters.postgresql { column.sql_type.should == 'uuid' }
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
+ let(:column_name) { :string_col }
+ let(:column) { connection.columns(table_name).detect { |c| c.name.to_sym == column_name } }
+
+ before do
+ connection.add_column table_name, column_name, :string
spec_for_adapter do |adapters|
- adapters.mysql2 do
- col.type.should == :binary
- col.sql_type.should == 'tinyblob'
- end
+ 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
-
- 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
+ end
+
+ it 'support changing type from string to uuid' do
spec_for_adapter do |adapters|
- adapters.mysql2 do
- col.type.should == :binary
- col.sql_type.should == 'binary(16)'
- end
+ 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') }
end
end
end
-
+
end
end
@@ -68,13 +63,16 @@
let!(:article) { Fabricate :article }
let(:id) { article.id }
let(:model) { Article }
+ subject { model }
+
+ context 'model' do
+ its(:all) { should == [article] }
+ its(:first) { should == article }
+ end
context 'existance' do
- specify { article.id.should be_a Integer }
- it "should create record" do
- model.all.should == [article]
- model.first.should == article
- end
+ subject { article }
+ its(:id) { should be_a Integer }
end
context '.find' do
@@ -95,16 +93,18 @@
describe UuidArticle do
let!(:article) { Fabricate :uuid_article }
let!(:id) { article.id }
- let(:model) { UuidArticle }
+ let(:model) { described_class }
+ subject { model }
- specify { model.primary_key.should == 'id' }
+ context 'model' do
+ its(:primary_key) { should == 'id' }
+ its(:all) { should == [article] }
+ its(:first) { should == article }
+ end
context 'existance' do
- specify { article.id.should be_a UUIDTools::UUID }
- it "should create record" do
- model.all.should == [article]
- model.first.should == article
- end
+ subject { article }
+ its(:id) { should be_a UUIDTools::UUID }
end
context '.find' do
@@ -133,6 +133,15 @@
specify { subject.reload(:select => :another_uuid).id.should == id }
end
+ context 'columns' do
+ [:id, :another_uuid].each do |column|
+ context column do
+ subject { model.columns_hash[column.to_s] }
+ its(:type) { should == :uuid }
+ end
+ end
+ end
+
context 'typecasting' do
let(:uuid) { UUIDTools::UUID.random_create }
let(:string) { uuid.to_s }
View
3 spec/spec_helper.rb
@@ -13,6 +13,7 @@
require 'activeuuid'
ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + "/support/migrate")
+ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, STDOUT)
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/fabricators/**/*.rb"].each { |f| require f }
@@ -38,7 +39,7 @@
config.after(:each) do
DatabaseCleaner.clean
end
-
+
def spec_for_adapter(&block)
switcher = ActiveUUID::SpecSupport::SpecForAdapter.new()
yield switcher
View
6 spec/support/spec_for_adapter.rb
@@ -5,13 +5,13 @@ class SpecForAdapter
def initialize
@specs = {}
end
-
- [:sqlite, :mysql2, :postgres].each do |name|
+
+ [:sqlite3, :mysql2, :postgresql].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)

0 comments on commit 423bc58

Please sign in to comment.
Something went wrong with that request. Please try again.