composite_primary_keys 4.0.0.beta3 → 4.0.0.beta4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ == 4.0.0.beta4 2011-07-22
2
+ * Compatible with Rails 3.1 stable branch. No longer works with RC4 (Charlie Savage)
3
+ * Do not require loading of postgresql gem unless needed (Charlie Savage)
4
+
1
5
  == 4.0.0.beta3 2011-07-08
2
6
  * Fix the ability to update the values of a primary key (Travis Warlick)
3
7
  * Support port and host configurations for postgres rake tasks (Travis Warlick)
@@ -53,7 +53,6 @@ require 'active_record/attribute_methods/read'
53
53
  require 'active_record/attribute_methods/write'
54
54
 
55
55
  require 'active_record/connection_adapters/abstract_adapter'
56
- require 'active_record/connection_adapters/postgresql_adapter'
57
56
 
58
57
  require 'active_record/relation/calculations'
59
58
  require 'active_record/relation/finder_methods'
@@ -89,7 +88,7 @@ require 'composite_primary_keys/attribute_methods/read'
89
88
  require 'composite_primary_keys/attribute_methods/write'
90
89
 
91
90
  require 'composite_primary_keys/connection_adapters/abstract_adapter'
92
- require 'composite_primary_keys/connection_adapters/postgresql_adapter'
91
+ require 'composite_primary_keys/connection_adapters/abstract/connection_specification_changes'
93
92
 
94
93
  require 'composite_primary_keys/relation/calculations'
95
94
  require 'composite_primary_keys/relation/finder_methods'
@@ -11,7 +11,8 @@ module ActiveRecord
11
11
 
12
12
  def associated_records_by_owner
13
13
  # CPK
14
- # owner_keys = owners.map { |owner| owner[owner_key_name] }.compact.uniq
14
+ owners_map = owners_by_key
15
+ #owner_keys = owners_map.keys.compact
15
16
  owner_keys = owners.map do |owner|
16
17
  Array(owner_key_name).map do |owner_key|
17
18
  owner[owner_key]
@@ -36,7 +37,7 @@ module ActiveRecord
36
37
  record[key_name]
37
38
  end.join(CompositePrimaryKeys::ID_SEP)
38
39
 
39
- owners_by_key[owner_key].each do |owner|
40
+ owners_map[owner_key].each do |owner|
40
41
  records_by_owner[owner] << record
41
42
  end
42
43
  end
@@ -124,6 +124,12 @@ module ActiveRecord
124
124
  def can_change_primary_key_values?
125
125
  false
126
126
  end
127
+
128
+ # Returns this record's primary keys values in an Array or nil if
129
+ # the record is not persisted? or has just been destroyed.
130
+ def to_key
131
+ ids.to_a if persisted? && !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids
132
+ end
127
133
  end
128
134
  end
129
135
  end
@@ -0,0 +1,88 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.load_cpk_adapter(adapter)
4
+ if adapter.to_s == 'postgresql'
5
+ require "composite_primary_keys/connection_adapters/#{adapter}_adapter.rb"
6
+ end
7
+ end
8
+
9
+ def self.establish_connection(spec = nil)
10
+ case spec
11
+ when nil
12
+ raise AdapterNotSpecified unless defined?(Rails.env)
13
+ establish_connection(Rails.env)
14
+ when ConnectionSpecification
15
+ self.connection_handler.establish_connection(name, spec)
16
+ when Symbol, String
17
+ if configuration = configurations[spec.to_s]
18
+ establish_connection(configuration)
19
+ else
20
+ raise AdapterNotSpecified, "#{spec} database is not configured"
21
+ end
22
+ else
23
+ spec = spec.symbolize_keys
24
+ unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end
25
+
26
+ begin
27
+ require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
28
+ rescue LoadError => e
29
+ raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})"
30
+ end
31
+
32
+ # CPK
33
+ load_cpk_adapter(spec[:adapter])
34
+
35
+ adapter_method = "#{spec[:adapter]}_connection"
36
+ unless respond_to?(adapter_method)
37
+ raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
38
+ end
39
+
40
+ remove_connection
41
+ establish_connection(ConnectionSpecification.new(spec, adapter_method))
42
+ end
43
+ end
44
+
45
+ class << self
46
+ # Returns the connection currently associated with the class. This can
47
+ # also be used to "borrow" the connection to do database work unrelated
48
+ # to any of the specific Active Records.
49
+ def connection
50
+ retrieve_connection
51
+ end
52
+
53
+ # Returns the configuration of the associated connection as a hash:
54
+ #
55
+ # ActiveRecord::Base.connection_config
56
+ # # => {:pool=>5, :timeout=>5000, :database=>"db/development.sqlite3", :adapter=>"sqlite3"}
57
+ #
58
+ # Please use only for reading.
59
+ def connection_config
60
+ connection_pool.spec.config
61
+ end
62
+
63
+ def connection_pool
64
+ connection_handler.retrieve_connection_pool(self)
65
+ end
66
+
67
+ def retrieve_connection
68
+ connection_handler.retrieve_connection(self)
69
+ end
70
+
71
+ # Returns true if Active Record is connected.
72
+ def connected?
73
+ connection_handler.connected?(self)
74
+ end
75
+
76
+ def remove_connection(klass = self)
77
+ connection_handler.remove_connection(klass)
78
+ end
79
+
80
+ def clear_active_connections!
81
+ connection_handler.clear_active_connections!
82
+ end
83
+
84
+ delegate :clear_reloadable_connections!,
85
+ :clear_all_connections!,:verify_active_connections!, :to => :connection_handler
86
+ end
87
+ end
88
+ end
@@ -8,6 +8,8 @@ module ActiveRecord
8
8
  pk ||= primary_key(table)
9
9
 
10
10
  if pk
11
+ # CPK
12
+ # select_value("#{sql} RETURNING #{quote_column_name(pk)}")
11
13
  select_value("#{sql} RETURNING #{quote_column_names(pk)}")
12
14
  else
13
15
  super
@@ -22,10 +24,12 @@ module ActiveRecord
22
24
  pk = primary_key(table)
23
25
  end
24
26
 
27
+ # CPK
28
+ # sql = "#{sql} RETURNING #{quote_column_name(pk)}" if pk
25
29
  sql = "#{sql} RETURNING #{quote_column_names(pk)}" if pk
26
30
 
27
31
  [sql, binds]
28
32
  end
29
33
  end
30
34
  end
31
- end
35
+ end
@@ -1,13 +1,15 @@
1
1
  module ActiveRecord
2
2
  module Persistence
3
3
  def destroy
4
- if persisted?
5
- ::ActiveRecord::IdentityMap.remove(self) if ::ActiveRecord::IdentityMap.enabled?
4
+ destroy_associations
6
5
 
6
+ if persisted?
7
+ IdentityMap.remove(self) if IdentityMap.enabled?
7
8
  # CPK
8
9
  #pk = self.class.primary_key
9
10
  #column = self.class.columns_hash[pk]
10
11
  #substitute = connection.substitute_at(column, 0)
12
+
11
13
  primary_keys = Array(self.class.primary_key)
12
14
  bind_values = Array.new
13
15
  eq_predicates = Array.new
@@ -20,10 +22,12 @@ module ActiveRecord
20
22
  predicate = Arel::Nodes::And.new(eq_predicates)
21
23
  relation = self.class.unscoped.where(predicate)
22
24
 
25
+ #relation = self.class.unscoped.where(
26
+ # self.class.arel_table[pk].eq(substitute))
27
+
23
28
  # CPK
24
29
  #relation.bind_values = [[column, id]]
25
30
  relation.bind_values = bind_values
26
-
27
31
  relation.delete_all
28
32
  end
29
33
 
@@ -3,6 +3,6 @@ module CompositePrimaryKeys
3
3
  MAJOR = 4
4
4
  MINOR = 0
5
5
  TINY = 0
6
- STRING = [MAJOR, MINOR, TINY, 'beta3'].join('.')
6
+ STRING = [MAJOR, MINOR, TINY, 'beta4'].join('.')
7
7
  end
8
8
  end
@@ -1,3 +1,4 @@
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
1
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
2
3
 
3
4
  namespace :mysql do
@@ -1,3 +1,4 @@
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
1
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
2
3
 
3
4
  namespace :oracle do
@@ -1,3 +1,4 @@
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
1
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
2
3
 
3
4
  namespace :postgresql do
@@ -1,3 +1,4 @@
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
1
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
2
3
 
3
4
  namespace :sqlite3 do
