gold-record 0.1.0

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.
Files changed (42) hide show
  1. data/HISTORY.rdoc +7 -0
  2. data/LICENSE.txt +7 -0
  3. data/README.rdoc +93 -0
  4. data/Rakefile +138 -0
  5. data/lib/gold-record.rb +1 -0
  6. data/lib/gold_record.rb +26 -0
  7. data/lib/gold_record/base.rb +57 -0
  8. data/lib/gold_record/connection_adapters/mysql_adapter.rb +65 -0
  9. data/lib/gold_record/fixtures.rb +15 -0
  10. data/lib/gold_record/version.rb +9 -0
  11. data/test/cases/aaa_create_tables_test.rb +11 -0
  12. data/test/cases/acts_as_gold_record_test.rb +32 -0
  13. data/test/cases/associations/belongs_to_integer_association_test.rb +13 -0
  14. data/test/cases/associations/belongs_to_uuid_association_test.rb +13 -0
  15. data/test/cases/associations/habtm_integer_to_integer_association_test.rb +23 -0
  16. data/test/cases/associations/habtm_integer_to_uuid_association_test.rb +23 -0
  17. data/test/cases/associations/habtm_uuid_to_uuid_association_test.rb +23 -0
  18. data/test/cases/associations/has_many_integer_assocation_test.rb +16 -0
  19. data/test/cases/associations/has_many_uuid_association_test.rb +16 -0
  20. data/test/cases/coerce_id_test.rb +20 -0
  21. data/test/cases/find_test.rb +67 -0
  22. data/test/cases/generate_id_test.rb +25 -0
  23. data/test/cases/helper.rb +85 -0
  24. data/test/cases/to_param_test.rb +20 -0
  25. data/test/cases/to_uuid_test.rb +25 -0
  26. data/test/cases/zzz_migration_test.rb +97 -0
  27. data/test/config.rb +5 -0
  28. data/test/connection.rb +15 -0
  29. data/test/fixtures/albums.yml +19 -0
  30. data/test/fixtures/artists.yml +28 -0
  31. data/test/fixtures/fans.yml +23 -0
  32. data/test/fixtures/labels.yml +11 -0
  33. data/test/fixtures/record_stores.yml +12 -0
  34. data/test/fixtures/songs.yml +11 -0
  35. data/test/models/album.rb +5 -0
  36. data/test/models/artist.rb +8 -0
  37. data/test/models/fan.rb +4 -0
  38. data/test/models/label.rb +6 -0
  39. data/test/models/record_store.rb +5 -0
  40. data/test/models/song.rb +5 -0
  41. data/test/schema/schema.rb +56 -0
  42. metadata +127 -0
