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 CHANGED
@@ -1,3 +1,6 @@
1
+ * 0.2.0 (27 Aug 2013) [Michael Klein]
2
+ * Add Dispatcher#migrate_all!(*classes, :to=>version, :path=>path_to_migrations)
3
+
1
4
  * 0.1.0 (19 Aug 2013) [Michael Klein]
2
5
  * Initial usable release
3
6
 
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[MyModel]
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,5 +1,5 @@
1
1
  module Hydra
2
2
  module Migrate
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,20 +1,14 @@
1
1
  namespace :hydra do
2
2
  desc "Run ActiveFedora model migrations"
3
- task :migrate, [:models] => :environment do |t,args|
4
- migrator = Hydra::Migrate::Dispatcher.new
5
- migrator.load_migrations(File.join(Rails.root,'db/hydra'))
6
- models = (args[:models] || 'ActiveFedora::Base').split(/[,;\s]+/)
7
- models.each do |model|
8
- klass = model.split(/::/).inject(Module) { |k,c| k.const_get(c.to_sym) }
9
- klass.find_each({},{:cast=>true}) do |obj|
10
- while migrator.can_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
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
@@ -1,7 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
- ENV["RAILS_ENV"] ||= "test"
4
+ ENV["environment"] ||= "test"
5
5
 
6
6
  require 'bundler/setup'
7
7
  require 'rspec'
@@ -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.1.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-21 00:00:00.000000000 Z
12
+ date: 2013-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active-fedora