composite_primary_keys 8.1.4 → 8.1.5
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.
- checksums.yaml +4 -4
- data/History.rdoc +4 -0
- data/Rakefile +34 -34
- data/lib/composite_primary_keys/attribute_methods/read.rb +2 -1
- data/lib/composite_primary_keys/dirty.rb +19 -19
- data/lib/composite_primary_keys/version.rb +1 -1
- data/tasks/databases/mysql.rake +42 -42
- data/tasks/databases/postgresql.rake +47 -47
- data/tasks/databases/sqlite3.rake +27 -27
- data/test/connections/connection_spec.rb +18 -18
- data/test/connections/databases.yml +25 -4
- data/test/connections/native_ibm_db/connection.rb +18 -18
- data/test/connections/native_mysql/connection.rb +17 -17
- data/test/connections/native_postgresql/connection.rb +12 -12
- data/test/connections/native_sqlite3/connection.rb +9 -9
- data/test/fixtures/article.rb +5 -5
- data/test/fixtures/db_definitions/postgresql.sql +9 -0
- data/test/fixtures/pk_called_id.rb +5 -0
- data/test/fixtures/pk_called_ids.yml +11 -0
- data/test/test_ids.rb +113 -109
- data/test/test_update.rb +5 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abff4d9e394b49dc7b5202df306d247ac92cbcd4
|
4
|
+
data.tar.gz: f0faa4bff366aba7dd02ce375eb1ba69b0b33cff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25822213d2b20007dfa81137f7504e6df9b86b75e41592bf9adfb75ef903c234d3d5dcda2a4df8a82a0579701b1db8bea0a4320b9db62ce3aae2c96999236d48
|
7
|
+
data.tar.gz: 9734252590c4f158064e6c8f8ef1e695892f97084731639bd5875c9b0f67c18bc51070b3a7cc458b1477a53b85ebcdc915f561735b3f780d147fd9d77affd0e5
|
data/History.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
require 'rake/clean'
|
4
|
-
require 'rake/testtask'
|
5
|
-
require 'rubygems/package_task'
|
6
|
-
|
7
|
-
# Set global variable so other tasks can access them
|
8
|
-
::PROJECT_ROOT = File.expand_path(".")
|
9
|
-
::GEM_NAME = 'composite_primary_keys'
|
10
|
-
|
11
|
-
# Read the spec file
|
12
|
-
spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
|
13
|
-
|
14
|
-
# Setup Rake tasks for managing the gem
|
15
|
-
Gem::PackageTask.new(spec).define
|
16
|
-
|
17
|
-
# Now load in other task files
|
18
|
-
Dir.glob('tasks/**/*.rake').each do |rake_file|
|
19
|
-
load File.join(File.dirname(__FILE__), rake_file)
|
20
|
-
end
|
21
|
-
|
22
|
-
# Set up test tasks for each supported connection adapter
|
23
|
-
%w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
|
24
|
-
namespace adapter do
|
25
|
-
desc "Run tests using the #{adapter} adapter"
|
26
|
-
task "test" do
|
27
|
-
ENV["ADAPTER"] = adapter
|
28
|
-
Rake::TestTask.new("subtest_#{adapter}") do |t|
|
29
|
-
t.libs << "test"
|
30
|
-
end
|
31
|
-
Rake::Task["subtest_#{adapter}"].invoke
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rubygems/package_task'
|
6
|
+
|
7
|
+
# Set global variable so other tasks can access them
|
8
|
+
::PROJECT_ROOT = File.expand_path(".")
|
9
|
+
::GEM_NAME = 'composite_primary_keys'
|
10
|
+
|
11
|
+
# Read the spec file
|
12
|
+
spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
|
13
|
+
|
14
|
+
# Setup Rake tasks for managing the gem
|
15
|
+
Gem::PackageTask.new(spec).define
|
16
|
+
|
17
|
+
# Now load in other task files
|
18
|
+
Dir.glob('tasks/**/*.rake').each do |rake_file|
|
19
|
+
load File.join(File.dirname(__FILE__), rake_file)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Set up test tasks for each supported connection adapter
|
23
|
+
%w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
|
24
|
+
namespace adapter do
|
25
|
+
desc "Run tests using the #{adapter} adapter"
|
26
|
+
task "test" do
|
27
|
+
ENV["ADAPTER"] = adapter
|
28
|
+
Rake::TestTask.new("subtest_#{adapter}") do |t|
|
29
|
+
t.libs << "test"
|
30
|
+
end
|
31
|
+
Rake::Task["subtest_#{adapter}"].invoke
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -7,7 +7,8 @@ module ActiveRecord
|
|
7
7
|
_read_attribute(attr_name, &block)
|
8
8
|
else
|
9
9
|
name = attr_name.to_s
|
10
|
-
|
10
|
+
# CPK
|
11
|
+
name = self.class.primary_key if name == 'id'.freeze && !composite?
|
11
12
|
_read_attribute(name, &block)
|
12
13
|
end
|
13
14
|
end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module Dirty
|
3
|
-
def can_change_primary_key?
|
4
|
-
true
|
5
|
-
end
|
6
|
-
|
7
|
-
def primary_key_changed?
|
8
|
-
!!changed.detect { |key| ids_hash.keys.include?(key.to_s) }
|
9
|
-
end
|
10
|
-
|
11
|
-
def primary_key_was
|
12
|
-
ids_hash.keys.inject(Hash.new) do |result, attribute_name|
|
13
|
-
result[attribute_name] = attribute_was(attribute_name.to_s)
|
14
|
-
result
|
15
|
-
end
|
16
|
-
end
|
17
|
-
alias_method :ids_hash_was, :primary_key_was
|
18
|
-
end
|
19
|
-
end
|
1
|
+
module ActiveModel
|
2
|
+
module Dirty
|
3
|
+
def can_change_primary_key?
|
4
|
+
true
|
5
|
+
end
|
6
|
+
|
7
|
+
def primary_key_changed?
|
8
|
+
!!changed.detect { |key| ids_hash.keys.include?(key.to_s) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def primary_key_was
|
12
|
+
ids_hash.keys.inject(Hash.new) do |result, attribute_name|
|
13
|
+
result[attribute_name] = attribute_was(attribute_name.to_s)
|
14
|
+
result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method :ids_hash_was, :primary_key_was
|
18
|
+
end
|
19
|
+
end
|
data/tasks/databases/mysql.rake
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
-
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
-
|
4
|
-
namespace :mysql do
|
5
|
-
desc 'Create the MySQL test database'
|
6
|
-
task :create_database do
|
7
|
-
ActiveRecord::Base.clear_all_connections!
|
8
|
-
spec = CompositePrimaryKeys::ConnectionSpec['mysql'].dup
|
9
|
-
database_name = spec.delete('database')
|
10
|
-
connection = ActiveRecord::Base.establish_connection(spec)
|
11
|
-
ActiveRecord::Base.connection.create_database(database_name)
|
12
|
-
ActiveRecord::Base.clear_all_connections!
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Build the MySQL test database'
|
16
|
-
task :build_database => [:create_database] do
|
17
|
-
path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'mysql.sql')
|
18
|
-
sql = File.open(path, 'rb') do |file|
|
19
|
-
file.read
|
20
|
-
end
|
21
|
-
|
22
|
-
Rake::Task['mysql:load_connection'].reenable
|
23
|
-
Rake::Task['mysql:load_connection'].invoke
|
24
|
-
#puts %(ActiveRecord::Base.connection.instance_variable_get(:@config)=#{(ActiveRecord::Base.connection.instance_variable_get(:@config)).inspect})
|
25
|
-
sql.split(";").each do |statement|
|
26
|
-
ActiveRecord::Base.connection.execute(statement) unless statement.strip.length == 0
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
desc 'Drop the MySQL test database'
|
31
|
-
task :drop_database => :load_connection do
|
32
|
-
ActiveRecord::Base.connection.drop_database(SPEC['database'])
|
33
|
-
end
|
34
|
-
|
35
|
-
desc 'Rebuild the MySQL test database'
|
36
|
-
task :rebuild_database => [:drop_database, :build_database]
|
37
|
-
|
38
|
-
task :load_connection do
|
39
|
-
require File.join(PROJECT_ROOT, "test", "connections", "native_mysql", "connection")
|
40
|
-
establish_connection
|
41
|
-
end
|
42
|
-
end
|
1
|
+
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
+
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
+
|
4
|
+
namespace :mysql do
|
5
|
+
desc 'Create the MySQL test database'
|
6
|
+
task :create_database do
|
7
|
+
ActiveRecord::Base.clear_all_connections!
|
8
|
+
spec = CompositePrimaryKeys::ConnectionSpec['mysql'].dup
|
9
|
+
database_name = spec.delete('database')
|
10
|
+
connection = ActiveRecord::Base.establish_connection(spec)
|
11
|
+
ActiveRecord::Base.connection.create_database(database_name)
|
12
|
+
ActiveRecord::Base.clear_all_connections!
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Build the MySQL test database'
|
16
|
+
task :build_database => [:create_database] do
|
17
|
+
path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'mysql.sql')
|
18
|
+
sql = File.open(path, 'rb') do |file|
|
19
|
+
file.read
|
20
|
+
end
|
21
|
+
|
22
|
+
Rake::Task['mysql:load_connection'].reenable
|
23
|
+
Rake::Task['mysql:load_connection'].invoke
|
24
|
+
#puts %(ActiveRecord::Base.connection.instance_variable_get(:@config)=#{(ActiveRecord::Base.connection.instance_variable_get(:@config)).inspect})
|
25
|
+
sql.split(";").each do |statement|
|
26
|
+
ActiveRecord::Base.connection.execute(statement) unless statement.strip.length == 0
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
desc 'Drop the MySQL test database'
|
31
|
+
task :drop_database => :load_connection do
|
32
|
+
ActiveRecord::Base.connection.drop_database(SPEC['database'])
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Rebuild the MySQL test database'
|
36
|
+
task :rebuild_database => [:drop_database, :build_database]
|
37
|
+
|
38
|
+
task :load_connection do
|
39
|
+
require File.join(PROJECT_ROOT, "test", "connections", "native_mysql", "connection")
|
40
|
+
establish_connection
|
41
|
+
end
|
42
|
+
end
|
@@ -1,48 +1,48 @@
|
|
1
|
-
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
-
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
-
|
4
|
-
namespace :postgresql do
|
5
|
-
desc 'Create the PostgreSQL test database'
|
6
|
-
task :create_database do
|
7
|
-
ActiveRecord::Base.clear_all_connections!
|
8
|
-
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
9
|
-
database_name = spec.delete('database')
|
10
|
-
spec['database'] = 'postgres'
|
11
|
-
connection = ActiveRecord::Base.establish_connection(spec)
|
12
|
-
ActiveRecord::Base.connection.create_database(database_name)
|
13
|
-
ActiveRecord::Base.clear_all_connections!
|
14
|
-
end
|
15
|
-
|
16
|
-
desc 'Build the PostgreSQL test database'
|
17
|
-
task :build_database => [:create_database] do
|
18
|
-
ActiveRecord::Base.clear_all_connections!
|
19
|
-
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
20
|
-
connection = ActiveRecord::Base.establish_connection(spec)
|
21
|
-
|
22
|
-
path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
|
23
|
-
sql = File.open(path, 'rb') do |file|
|
24
|
-
file.read
|
25
|
-
end
|
26
|
-
|
27
|
-
Rake::Task['postgresql:load_connection'].invoke
|
28
|
-
ActiveRecord::Base.connection.execute(sql)
|
29
|
-
end
|
30
|
-
|
31
|
-
desc 'Drop the PostgreSQL test database'
|
32
|
-
task :drop_database => :load_connection do
|
33
|
-
ActiveRecord::Base.clear_all_connections!
|
34
|
-
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
35
|
-
database_name = spec.delete('database')
|
36
|
-
spec['database'] = 'postgres'
|
37
|
-
connection = ActiveRecord::Base.establish_connection(spec)
|
38
|
-
ActiveRecord::Base.connection.drop_database(SPEC['database'])
|
39
|
-
ActiveRecord::Base.clear_all_connections!
|
40
|
-
end
|
41
|
-
|
42
|
-
desc 'Rebuild the PostgreSQL test database'
|
43
|
-
task :rebuild_database => [:drop_database, :build_database]
|
44
|
-
|
45
|
-
task :load_connection do
|
46
|
-
require File.join(PROJECT_ROOT, "test", "connections", "native_postgresql", "connection")
|
47
|
-
end
|
1
|
+
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
+
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
+
|
4
|
+
namespace :postgresql do
|
5
|
+
desc 'Create the PostgreSQL test database'
|
6
|
+
task :create_database do
|
7
|
+
ActiveRecord::Base.clear_all_connections!
|
8
|
+
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
9
|
+
database_name = spec.delete('database')
|
10
|
+
spec['database'] = 'postgres'
|
11
|
+
connection = ActiveRecord::Base.establish_connection(spec)
|
12
|
+
ActiveRecord::Base.connection.create_database(database_name)
|
13
|
+
ActiveRecord::Base.clear_all_connections!
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Build the PostgreSQL test database'
|
17
|
+
task :build_database => [:create_database] do
|
18
|
+
ActiveRecord::Base.clear_all_connections!
|
19
|
+
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
20
|
+
connection = ActiveRecord::Base.establish_connection(spec)
|
21
|
+
|
22
|
+
path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
|
23
|
+
sql = File.open(path, 'rb') do |file|
|
24
|
+
file.read
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::Task['postgresql:load_connection'].invoke
|
28
|
+
ActiveRecord::Base.connection.execute(sql)
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Drop the PostgreSQL test database'
|
32
|
+
task :drop_database => :load_connection do
|
33
|
+
ActiveRecord::Base.clear_all_connections!
|
34
|
+
spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
|
35
|
+
database_name = spec.delete('database')
|
36
|
+
spec['database'] = 'postgres'
|
37
|
+
connection = ActiveRecord::Base.establish_connection(spec)
|
38
|
+
ActiveRecord::Base.connection.drop_database(SPEC['database'])
|
39
|
+
ActiveRecord::Base.clear_all_connections!
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'Rebuild the PostgreSQL test database'
|
43
|
+
task :rebuild_database => [:drop_database, :build_database]
|
44
|
+
|
45
|
+
task :load_connection do
|
46
|
+
require File.join(PROJECT_ROOT, "test", "connections", "native_postgresql", "connection")
|
47
|
+
end
|
48
48
|
end
|
@@ -1,27 +1,27 @@
|
|
1
|
-
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
-
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
-
|
4
|
-
namespace :sqlite3 do
|
5
|
-
desc 'Build the sqlite test database'
|
6
|
-
task :build_database => :load_connection do
|
7
|
-
schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
|
8
|
-
dbfile = File.join(PROJECT_ROOT, connection_string)
|
9
|
-
FileUtils.mkdir_p(File.dirname(dbfile))
|
10
|
-
cmd = "sqlite3 #{dbfile} < #{schema}"
|
11
|
-
puts cmd
|
12
|
-
sh %{ #{cmd} }
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Drop the sqlite test database'
|
16
|
-
task :drop_database => :load_connection do
|
17
|
-
dbfile = connection_string
|
18
|
-
sh %{ rm -f #{dbfile} }
|
19
|
-
end
|
20
|
-
|
21
|
-
desc 'Rebuild the sqlite test database'
|
22
|
-
task :rebuild_database => [:drop_database, :build_database]
|
23
|
-
|
24
|
-
task :load_connection do
|
25
|
-
require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
|
26
|
-
end
|
27
|
-
end
|
1
|
+
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
2
|
+
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
3
|
+
|
4
|
+
namespace :sqlite3 do
|
5
|
+
desc 'Build the sqlite test database'
|
6
|
+
task :build_database => :load_connection do
|
7
|
+
schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
|
8
|
+
dbfile = File.join(PROJECT_ROOT, connection_string)
|
9
|
+
FileUtils.mkdir_p(File.dirname(dbfile))
|
10
|
+
cmd = "sqlite3 #{dbfile} < #{schema}"
|
11
|
+
puts cmd
|
12
|
+
sh %{ #{cmd} }
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Drop the sqlite test database'
|
16
|
+
task :drop_database => :load_connection do
|
17
|
+
dbfile = connection_string
|
18
|
+
sh %{ rm -f #{dbfile} }
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Rebuild the sqlite test database'
|
22
|
+
task :rebuild_database => [:drop_database, :build_database]
|
23
|
+
|
24
|
+
task :load_connection do
|
25
|
+
require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
|
26
|
+
end
|
27
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module CompositePrimaryKeys
|
4
|
-
class ConnectionSpec
|
5
|
-
def self.[](adapter)
|
6
|
-
config[adapter.to_s]
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def self.config
|
12
|
-
@config ||= begin
|
13
|
-
path = File.join(PROJECT_ROOT, 'test', 'connections', 'databases.yml')
|
14
|
-
YAML.load_file(path)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module CompositePrimaryKeys
|
4
|
+
class ConnectionSpec
|
5
|
+
def self.[](adapter)
|
6
|
+
config[adapter.to_s]
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
@config ||= begin
|
13
|
+
path = File.join(PROJECT_ROOT, 'test', 'connections', 'databases.yml')
|
14
|
+
YAML.load_file(path)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,9 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
mysql:
|
2
|
+
adapter: mysql2
|
3
|
+
username: root
|
4
|
+
password: mysql
|
5
|
+
database: composite_primary_keys_unittest
|
6
|
+
|
7
|
+
oracle:
|
8
|
+
adapter: oracle_enhanced
|
9
|
+
database: xe
|
10
|
+
username: SYSTEM
|
11
|
+
password: oracle
|
12
|
+
host: localhost
|
13
|
+
|
5
14
|
postgresql:
|
6
15
|
adapter: postgresql
|
7
16
|
database: composite_primary_keys_unittest
|
8
17
|
username: postgres
|
9
18
|
host: localhost
|
19
|
+
|
20
|
+
sqlite:
|
21
|
+
adapter: sqlite3
|
22
|
+
database: <%= File.join(project_root, 'db', 'composite_primary_keys_unittest.sqlite') %>
|
23
|
+
|
24
|
+
sqlserver:
|
25
|
+
adapter: sqlserver
|
26
|
+
username: cpk
|
27
|
+
password: cpk
|
28
|
+
database: composite_primary_keys_unittest
|
29
|
+
host: localhost
|
30
|
+
port: 1433
|
@@ -1,19 +1,19 @@
|
|
1
|
-
print "Using IBM2 \n"
|
2
|
-
|
3
|
-
gem 'ibm_db'
|
4
|
-
require 'IBM_DB'
|
5
|
-
|
6
|
-
RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
|
7
|
-
|
8
|
-
db1 = 'composite_primary_keys_unittest'
|
9
|
-
|
10
|
-
connection_options = {
|
11
|
-
:adapter => "ibm_db",
|
12
|
-
:database => "ocdpdev",
|
13
|
-
:username => "db2inst1",
|
14
|
-
:password => "password",
|
15
|
-
:host => '192.168.2.21'
|
16
|
-
}
|
17
|
-
|
18
|
-
ActiveRecord::Base.configurations = { db1 => connection_options }
|
1
|
+
print "Using IBM2 \n"
|
2
|
+
|
3
|
+
gem 'ibm_db'
|
4
|
+
require 'IBM_DB'
|
5
|
+
|
6
|
+
RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
|
7
|
+
|
8
|
+
db1 = 'composite_primary_keys_unittest'
|
9
|
+
|
10
|
+
connection_options = {
|
11
|
+
:adapter => "ibm_db",
|
12
|
+
:database => "ocdpdev",
|
13
|
+
:username => "db2inst1",
|
14
|
+
:password => "password",
|
15
|
+
:host => '192.168.2.21'
|
16
|
+
}
|
17
|
+
|
18
|
+
ActiveRecord::Base.configurations = { db1 => connection_options }
|
19
19
|
ActiveRecord::Base.establish_connection(connection_options)
|
@@ -1,17 +1,17 @@
|
|
1
|
-
print "Using native MySQL\n"
|
2
|
-
|
3
|
-
def connection_string
|
4
|
-
options = {}
|
5
|
-
options['u'] = SPEC['username'] if SPEC['username']
|
6
|
-
options['p'] = SPEC['password'] if SPEC['password']
|
7
|
-
options['S'] = SPEC['sock'] if SPEC['sock']
|
8
|
-
options.map { |key, value| "-#{key}#{value}" }.join(" ")
|
9
|
-
end
|
10
|
-
|
11
|
-
# Adapter config setup in test/connections/databases.yml
|
12
|
-
SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
|
13
|
-
|
14
|
-
def establish_connection
|
15
|
-
ActiveRecord::Base.establish_connection(SPEC)
|
16
|
-
end
|
17
|
-
establish_connection
|
1
|
+
print "Using native MySQL\n"
|
2
|
+
|
3
|
+
def connection_string
|
4
|
+
options = {}
|
5
|
+
options['u'] = SPEC['username'] if SPEC['username']
|
6
|
+
options['p'] = SPEC['password'] if SPEC['password']
|
7
|
+
options['S'] = SPEC['sock'] if SPEC['sock']
|
8
|
+
options.map { |key, value| "-#{key}#{value}" }.join(" ")
|
9
|
+
end
|
10
|
+
|
11
|
+
# Adapter config setup in test/connections/databases.yml
|
12
|
+
SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
|
13
|
+
|
14
|
+
def establish_connection
|
15
|
+
ActiveRecord::Base.establish_connection(SPEC)
|
16
|
+
end
|
17
|
+
establish_connection
|
@@ -1,13 +1,13 @@
|
|
1
|
-
print "Using native Postgresql\n"
|
2
|
-
|
3
|
-
def connection_string
|
4
|
-
options = Hash.new
|
5
|
-
options['U'] = SPEC['username'] if SPEC['username']
|
6
|
-
options['h'] = SPEC['host'] if SPEC['host']
|
7
|
-
options['p'] = SPEC['port'] if SPEC['port']
|
8
|
-
options.map { |key, value| "-#{key} #{value}" }.join(" ")
|
9
|
-
end
|
10
|
-
|
11
|
-
# Adapter config setup in text/connections/databases.yml
|
12
|
-
SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
|
1
|
+
print "Using native Postgresql\n"
|
2
|
+
|
3
|
+
def connection_string
|
4
|
+
options = Hash.new
|
5
|
+
options['U'] = SPEC['username'] if SPEC['username']
|
6
|
+
options['h'] = SPEC['host'] if SPEC['host']
|
7
|
+
options['p'] = SPEC['port'] if SPEC['port']
|
8
|
+
options.map { |key, value| "-#{key} #{value}" }.join(" ")
|
9
|
+
end
|
10
|
+
|
11
|
+
# Adapter config setup in text/connections/databases.yml
|
12
|
+
SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
|
13
13
|
ActiveRecord::Base.establish_connection(SPEC)
|
@@ -1,9 +1,9 @@
|
|
1
|
-
print "Using native Sqlite3\n"
|
2
|
-
|
3
|
-
def connection_string
|
4
|
-
SPEC['database']
|
5
|
-
end
|
6
|
-
|
7
|
-
# Adapter config setup in text/connections/databases.yml
|
8
|
-
SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
|
9
|
-
ActiveRecord::Base.establish_connection(SPEC)
|
1
|
+
print "Using native Sqlite3\n"
|
2
|
+
|
3
|
+
def connection_string
|
4
|
+
SPEC['database']
|
5
|
+
end
|
6
|
+
|
7
|
+
# Adapter config setup in text/connections/databases.yml
|
8
|
+
SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
|
9
|
+
ActiveRecord::Base.establish_connection(SPEC)
|
data/test/fixtures/article.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
class Article < ActiveRecord::Base
|
2
|
-
has_many :readings, :dependent => :delete_all
|
3
|
-
has_many :users, :through => :readings
|
4
|
-
end
|
5
|
-
|
1
|
+
class Article < ActiveRecord::Base
|
2
|
+
has_many :readings, :dependent => :delete_all
|
3
|
+
has_many :users, :through => :readings
|
4
|
+
end
|
5
|
+
|
@@ -218,3 +218,12 @@ create table employees_groups (
|
|
218
218
|
employee_id int not null,
|
219
219
|
group_id int not null
|
220
220
|
);
|
221
|
+
|
222
|
+
create table pk_called_ids (
|
223
|
+
id serial not null,
|
224
|
+
reference_code int not null,
|
225
|
+
code_label varchar(50) default null,
|
226
|
+
abbreviation varchar(50) default null,
|
227
|
+
description varchar(50) default null,
|
228
|
+
primary key (id, reference_code)
|
229
|
+
);
|
data/test/test_ids.rb
CHANGED
@@ -1,109 +1,113 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class ChildCpkTest < ReferenceCode
|
4
|
-
end
|
5
|
-
|
6
|
-
class TestIds < ActiveSupport::TestCase
|
7
|
-
fixtures :reference_types, :reference_codes
|
8
|
-
|
9
|
-
CLASSES = {
|
10
|
-
:single => {
|
11
|
-
:class => ReferenceType,
|
12
|
-
:primary_keys => [:reference_type_id],
|
13
|
-
},
|
14
|
-
:dual => {
|
15
|
-
:class => ReferenceCode,
|
16
|
-
:primary_keys => [:reference_type_id, :reference_code],
|
17
|
-
},
|
18
|
-
:dual_strs => {
|
19
|
-
:class => ReferenceCode,
|
20
|
-
:primary_keys => ['reference_type_id', 'reference_code'],
|
21
|
-
},
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
assert_equal @primary_keys.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class ChildCpkTest < ReferenceCode
|
4
|
+
end
|
5
|
+
|
6
|
+
class TestIds < ActiveSupport::TestCase
|
7
|
+
fixtures :reference_types, :reference_codes, :pk_called_ids
|
8
|
+
|
9
|
+
CLASSES = {
|
10
|
+
:single => {
|
11
|
+
:class => ReferenceType,
|
12
|
+
:primary_keys => [:reference_type_id],
|
13
|
+
},
|
14
|
+
:dual => {
|
15
|
+
:class => ReferenceCode,
|
16
|
+
:primary_keys => [:reference_type_id, :reference_code],
|
17
|
+
},
|
18
|
+
:dual_strs => {
|
19
|
+
:class => ReferenceCode,
|
20
|
+
:primary_keys => ['reference_type_id', 'reference_code'],
|
21
|
+
},
|
22
|
+
:pk_called_id => {
|
23
|
+
:class => PkCalledId,
|
24
|
+
:primary_keys => ['id', 'reference_code'],
|
25
|
+
},
|
26
|
+
}
|
27
|
+
|
28
|
+
def setup
|
29
|
+
self.class.classes = CLASSES
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_id
|
33
|
+
testing_with do
|
34
|
+
assert_equal @first.id, @first.ids if composite?
|
35
|
+
assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_to_param
|
40
|
+
testing_with do
|
41
|
+
assert_equal '1,1', @first.to_param if composite?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_ids_to_s
|
46
|
+
testing_with do
|
47
|
+
order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
|
48
|
+
to_test = @klass.order(order)[0..1].map(&:id)
|
49
|
+
assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
|
50
|
+
assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_set_ids_string
|
55
|
+
testing_with do
|
56
|
+
array = @primary_keys.collect {|key| 5}
|
57
|
+
expected = composite? ? array.to_composite_keys : array.first
|
58
|
+
@first.id = expected.to_s
|
59
|
+
assert_equal expected, @first.id
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_set_ids_array
|
64
|
+
testing_with do
|
65
|
+
array = @primary_keys.collect {|key| 5}
|
66
|
+
expected = composite? ? array.to_composite_keys : array.first
|
67
|
+
@first.id = expected
|
68
|
+
assert_equal expected, @first.id
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_set_ids_comp
|
73
|
+
testing_with do
|
74
|
+
array = @primary_keys.collect {|key| 5}
|
75
|
+
expected = composite? ? array.to_composite_keys : array.first
|
76
|
+
@first.id = expected
|
77
|
+
assert_equal expected, @first.id
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_primary_keys
|
82
|
+
testing_with do
|
83
|
+
if composite?
|
84
|
+
assert_not_nil @klass.primary_keys
|
85
|
+
assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
|
86
|
+
assert_equal @klass.primary_keys, @klass.primary_key
|
87
|
+
assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
|
88
|
+
assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
|
89
|
+
else
|
90
|
+
assert_not_nil @klass.primary_key
|
91
|
+
assert_equal @primary_keys.first, @klass.primary_key.to_sym
|
92
|
+
assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_inherited_primary_keys
|
98
|
+
assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_inherited_ids
|
102
|
+
cpk_test = ChildCpkTest.new
|
103
|
+
assert_equal([nil, nil], cpk_test.id)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_assign_ids
|
107
|
+
ref_code = ReferenceCode.new
|
108
|
+
assert_equal([nil, nil], ref_code.id)
|
109
|
+
|
110
|
+
ref_code.id = [2,1]
|
111
|
+
assert_equal([2,1], ref_code.id)
|
112
|
+
end
|
113
|
+
end
|
data/test/test_update.rb
CHANGED
@@ -62,10 +62,12 @@ class TestUpdate < ActiveSupport::TestCase
|
|
62
62
|
|
63
63
|
def test_update_all
|
64
64
|
assert_nothing_raised do
|
65
|
-
reference_code = ReferenceCode.
|
65
|
+
reference_code = ReferenceCode.first
|
66
66
|
primary_key = reference_code.class.primary_key
|
67
|
-
ReferenceCode.where(primary_key => reference_code[primary_key]).
|
68
|
-
|
67
|
+
ReferenceCode.where(primary_key => reference_code[primary_key]).update_all(description: 'random value')
|
68
|
+
|
69
|
+
reference_code.reload
|
70
|
+
assert_equal('random value', reference_code.description)
|
69
71
|
end
|
70
72
|
end
|
71
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.1.
|
4
|
+
version: 8.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -172,6 +172,8 @@ files:
|
|
172
172
|
- test/fixtures/membership_status.rb
|
173
173
|
- test/fixtures/membership_statuses.yml
|
174
174
|
- test/fixtures/memberships.yml
|
175
|
+
- test/fixtures/pk_called_id.rb
|
176
|
+
- test/fixtures/pk_called_ids.yml
|
175
177
|
- test/fixtures/product.rb
|
176
178
|
- test/fixtures/product_tariff.rb
|
177
179
|
- test/fixtures/product_tariffs.yml
|
@@ -269,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
269
271
|
version: '0'
|
270
272
|
requirements: []
|
271
273
|
rubyforge_project:
|
272
|
-
rubygems_version: 2.6.
|
274
|
+
rubygems_version: 2.6.8
|
273
275
|
signing_key:
|
274
276
|
specification_version: 4
|
275
277
|
summary: Composite key support for ActiveRecord
|