Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added minimal support for data migrations.
- Loading branch information
Showing
12 changed files
with
240 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module RelaxDB | ||
|
||
class Migration | ||
|
||
def self.run klass, limit = 1000 | ||
query = lambda do |page_params| | ||
RelaxDB.paginate_view "#{klass}_all", :startkey => nil, :endkey => {}, :attributes => [:_id], | ||
:page_params => page_params, :limit => limit | ||
end | ||
|
||
objs = query.call({}) | ||
until objs.empty? | ||
migrated = objs.map { |o| yield o }.flatten.reject { |o| o.nil? } | ||
RelaxDB.bulk_save! *migrated | ||
objs = objs.next_params ? query.call(objs.next_params) : [] | ||
end | ||
end | ||
|
||
# | ||
# Runs all outstanding migrations in a given directory | ||
# | ||
# ==== Example | ||
# RelaxDB::Migration.run_all Dir["couchdb/migrations/**/*.rb"] | ||
# | ||
def self.run_all file_names, action = lambda { |fn| require fn } | ||
v = RelaxDB::MigrationVersion.version | ||
file_names.select { |fn| fv(fn) > v }.each do |fn| | ||
RelaxDB.logger.info "Applying #{fn}" | ||
action.call fn | ||
RelaxDB::MigrationVersion.update fv(fn) | ||
end | ||
end | ||
|
||
def self.fv file_name | ||
File.basename(file_name).split("_")[0].to_i | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class RelaxDB::MigrationVersion < RelaxDB::Document | ||
|
||
DOC_ID = "relaxdb_migration_version" | ||
|
||
property :version, :default => 0 | ||
|
||
def self.version | ||
retrieve.version | ||
end | ||
|
||
def self.update v | ||
mv = retrieve | ||
mv.version = v | ||
mv.save! | ||
end | ||
|
||
def self.retrieve | ||
(v = RelaxDB.load(DOC_ID)) ? v : new(:_id => DOC_ID).save! | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require File.join(File.dirname(__FILE__), "spec_helper") | ||
require File.join(File.dirname(__FILE__), "spec_models") | ||
|
||
describe RelaxDB::Migration do | ||
|
||
before(:each) do | ||
@mig = RelaxDB::Migration | ||
setup_test_db | ||
end | ||
|
||
it "should yield each obj to the block and save the result" do | ||
Primitives.new(:num => 5).save! | ||
r = @mig.run Primitives do |p| | ||
pn = Primitives.new(:num => p.num * 2) | ||
[p, pn] | ||
end | ||
Primitives.by_num.map { |p| p.num}.should == [5, 10] | ||
end | ||
|
||
it "should raise an exception if a save results in a conflict" do | ||
op = Primitives.new.save! | ||
lambda do | ||
@mig.run Primitives do |p| | ||
op.save! | ||
p | ||
end | ||
end.should raise_error(RelaxDB::UpdateConflict) | ||
end | ||
|
||
it "should not save docs for blocks that return nil" do | ||
Primitives.new.save! | ||
@mig.run Primitives do |p| | ||
nil | ||
end | ||
end | ||
|
||
describe "multiple docs" do | ||
|
||
before(:each) do | ||
ps = (1..5).map { |i| Primitives.new :num => i } | ||
RelaxDB.bulk_save *ps | ||
RelaxDB.db.reset_req_count | ||
end | ||
|
||
# Note: two extra requests per paginate_view request are required | ||
it "should operate on a doc set of the given size aka limit" do | ||
@mig.run(Primitives, 1) { |p| p.num *= p.num; p } | ||
RelaxDB.db.req_count.should == 10 + 5 * 2 | ||
Primitives.by_num.map { |p| p.num }.should == [1, 4, 9, 16, 25] | ||
end | ||
|
||
it "should operate on a doc set of default size" do | ||
@mig.run(Primitives) { |p| p.num *= p.num; p } | ||
RelaxDB.db.req_count.should == 2 + 1 * 2 | ||
Primitives.by_num.map { |p| p.num }.should == [1, 4, 9, 16, 25] | ||
end | ||
|
||
end | ||
|
||
describe "#fv" do | ||
it "should return valid numbers" do | ||
@mig.fv("foo/bar/001_foo.rb").should == 1 | ||
end | ||
end | ||
|
||
describe ".run_all" do | ||
|
||
it "should save the version after each successful migration" do | ||
@mig.run_all "a/b/1_", lambda {} | ||
RelaxDB::MigrationVersion.version.should == 1 | ||
end | ||
|
||
it "should not run those migrations whose version is less than the current version" do | ||
v = RelaxDB::MigrationVersion.retrieve | ||
v.version = 2 | ||
v.save! | ||
@mig.run_all "3_", lambda {} | ||
RelaxDB::MigrationVersion.version.should == 3 | ||
end | ||
|
||
it "should run those migrations whose version is greater than the current version" do | ||
v = RelaxDB::MigrationVersion.retrieve | ||
v.version = 2 | ||
v.save! | ||
@mig.run_all "1_foo", lambda {} | ||
RelaxDB::MigrationVersion.version.should == 2 | ||
end | ||
|
||
it "should raise an exception on failure" do | ||
lambda do | ||
@mig.run_all "1_foo", lambda { raise "Expected" } | ||
end.should raise_error(RuntimeError, "Expected") | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require File.join(File.dirname(__FILE__), "spec_helper") | ||
require File.join(File.dirname(__FILE__), "spec_models") | ||
|
||
describe RelaxDB::MigrationVersion do | ||
|
||
before(:each) do | ||
setup_test_db | ||
end | ||
|
||
it "should not exist in a clean db" do | ||
RelaxDB.load(RelaxDB::MigrationVersion::DOC_ID).should be_nil | ||
end | ||
|
||
it "should return zero when it doesnt exist" do | ||
RelaxDB::MigrationVersion.version.should == 0 | ||
end | ||
|
||
it "should autosave on retrieval when when it doesnt exist" do | ||
RelaxDB::MigrationVersion.version | ||
RelaxDB.load(RelaxDB::MigrationVersion::DOC_ID).should be | ||
end | ||
|
||
it "should return the saved version" do | ||
RelaxDB::MigrationVersion.update 10 | ||
RelaxDB::MigrationVersion.version.should == 10 | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters