hydra-migrate 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +3 -0
- data/README.md +4 -2
- data/lib/hydra/migrate/dispatcher.rb +19 -0
- data/lib/hydra/migrate/version.rb +1 -1
- data/lib/railties/hydra-migrate.rake +9 -15
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/migrate_spec.rb +18 -0
- metadata +2 -2
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -57,13 +57,15 @@ Or install it yourself as:
|
|
57
57
|
$ rake hydra:migrate
|
58
58
|
|
59
59
|
# Migrate one particular class of objects
|
60
|
-
$ rake hydra:migrate
|
60
|
+
$ rake hydra:migrate models=MyModel
|
61
|
+
|
62
|
+
# Target a specific version
|
63
|
+
$ rake hydra:migrate to=3
|
61
64
|
|
62
65
|
## Todo
|
63
66
|
|
64
67
|
* Reversible migrations (rollback)
|
65
68
|
* Improved rake task(s):
|
66
|
-
* Specify target version (already possible in raw code)
|
67
69
|
* Migrate specific object, not entire model class
|
68
70
|
|
69
71
|
## Contributing
|
@@ -3,6 +3,10 @@ require 'active_support/core_ext/class/subclasses'
|
|
3
3
|
module Hydra
|
4
4
|
module Migrate
|
5
5
|
class Dispatcher
|
6
|
+
def initialize(path=nil)
|
7
|
+
self.load_migrations(path) unless path.nil?
|
8
|
+
end
|
9
|
+
|
6
10
|
def migrations
|
7
11
|
@migrations || reset!
|
8
12
|
end
|
@@ -66,6 +70,21 @@ module Hydra
|
|
66
70
|
object
|
67
71
|
}
|
68
72
|
end
|
73
|
+
|
74
|
+
def self.migrate_all!(*args, &block)
|
75
|
+
opts = {path: '.'}
|
76
|
+
opts.merge!(args.pop) if args.last.is_a?(Hash)
|
77
|
+
dispatcher = self.new(opts[:path])
|
78
|
+
models = args
|
79
|
+
models << ActiveFedora::Base if models.empty?
|
80
|
+
models.flatten.each do |klass|
|
81
|
+
klass.find_each({},{:cast=>true}) do |obj|
|
82
|
+
while dispatcher.can_migrate? obj and (opts[:to].nil? or obj.current_migration != opts[:to])
|
83
|
+
dispatcher.migrate!(obj, &block)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
69
88
|
end
|
70
89
|
end
|
71
90
|
end
|
@@ -1,20 +1,14 @@
|
|
1
1
|
namespace :hydra do
|
2
2
|
desc "Run ActiveFedora model migrations"
|
3
|
-
task :migrate
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
16
|
-
end
|
17
|
-
end
|
3
|
+
task :migrate => :environment do |t,args|
|
4
|
+
models = env['models'].split(/[,;\s]+/).collect do |model_name|
|
5
|
+
model_name.split(/::/).inject(Module) { |k,c| k.const_get(c.to_sym) }
|
6
|
+
end
|
7
|
+
target_version = env['to']
|
8
|
+
Hydra::Migrate::Dispatcher.migrate_all!(models, to: target_version, path: File.join(Rails.root,'db/hydra')) do |o,m,d|
|
9
|
+
current = o.current_migration
|
10
|
+
current = 'unknown version' if current.blank?
|
11
|
+
$stderr.puts "Migrating #{o.class} #{o.pid} from #{current} to #{m[:to]}"
|
18
12
|
end
|
19
13
|
end
|
20
14
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/unit/migrate_spec.rb
CHANGED
@@ -52,6 +52,10 @@ describe Hydra::Migrate do
|
|
52
52
|
@migration = TestClassMigration.new(migrator)
|
53
53
|
end
|
54
54
|
|
55
|
+
after :each do
|
56
|
+
Object.send(:remove_const, :TestClassMigration)
|
57
|
+
end
|
58
|
+
|
55
59
|
it "should target the right class" do
|
56
60
|
TestClassMigration.target_class.should eq(TestClass)
|
57
61
|
end
|
@@ -130,4 +134,18 @@ describe Hydra::Migrate do
|
|
130
134
|
expect { IncorrectTestClassMigration.migrate(1=>2,2=>3) { } }.to raise_error(ArgumentError)
|
131
135
|
end
|
132
136
|
end
|
137
|
+
|
138
|
+
describe "class methods" do
|
139
|
+
it ".migrate_all!" do
|
140
|
+
receiver = double('receiver')
|
141
|
+
receiver.should_receive(:check).once
|
142
|
+
ActiveFedora::Base.stub(:find_each).and_yield(subject)
|
143
|
+
expect(subject.current_migration).to eq('')
|
144
|
+
Hydra::Migrate::Dispatcher.migrate_all!(TestClass, to: '1', path: File.expand_path('../../fixtures/db/hydra',__FILE__)) do |o,m,d|
|
145
|
+
receiver.check(o,m,d)
|
146
|
+
end
|
147
|
+
expect(subject.myMetadata.migrated).to eq(['yep'])
|
148
|
+
expect(subject.current_migration).to eq('1')
|
149
|
+
end
|
150
|
+
end
|
133
151
|
end
|
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.
|
4
|
+
version: 0.2.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-
|
12
|
+
date: 2013-08-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active-fedora
|