composite_primary_keys 0.1.3

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.
@@ -0,0 +1,9 @@
1
+ module CompositePrimayKeys
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 3
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,69 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib')
3
+
4
+ require 'test/unit'
5
+ require 'active_record'
6
+ require 'active_record/fixtures'
7
+ require 'active_support/binding_of_caller'
8
+ require 'active_support/breakpoint'
9
+ require 'connection'
10
+ require 'composite_primary_keys'
11
+ require 'composite_primary_keys/fixtures'
12
+
13
+ QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
14
+
15
+ class Test::Unit::TestCase #:nodoc:
16
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
17
+ self.use_instantiated_fixtures = false
18
+ self.use_transactional_fixtures = (ENV['AR_NO_TX_FIXTURES'] != "yes")
19
+
20
+ def create_fixtures(*table_names, &block)
21
+ Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block)
22
+ end
23
+
24
+ def assert_date_from_db(expected, actual, message = nil)
25
+ # SQL Server doesn't have a separate column type just for dates,
26
+ # so the time is in the string and incorrectly formatted
27
+ if current_adapter?(:SQLServerAdapter)
28
+ assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
29
+ elsif current_adapter?(:SybaseAdapter)
30
+ assert_equal expected.to_s, actual.to_date.to_s, message
31
+ else
32
+ assert_equal expected.to_s, actual.to_s, message
33
+ end
34
+ end
35
+
36
+ def assert_queries(num = 1)
37
+ ActiveRecord::Base.connection.class.class_eval do
38
+ self.query_count = 0
39
+ alias_method :execute, :execute_with_query_counting
40
+ end
41
+ yield
42
+ ensure
43
+ ActiveRecord::Base.connection.class.class_eval do
44
+ alias_method :execute, :execute_without_query_counting
45
+ end
46
+ assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
47
+ end
48
+
49
+ def assert_no_queries(&block)
50
+ assert_queries(0, &block)
51
+ end
52
+ end
53
+
54
+ def current_adapter?(type)
55
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
56
+ ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
57
+ end
58
+
59
+ ActiveRecord::Base.connection.class.class_eval do
60
+ cattr_accessor :query_count
61
+ alias_method :execute_without_query_counting, :execute
62
+ def execute_with_query_counting(sql, name = nil)
63
+ self.query_count += 1
64
+ execute_without_query_counting(sql, name)
65
+ end
66
+ end
67
+
68
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
69
+ #ActiveRecord::Base.colorize_logging = false
@@ -0,0 +1,13 @@
1
+ print "Using native MySQL\n"
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ db1 = 'composite_primary_keys_unittest'
7
+
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => "mysql",
10
+ :username => "root",
11
+ :encoding => "utf8",
12
+ :database => db1
13
+ )
@@ -0,0 +1,8 @@
1
+ require 'abstract_unit'
2
+
3
+ class DummyTest < Test::Unit::TestCase
4
+
5
+ def test_truth
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,88 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ # Testing the find action on composite ActiveRecords with two primary keys
6
+ class FindTest < Test::Unit::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
+ }
19
+
20
+ def test_primary_keys
21
+ testing_with do
22
+ if composite?
23
+ assert_equal @primary_keys, @klass.primary_keys
24
+ else
25
+ assert_equal @primary_keys, [@klass.primary_key.to_sym]
26
+ end
27
+ end
28
+ end
29
+
30
+ def test_find_first
31
+ testing_with do
32
+ obj = @klass.find_first
33
+ assert obj
34
+ assert_equal @klass, obj.class
35
+ end
36
+ end
37
+
38
+ def test_ids
39
+ testing_with do
40
+ assert_equal @first.id, @first.ids if composite?
41
+ end
42
+ end
43
+
44
+ def test_find
45
+ testing_with do
46
+ found = @klass.find(*first_id) # e.g. find(1,1) or find 1,1
47
+ assert found
48
+ assert_equal @klass, found.class
49
+ #breakpoint
50
+ assert_equal found, @klass.find(found.id)
51
+ assert_equal found, @klass.find(found.to_param)
52
+ end
53
+ end
54
+
55
+ def test_to_param
56
+ testing_with do
57
+ assert_equal first_id_str, @first.to_param.to_s
58
+ end
59
+ end
60
+
61
+ def things_to_look_at
62
+ testing_with do
63
+ assert_equal found, @klass.find(found.id.to_s) # fails for 2+ keys
64
+ end
65
+ end
66
+
67
+ protected
68
+
69
+ def testing_with(&block)
70
+ CLASSES.keys.each do |@key_test|
71
+ @klass, @primary_keys = CLASSES[@key_test][:class], CLASSES[@key_test][:primary_keys]
72
+ @first = @klass.find_first
73
+ yield
74
+ end
75
+ end
76
+
77
+ def first_id
78
+ (1..@primary_keys.length).map {|num| 1}
79
+ end
80
+
81
+ def first_id_str
82
+ first_id.join(',')
83
+ end
84
+
85
+ def composite?
86
+ @key_test != :single
87
+ end
88
+ end
@@ -0,0 +1,30 @@
1
+ DROP TABLE accounts;
2
+ DROP TABLE funny_jokes;
3
+ DROP TABLE companies;
4
+ DROP TABLE topics;
5
+ DROP TABLE developers;
6
+ DROP TABLE projects;
7
+ DROP TABLE developers_projects;
8
+ DROP TABLE customers;
9
+ DROP TABLE orders;
10
+ DROP TABLE movies;
11
+ DROP TABLE subscribers;
12
+ DROP TABLE booleantests;
13
+ DROP TABLE auto_id_tests;
14
+ DROP TABLE entrants;
15
+ DROP TABLE colnametests;
16
+ DROP TABLE mixins;
17
+ DROP TABLE people;
18
+ DROP TABLE readers;
19
+ DROP TABLE binaries;
20
+ DROP TABLE computers;
21
+ DROP TABLE tasks;
22
+ DROP TABLE posts;
23
+ DROP TABLE comments;
24
+ DROP TABLE authors;
25
+ DROP TABLE categories;
26
+ DROP TABLE categories_posts;
27
+ DROP TABLE fk_test_has_fk;
28
+ DROP TABLE fk_test_has_pk;
29
+ DROP TABLE keyboards;
30
+ DROP TABLE legacy_things;
@@ -0,0 +1,16 @@
1
+ CREATE TABLE `reference_types` (
2
+ `reference_type_id` int(11) NOT NULL auto_increment,
3
+ `type_label` varchar(50) default NULL,
4
+ `abbreviation` varchar(50) default NULL,
5
+ `description` varchar(50) default NULL,
6
+ PRIMARY KEY (`reference_type_id`)
7
+ ) TYPE=InnoDB;
8
+
9
+ CREATE TABLE `reference_codes` (
10
+ `reference_type_id` int(11) NOT NULL,
11
+ `reference_code` int(11) NOT NULL,
12
+ `code_label` varchar(50) default NULL,
13
+ `abbreviation` varchar(50) default NULL,
14
+ `description` varchar(50) default NULL,
15
+ PRIMARY KEY (`reference_type_id`,`reference_code`)
16
+ ) TYPE=InnoDB;
@@ -0,0 +1,2 @@
1
+ DROP TABLE courses;
2
+
@@ -0,0 +1,5 @@
1
+ CREATE TABLE `courses` (
2
+ `id` INTEGER NOT NULL PRIMARY KEY,
3
+ `name` VARCHAR(255) NOT NULL
4
+ ) TYPE=InnoDB;
5
+
@@ -0,0 +1,7 @@
1
+ class ReferenceCode < ActiveRecord::Base
2
+ set_primary_keys :reference_type_id, :reference_code
3
+
4
+ belongs_to :reference_type, :foreign_key => "reference_type_id"
5
+
6
+ validates_presence_of :reference_code, :code_label, :abbreviation
7
+ end
@@ -0,0 +1,28 @@
1
+ name_prefix_mr:
2
+ reference_type_id: 1
3
+ reference_code: 1
4
+ code_label: MR
5
+ abbreviation: Mr
6
+ name_prefix_mrs:
7
+ reference_type_id: 1
8
+ reference_code: 2
9
+ code_label: MRS
10
+ abbreviation: Mrs
11
+ name_prefix_ms:
12
+ reference_type_id: 1
13
+ reference_code: 3
14
+ code_label: MS
15
+ abbreviation: Ms
16
+
17
+ gender_male:
18
+ reference_type_id: 2
19
+ reference_code: 1
20
+ code_label: MALE
21
+ abbreviation: Male
22
+ gender_female:
23
+ reference_type_id: 2
24
+ reference_code: 2
25
+ code_label: FEMALE
26
+ abbreviation: Female
27
+
28
+
@@ -0,0 +1,7 @@
1
+ class ReferenceType < ActiveRecord::Base
2
+ set_primary_key :reference_type_id
3
+ has_many :reference_codes, :foreign_key => "reference_type_id"
4
+
5
+ validates_presence_of :type_label, :abbreviation
6
+ validates_uniqueness_of :type_label
7
+ end
@@ -0,0 +1,9 @@
1
+ name_prefix:
2
+ reference_type_id: 1
3
+ type_label: NAME_PREFIX
4
+ abbreviation: Name Prefix
5
+
6
+ gender:
7
+ reference_type_id: 2
8
+ type_label: GENDER
9
+ abbreviation: Gender
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: composite_primary_keys
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.3
7
+ date: 2006-07-22 00:00:00 +02:00
8
+ summary: Support for composite primary keys in ActiveRecords
9
+ require_paths:
10
+ - lib
11
+ email: drnicwilliams@gmail.com
12
+ homepage: http://composite_primary_keys.rubyforge.org
13
+ rubyforge_project: composite_primary_keys
14
+ description: ActiveRecords only support a single primary key, preventing their use on legacy databases where tables have primary keys over 2+ columns. This solution allows an ActiveRecord to be extended to support multiple keys using the class method set_primary_keys.
15
+ autorequire: composite_primary_keys
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Dr Nic Williams
30
+ files:
31
+ - Rakefile
32
+ - install.rb
33
+ - README
34
+ - CHANGELOG
35
+ - lib/composite_primary_keys
36
+ - lib/composite_primary_keys.rb
37
+ - lib/composite_primary_keys/version.rb
38
+ - lib/composite_primary_keys/base.rb
39
+ - lib/composite_primary_keys/fixtures.rb
40
+ - test/connections
41
+ - test/abstract_unit.rb
42
+ - test/dummy_test.rb
43
+ - test/fixtures
44
+ - test/find_test.rb
45
+ - test/connections/native_mysql
46
+ - test/connections/native_mysql/connection.rb
47
+ - test/fixtures/reference_type.rb
48
+ - test/fixtures/reference_code.rb
49
+ - test/fixtures/reference_types.yml
50
+ - test/fixtures/reference_codes.yml
51
+ - test/fixtures/db_definitions
52
+ - test/fixtures/db_definitions/mysql.drop.sql
53
+ - test/fixtures/db_definitions/mysql.sql
54
+ - test/fixtures/db_definitions/mysql2.drop.sql
55
+ - test/fixtures/db_definitions/mysql2.sql
56
+ test_files: []
57
+
58
+ rdoc_options:
59
+ - --main
60
+ - README
61
+ extra_rdoc_files:
62
+ - README
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ requirements: []
68
+
69
+ dependencies:
70
+ - !ruby/object:Gem::Dependency
71
+ name: activerecord
72
+ version_requirement:
73
+ version_requirements: !ruby/object:Gem::Version::Requirement
74
+ requirements:
75
+ - - "="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.14.3
78
+ version: