hydra-migrate 0.0.1 → 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.
data/HISTORY.md ADDED
@@ -0,0 +1,5 @@
1
+ * 0.1.0 (19 Aug 2013) [Michael Klein]
2
+ * Initial usable release
3
+
4
+ * 0.0.1 (21 Aug 2013) [Michael Klein]
5
+ * Initial alpha release
data/README.md CHANGED
@@ -68,8 +68,4 @@ Or install it yourself as:
68
68
 
69
69
  ## Contributing
70
70
 
71
- 1. Fork it
72
- 2. Create your feature branch (`git checkout -b my-new-feature`)
73
- 3. Commit your changes (`git commit -am 'Add some feature'`)
74
- 4. Push to the branch (`git push origin my-new-feature`)
75
- 5. Create new Pull Request
71
+ See [CONTRIBUTING.md](CONTRIBUTING.md)
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'active-fedora'
22
+ spec.add_dependency 'activesupport'
22
23
  spec.add_development_dependency "bundler", "~> 1.3"
23
24
  spec.add_development_dependency "rake"
24
25
  spec.add_development_dependency "rspec"
@@ -8,9 +8,9 @@ module Hydra
8
8
  t.current
9
9
  t.history do
10
10
  t.migration do
11
- t.from(:path=>'@from')
12
- t.to(:path=>'@to')
13
- t.at(:path=>'@at')
11
+ t.from(:path=>'@from', :namespace_prefix=>nil)
12
+ t.to(:path=>'@to', :namespace_prefix=>nil)
13
+ t.at(:path=>'@at', :namespace_prefix=>nil)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/class/subclasses'
2
+
1
3
  module Hydra
2
4
  module Migrate
3
5
  class Dispatcher
@@ -11,16 +13,20 @@ module Hydra
11
13
  end
12
14
 
13
15
  def load_migrations(path)
16
+ result = []
14
17
  Dir[File.join(path,'**','*.rb')].each { |migration_file|
15
- existing_migrations = ObjectSpace.each_object(Hydra::Migrate::Migration).to_a
18
+ existing_migrations = Hydra::Migrate::Migration.descendants
16
19
  load(migration_file)
17
- new_migrations = ObjectSpace.each_object(Hydra::Migrate::Migration).to_a - existing_migrations
20
+ new_migrations = Hydra::Migrate::Migration.descendants - existing_migrations
18
21
  new_migrations.each { |klass| klass.new(self) }
22
+ result = new_migrations
19
23
  }
24
+ result
20
25
  end
21
26
 
22
27
  def define_migration(signature={}, block)
23
- self.migrations[signature[:for]] << { :from=>signature[:from].to_s, :to=>signature[:to].to_s, :block=>block }
28
+ memo = { :from=>signature[:from].to_s, :to=>signature[:to].to_s, :block=>block }
29
+ self.migrations[signature[:for]] << memo unless self.migrations[signature[:for]].include?(memo)
24
30
  end
25
31
 
26
32
  def migrations_for(target, constraints={})
@@ -38,15 +44,27 @@ module Hydra
38
44
  object.is_a?(Hydra::ModelMixins::Migratable) and not migrations_for(object, {:from=>object.current_migration}.merge(constraints)).empty?
39
45
  end
40
46
 
41
- def migrate!(object, to=nil, opts={})
42
- raise "Not a migratable object: #{object.inspect}" unless object.is_a?(Hydra::ModelMixins::Migratable)
43
- migrations_for(object, :from=>object.current_migration, :to=>to).each do |migration|
44
- migration[:block].call(object, to, self)
45
- object.migrationInfo.migrate(migration[:to])
46
- object.current_migration = migration[:to]
47
- object.save unless opts[:dry_run]
48
- end
49
- object
47
+ def migrate!(*args)
48
+ opts = args.last.is_a?(Hash) ? args.pop : {}
49
+ objects=args.flatten
50
+ objects.each { |object|
51
+ raise "Not a migratable object: #{object.inspect}" unless object.is_a?(Hydra::ModelMixins::Migratable)
52
+ }
53
+
54
+ objects.collect { |object|
55
+ migrations_for(object, :from=>object.current_migration, :to=>opts[:to]).each do |migration|
56
+ yield(object,migration,self) if block_given?
57
+ migration[:block].call(object, migration[:to], self)
58
+ object.migrationInfo.migrate(migration[:to])
59
+ object.current_migration = migration[:to]
60
+ unless opts[:dry_run]
61
+ unless object.save
62
+ raise %{Cannot save #{object.pid}:\n#{object.errors.to_a.join("\n")}}
63
+ end
64
+ end
65
+ end
66
+ object
67
+ }
50
68
  end
51
69
  end
52
70
  end
@@ -1,5 +1,5 @@
1
1
  module Hydra
2
2
  module Migrate
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -5,11 +5,14 @@ namespace :hydra do
5
5
  migrator.load_migrations(File.join(Rails.root,'db/hydra'))
6
6
  models = (args[:models] || 'ActiveFedora::Base').split(/[,;\s]+/)
7
7
  models.each do |model|
8
- klass = model.split(/::/).inject(Module) { |k,c| k.const_find(c.to_sym) }
9
- klass.find(:all).each do |obj|
8
+ klass = model.split(/::/).inject(Module) { |k,c| k.const_get(c.to_sym) }
9
+ klass.find_each({},{:cast=>true}) do |obj|
10
10
  while migrator.can_migrate? obj
11
- $stderr.puts "Migrating #{obj.class} #{obj.pid} from #{obj.current_migration.inspect} to #{version.inspect}"
12
- migrator.migrate! obj
11
+ migrator.migrate!(obj) do |o,m,d|
12
+ current = o.current_migration
13
+ current = 'unknown version' if current.blank?
14
+ $stderr.puts "Migrating #{o.class} #{o.pid} from #{current} to #{m[:to]}"
15
+ end
13
16
  end
14
17
  end
15
18
  end
@@ -74,6 +74,20 @@ describe Hydra::Migrate do
74
74
  expect(subject.myMetadata.migrated).to eq(['yep, YEP!'])
75
75
  expect(subject.current_migration).to eq('2')
76
76
  end
77
+
78
+ it "should migrate multiple objects" do
79
+ subject_2 = subject.class.new
80
+ expect(subject.current_migration).to be_blank
81
+ expect(subject.myMetadata.migrated).to be_blank
82
+ expect(subject_2.current_migration).to be_blank
83
+ expect(subject_2.myMetadata.migrated).to be_blank
84
+
85
+ migrator.migrate!([subject, subject_2], :to=>1)
86
+ expect(subject.myMetadata.migrated).to eq(['yep'])
87
+ expect(subject.current_migration).to eq('1')
88
+ expect(subject_2.myMetadata.migrated).to eq(['yep'])
89
+ expect(subject_2.current_migration).to eq('1')
90
+ end
77
91
  end
78
92
  end
79
93
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydra-migrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-20 00:00:00.000000000 Z
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active-fedora
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: bundler
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +119,7 @@ files:
103
119
  - .travis.yml
104
120
  - CONTRIBUTING.md
105
121
  - Gemfile
122
+ - HISTORY.md
106
123
  - LICENSE.txt
107
124
  - README.md
108
125
  - Rakefile