data/HISTORY.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ === Version 0.1.0 (git)
2
+
3
+ This is the first preview release of GoldRecord, an extension for ActiveRecord that implements unobtrusive support for binary UUIDs.
4
+
5
+ * Currently requires MySQL.
6
+ * Basic gem + project framework.
7
+ * Extracted and renamed from UniversalRecord to GoldRecord.
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright © 2009 Randy Reddig
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ = GoldRecord
2
+
3
+ * http://github.com/ydnar/gold-record
4
+ * http://gemcutter.org/gems/gold-record
5
+ * mailto:randy@shaderlab.com
6
+
7
+ == DESCRIPTION
8
+
9
+ GoldRecord is an extension for ActiveRecord that implements unobtrusive binary UUIDs[http://en.wikipedia.org/wiki/Universally_Unique_Identifier] in MySQL.
10
+
11
+ == FEATURES
12
+
13
+ * Uses space-efficient 16-byte binary representation for UUIDs.
14
+ * Works with associations, migrations and ActiveRecord::SchemaDumper.
15
+ * Transparently converts to/from hex-encoded UUIDs in #to_param and #find.
16
+
17
+ GoldRecord supports SchemaDumper by not declaring the <tt>id</tt> column a <tt>PRIMARY KEY</tt>. The odds of a random UUID collision are documented here[http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Random_UUID_probability_of_duplicates].
18
+
19
+ == SYNOPSIS
20
+
21
+ class Blog < ActiveRecord::Base
22
+ acts_as_gold_record
23
+ has_many :posts
24
+ end
25
+
26
+ class Post < ActiveRecord::Base
27
+ acts_as_gold_record
28
+ end
29
+
30
+ === Or, for the exceptionally paranoid:
31
+
32
+ class LargeHadron < ActiveRecord::Base
33
+ acts_as_gold_record
34
+ set_primary_key :lottery_number
35
+ validates_uniqueness_of :lottery_number, :on => :create
36
+ end
37
+
38
+ == MIGRATIONS
39
+
40
+ class MakeGoldRecords < ActiveRecord::Migration
41
+ def self.up
42
+ # Change each int(11) id column to binary(16).
43
+ # This preserves the column’s original value as a right-padded 16-byte string.
44
+ [:labels, :record_stores].each do |table|
45
+ change_integer_primary_key_to_uuid(table)
46
+ add_index table, :id
47
+ end
48
+
49
+ # Change association columns to binary(16).
50
+ [
51
+ [:artists, :label_id, :id],
52
+ [:labels_record_stores, :label_id, false],
53
+ [:labels_record_stores, :record_store_id, false],
54
+ [:artists_record_stores, :record_store_id, false],
55
+ ].each do |table, column, primary_key|
56
+ change_integer_to_uuid(table, column, primary_key)
57
+ end
58
+ end
59
+
60
+
61
+ # Down migration designed to be run immediately after an up migration
62
+ # in the event of some failure. Running this after generating any UUIDs
63
+ # will produce unpredictable results.
64
+
65
+ def self.down
66
+ # Change each binary(16) id column to int(11).
67
+ # MySQL casts the string value to an integer.
68
+ [:labels, :record_stores].each do |table|
69
+ remove_index table, :id
70
+ change_uuid_to_integer_primary_key(table)
71
+ end
72
+
73
+ # Change association columns to int(11).
74
+ [
75
+ [:artists, :label_id, :id],
76
+ [:labels_record_stores, :label_id, false],
77
+ [:labels_record_stores, :record_store_id, false],
78
+ [:artists_record_stores, :record_store_id, false],
79
+ ].each do |table, column|
80
+ change_uuid_to_integer(table, column, primary_key)
81
+ end
82
+ end
83
+ end
84
+
85
+ == INSTALL
86
+
87
+ === As a gem:
88
+
89
+ [sudo] gem install gold-record
90
+
91
+ === As a Rails plugin:
92
+
93
+ Clone/copy to <tt>vendor/plugins/gold_record</tt>.
data/Rakefile ADDED
@@ -0,0 +1,138 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+
8
+ require File.join(File.dirname(__FILE__), 'lib', 'gold_record', 'version')
9
+ require File.expand_path(File.dirname(__FILE__)) + '/test/config'
10
+
11
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
12
+ PKG_NAME = 'gold-record'
13
+ PKG_VERSION = GoldRecord::VERSION::STRING + PKG_BUILD
14
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
15
+
16
+ GEM_SPEC_NAME = File.join(File.dirname(__FILE__), "#{PKG_NAME}-#{PKG_VERSION}.gemspec")
17
+ GEM_NAME = File.join(File.dirname(__FILE__), "#{PKG_NAME}-#{PKG_VERSION}.gem")
18
+
19
+ RELEASE_NAME = "REL #{PKG_VERSION}"
20
+
21
+ RUBY_FORGE_PROJECT = "gold-record"
22
+ RUBY_FORGE_USER = "ydnar"
23
+
24
+ MYSQL_DB_USER = 'root'
25
+
26
+ PKG_FILES = FileList[
27
+ "lib/**/*", "test/**/*", "doc/**/*", "[A-Z]*",
28
+ "HISTORY.rdoc", "README.rdoc",
29
+ "Rakefile"
30
+ ]
31
+
32
+ desc 'Run unit tests by default'
33
+ task :default => :test
34
+
35
+ Rake::TestTask.new(:test) { |t|
36
+ t.libs << 'test'
37
+ t.test_files = Dir.glob( "test/cases/**/*_test.rb" ).sort
38
+ t.verbose = true
39
+ }
40
+
41
+ namespace :mysql do
42
+ desc 'Build the MySQL test databases'
43
+ task :build_databases do
44
+ %x( echo "create DATABASE gold_record_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci " | mysql --user=#{MYSQL_DB_USER})
45
+ end
46
+
47
+ desc 'Drop the MySQL test databases'
48
+ task :drop_databases do
49
+ %x( mysqladmin --user=#{MYSQL_DB_USER} -f drop gold_record_test )
50
+ end
51
+
52
+ desc 'Rebuild the MySQL test databases'
53
+ task :rebuild_databases => [:drop_databases, :build_databases]
54
+ end
55
+
56
+ task :build_mysql_databases => 'mysql:build_databases'
57
+ task :drop_mysql_databases => 'mysql:drop_databases'
58
+ task :rebuild_mysql_databases => 'mysql:rebuild_databases'
59
+
60
+
61
+ # Generate the RDoc documentation
62
+
63
+ Rake::RDocTask.new { |rdoc|
64
+ rdoc.rdoc_dir = 'doc'
65
+ rdoc.title = "GoldRecord -- Object-relation mapping put on rails"
66
+ rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
67
+ rdoc.options << '--charset' << 'utf-8'
68
+ rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo'
69
+ rdoc.rdoc_files.include('README.txt', 'HISTORY.rdoc', 'LICENSE.txt')
70
+ rdoc.rdoc_files.include('lib/**/*.rb')
71
+ }
72
+
73
+
74
+ # Create compressed packages
75
+
76
+ dist_dirs = [ "lib", "test" ]
77
+
78
+ spec = Gem::Specification.new do |s|
79
+ s.name = PKG_NAME
80
+ s.version = PKG_VERSION
81
+ s.summary = "Binary UUID support for ActiveRecord"
82
+ s.description = "Unobtrusive binary UUID support for ActiveRecord. Supports migrations, reflections, assocations and SchemaDumper."
83
+ s.author = "Randy Reddig"
84
+ s.email = "randy@shaderlab.com"
85
+ s.homepage = "http://github.com/ydnar/gold-record"
86
+ s.rubyforge_project = "gold-record"
87
+
88
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
89
+
90
+ s.extra_rdoc_files = ["HISTORY.rdoc", "LICENSE.txt", "README.rdoc"]
91
+ s.files = [ "Rakefile", "LICENSE.txt", "README.rdoc", "HISTORY.rdoc" ]
92
+ dist_dirs.each do |dir|
93
+ s.files = s.files + Dir.glob( "#{dir}/**/*" )
94
+ end
95
+
96
+ s.has_rdoc = true
97
+ s.rdoc_options.concat ["--main", "README.rdoc"]
98
+ s.require_paths = ["lib"]
99
+ s.rubygems_version = "1.3.5"
100
+
101
+ if s.respond_to? :specification_version then
102
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
103
+ s.specification_version = 3
104
+
105
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
106
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.3.4"])
107
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
108
+ s.add_runtime_dependency(%q<uuidtools>, [">= 2.0.0"])
109
+ else
110
+ s.add_dependency(%q<activerecord>, [">= 2.3.4"])
111
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
112
+ s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
113
+ end
114
+ else
115
+ s.add_dependency(%q<activerecord>, [">= 2.3.4"])
116
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
117
+ s.add_dependency(%q<uuidtools>, [">= 2.0.0"])
118
+ end
119
+ end
120
+
121
+ namespace :gem do
122
+ desc "Print gemspec"
123
+ task :spec do
124
+ open GEM_SPEC_NAME, "wb" do |file|
125
+ file.write(spec.to_ruby)
126
+ end
127
+ end
128
+
129
+ desc "Build gem with Gemcutter"
130
+ task :build => :spec do
131
+ system "gem build #{GEM_SPEC_NAME}"
132
+ end
133
+
134
+ desc "Push gem to Gemcutter"
135
+ task :push => :build do
136
+ system "gem push #{GEM_NAME}"
137
+ end
138
+ end
@@ -0,0 +1 @@
1
+ require 'gold_record'
@@ -0,0 +1,26 @@
1
+ #--
2
+ # Copyright (c) 2009 Randy Reddig
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'gold_record/base'
25
+ require 'gold_record/fixtures'
26
+ require 'gold_record/connection_adapters/mysql_adapter'
@@ -0,0 +1,57 @@
1
+ require 'uuidtools'
2
+
3
+ module GoldRecord
4
+ module ClassMethods
5
+ def gold_record?
6
+ true
7
+ end
8
+
9
+ def coerce_id(id)
10
+ if id.is_a?(String) && id.size == 16
11
+ id
12
+ else
13
+ UUIDTools::UUID.parse(id).raw rescue nil
14
+ end
15
+ end
16
+
17
+ def find_one_with_uuid(id, options)
18
+ find_one_without_uuid(coerce_id(id), options)
19
+ end
20
+
21
+ def find_some_with_uuid(ids, options)
22
+ ids = ids.map { |id| coerce_id(id) }
23
+ ids = ids.uniq # must do this after coercion
24
+ find_some_without_uuid(ids, options)
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+ def to_uuid
30
+ UUIDTools::UUID.parse_raw(id) rescue nil
31
+ end
32
+
33
+ def to_param_with_uuid
34
+ uuid = to_uuid
35
+ uuid ? uuid.to_param : nil
36
+ end
37
+
38
+ def generate_id!
39
+ self[self.class.primary_key] ||= UUIDTools::UUID.random_create.raw
40
+ end
41
+ end
42
+
43
+ module ActMethods
44
+ def acts_as_gold_record
45
+ extend ClassMethods
46
+ class << self
47
+ alias_method_chain :find_one, :uuid
48
+ alias_method_chain :find_some, :uuid
49
+ end
50
+ include InstanceMethods
51
+ alias_method_chain :to_param, :uuid
52
+ before_create :generate_id!
53
+ end
54
+ end
55
+ end
56
+
57
+ ActiveRecord::Base.extend GoldRecord::ActMethods
@@ -0,0 +1,65 @@
1
+ require 'active_record/connection_adapters/mysql_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ class MysqlAdapter < AbstractAdapter
6
+
7
+ # MySQL can store UUIDs in binary(16) instead of a blob column.
8
+ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
9
+ # New functionality to handle limit on binary columns
10
+ return "binary(#{limit})" if type.to_s == 'binary' && !limit.blank?
11
+
12
+ # Exact implementation from ActiveRecord 2.3.4
13
+ return super unless type.to_s == 'integer'
14
+
15
+ case limit
16
+ when 1; 'tinyint'
17
+ when 2; 'smallint'
18
+ when 3; 'mediumint'
19
+ when nil, 4, 11; 'int(11)' # compatibility with MySQL default
20
+ when 5..8; 'bigint'
21
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}")
22
+ end
23
+ end
24
+
25
+ def change_integer_primary_key_to_uuid(table, column = :id)
26
+ table = table.to_s
27
+ column = column.to_s
28
+ execute "ALTER TABLE #{table} ADD COLUMN #{column}_new BINARY(16) FIRST"
29
+ execute "UPDATE #{table} SET #{column}_new = #{column}"
30
+ execute "ALTER TABLE #{table} DROP COLUMN #{column}"
31
+ execute "ALTER TABLE #{table} CHANGE COLUMN #{column}_new #{column} BINARY(16) FIRST"
32
+ end
33
+
34
+ def change_uuid_to_integer_primary_key(table, column = :id)
35
+ table = table.to_s
36
+ column = column.to_s
37
+ execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column}_old BINARY(16)"
38
+ execute "ALTER TABLE #{table} ADD COLUMN #{column} int(11)"
39
+ execute "UPDATE #{table} SET #{column} = #{column}_old"
40
+ execute "ALTER TABLE #{table} DROP COLUMN #{column}_old"
41
+ execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column} #{NATIVE_DATABASE_TYPES[:primary_key]} FIRST"
42
+ end
43
+
44
+ def change_integer_to_uuid(table, column = :id, primary_key = :id)
45
+ table = table.to_s
46
+ column = column.to_s
47
+ after_clause = primary_key ? "AFTER #{primary_key}" : ""
48
+ execute "ALTER TABLE #{table} ADD COLUMN #{column}_new BINARY(16)"
49
+ execute "UPDATE #{table} SET #{column}_new = #{column}"
50
+ execute "ALTER TABLE #{table} DROP COLUMN #{column}"
51
+ execute "ALTER TABLE #{table} CHANGE COLUMN #{column}_new #{column} BINARY(16) #{after_clause}"
52
+ end
53
+
54
+ def change_uuid_to_integer(table, column = :id, primary_key = :id)
55
+ table = table.to_s
56
+ column = column.to_s
57
+ after_clause = primary_key ? "AFTER #{primary_key}" : ""
58
+ execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column}_old BINARY(16)"
59
+ execute "ALTER TABLE #{table} ADD COLUMN #{column} int(11) #{after_clause}"
60
+ execute "UPDATE #{table} SET #{column} = #{column}_old"
61
+ execute "ALTER TABLE #{table} DROP COLUMN #{column}_old"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,15 @@
1
+ module GoldRecord
2
+ module Fixtures
3
+ def self.included(base)
4
+ base.class_eval do
5
+ class << self
6
+ # Patch Fixtures.identify for binary UUIDs.
7
+ def identify_with_padding(label)
8
+ ("%-16d" % identify_without_padding(label)).tr(" ", "\0")
9
+ end
10
+ alias_method_chain :identify, :padding
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module GoldRecord
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # The filename begins with "aaa" to ensure this is the first test.
2
+ require 'cases/helper'
3
+
4
+ class AAACreateTablesTest < ActiveRecord::TestCase
5
+ self.use_transactional_fixtures = false
6
+
7
+ def test_load_schema
8
+ eval(File.read(SCHEMA_ROOT + "/schema.rb"))
9
+ assert true
10
+ end
11
+ end