@@ -1,7 +1,4 @@
1
- dir = File.dirname(__FILE__)
2
- PROJECT_ROOT = File.expand_path(File.join(dir, '..'))
3
-
4
- adapter = ENV["ADAPTER"] || "postgresql"
1
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
5
2
 
6
3
  require "pp"
7
4
  require "test/unit"
@@ -11,18 +8,22 @@ require "hash_tricks"
11
8
  #require 'composite_primary_keys'
12
9
  require File.join(PROJECT_ROOT, "lib", "composite_primary_keys")
13
10
 
11
+ # Now load the connection spec
14
12
  require File.join(PROJECT_ROOT, "test", "connections", "connection_spec")
13
+ config = CompositePrimaryKeys::ConnectionSpec.config
14
+ spec = config[config.keys.first]
15
+
16
+ # And now connect to the database
17
+ adapter = spec['adapter']
15
18
  require File.join(PROJECT_ROOT, "test", "connections", "native_#{adapter}", "connection")
16
19
 
20
+ # Tell active record about the configuration
21
+ ActiveRecord::Base.configurations[:test] = spec
17
22
 
18
23
  # Tell ActiveRecord where to find models
19
24
  ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
20
25
 
21
- QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
22
-
23
- ActiveRecord::Base.configurations[:test] = SPEC
24
-
25
- class ActiveSupport::TestCase #:nodoc:
26
+ class ActiveSupport::TestCase
26
27
  include ActiveRecord::TestFixtures
27
28
 
28
29
  self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
@@ -98,4 +99,3 @@ ActiveRecord::Base.connection.class.class_eval do
98
99
  end
99
100
 
100
101
  ActiveRecord::Base.logger = Logger.new(STDOUT)
101
- #ActiveRecord::Base.colorize_logging = false
@@ -1,3 +1,7 @@
1
+ # To run tests:
2
+ # 1. Copy this file to databases.yml
3
+ # 2. Delete all the specs except one
4
+
1
5
  mysql:
2
6
  adapter: mysql
3
7
  username: root
@@ -1,12 +1,3 @@
1
- mysql:
2
- adapter: mysql
3
- username: root
4
- database: composite_primary_keys_unittest
5
-
6
- sqlite3:
7
- adapter: sqlite3
8
- database: db/composite_primary_keys_unittest.sqlite3
9
-
10
1
  postgresql:
11
2
  adapter: postgresql
12
3
  database: composite_primary_keys_unittest
@@ -5,7 +5,6 @@ require 'IBM_DB'
5
5
 
6
6
  RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
7
7
 
8
-
9
8
  db1 = 'composite_primary_keys_unittest'
10
9
 
11
10
  connection_options = {
@@ -17,4 +16,4 @@ connection_options = {
17
16
  }
18
17
 
19
18
  ActiveRecord::Base.configurations = { db1 => connection_options }
20
- ActiveRecord::Base.establish_connection(connection_options)
19
+ ActiveRecord::Base.establish_connection(connection_options)
@@ -1,7 +1,5 @@
1
1
  print "Using native MySQL\n"
2
2
 
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
-
5
3
  def connection_string
6
4
  options = {}
7
5
  options['u'] = SPEC['username'] if SPEC['username']
@@ -10,6 +8,6 @@ def connection_string
10
8
  options.map { |key, value| "-#{key}#{value}" }.join(" ")
11
9
  end
12
10
 
13
- # Adapter config setup in locals/database_connections.rb
14
- SPEC = CompositePrimaryKeys::ConnectionSpec[:mysql]
11
+ # Adapter config setup in text/connections/databases.yml
12
+ SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
15
13
  ActiveRecord::Base.establish_connection(SPEC)
@@ -7,5 +7,5 @@ def connection_string
7
7
  end
8
8
 
9
9
  # Adapter config setup in locals/database_connections.rb
10
- SPEC = CompositePrimaryKeys::ConnectionSpec[:oracle]
10
+ SPEC = CompositePrimaryKeys::ConnectionSpec['oracle']
11
11
  ActiveRecord::Base.establish_connection(SPEC)
@@ -1,9 +1,5 @@
1
1
  print "Using native Postgresql\n"
2
2
 
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
5
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys', 'connection_adapters', 'postgresql_adapter')
6
-
7
3
  def connection_string
8
4
  options = Hash.new
9
5
  options['U'] = SPEC['username'] if SPEC['username']
@@ -12,5 +8,6 @@ def connection_string
12
8
  options.map { |key, value| "-#{key} #{value}" }.join(" ")
13
9
  end
14
10
 
15
- SPEC = CompositePrimaryKeys::ConnectionSpec[:postgresql]
16
- ActiveRecord::Base.establish_connection(SPEC)
11
+ # Adapter config setup in text/connections/databases.yml
12
+ SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
13
+ ActiveRecord::Base.establish_connection(SPEC)
@@ -1,11 +1,9 @@
1
1
  print "Using native Sqlite3\n"
2
2
 
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
5
-
6
3
  def connection_string
7
4
  SPEC['database']
8
5
  end
9
6
 
10
- SPEC = CompositePrimaryKeys::ConnectionSpec[:sqlite3]
7
+ # Adapter config setup in text/connections/databases.yml
8
+ SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
11
9
  ActiveRecord::Base.establish_connection(SPEC)
@@ -2,7 +2,7 @@ require 'abstract_unit'
2
2
 
3
3
  class TestAttributeMethods < ActiveSupport::TestCase
4
4
  fixtures :reference_types
5
-
5
+
6
6
  def test_read_attribute_with_single_key
7
7
  rt = ReferenceType.find(1)
8
8
  assert_equal(1, rt.reference_type_id)
@@ -11,9 +11,45 @@ class TestAttributeMethods < ActiveSupport::TestCase
11
11
  end
12
12
 
13
13
  def test_read_attribute_with_composite_keys
14
- ref_code = ReferenceCode.find(1,1)
14
+ ref_code = ReferenceCode.find(1, 1)
15
15
  assert_equal(1, ref_code.id.first)
16
16
  assert_equal(1, ref_code.id.last)
17
17
  assert_equal('Mr', ref_code.abbreviation)
18
18
  end
19
+
20
+ # to_key returns array even for single key
21
+ def test_to_key_with_single_key
22
+ rt = ReferenceType.find(1)
23
+ assert_equal([1], rt.to_key)
24
+ end
25
+
26
+ def test_to_key_with_composite_keys
27
+ ref_code = ReferenceCode.find(1, 1)
28
+ assert_equal(1, ref_code.to_key.first)
29
+ assert_equal(1, ref_code.to_key.last)
30
+ end
31
+
32
+ def test_to_key_with_single_key_unsaved
33
+ rt = ReferenceType.new
34
+ assert_nil(rt.to_key)
35
+ end
36
+
37
+ def test_to_key_with_composite_keys_unsaved
38
+ ref_code = ReferenceCode.new
39
+ assert_nil(ref_code.to_key)
40
+ end
41
+
42
+ def test_to_key_with_single_key_destroyed
43
+ rt = ReferenceType.find(1)
44
+ rt.destroy
45
+ assert_nil(rt.to_key)
46
+ end
47
+
48
+ def test_to_key_with_composite_key_destroyed
49
+ ref_code = ReferenceCode.find(1, 1)
50
+ ref_code.destroy
51
+ assert_nil(ref_code.to_key)
52
+ end
53
+
54
+
19
55
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- hash: 62196261
4
+ hash: 62196267
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 4
8
8
  - 0
9
9
  - 0
10
10
  - beta
11
- - 3
12
- version: 4.0.0.beta3
11
+ - 4
12
+ version: 4.0.0.beta4
13
13
  platform: ruby
14
14
  authors:
15
15
  - Dr Nic Williams
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-07-08 00:00:00 Z
21
+ date: 2011-07-22 00:00:00 Z
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
24
  name: activerecord
@@ -71,6 +71,7 @@ files:
71
71
  - lib/composite_primary_keys/base.rb
72
72
  - lib/composite_primary_keys/composite_arrays.rb
73
73
  - lib/composite_primary_keys/composite_predicates.rb
74
+ - lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb
74
75
  - lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
75
76
  - lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
76
77
  - lib/composite_primary_keys/dirty.rb
@@ -221,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
222
  requirements: []
222
223
 
223
224
  rubyforge_project: compositekeys
224
- rubygems_version: 1.7.2
225
+ rubygems_version: 1.8.5
225
226
  signing_key:
226
227
  specification_version: 3
227
228
  summary: Composite key support for ActiveRecord