Class: RelaxDB::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/migration.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) fv(file_name)



37
38
39
# File 'lib/relaxdb/migration.rb', line 37

def self.fv file_name
  File.basename(file_name).split("_")[0].to_i
end

+ (Object) load_batch(klass, startkey, limit)



5
6
7
8
9
10
11
# File 'lib/relaxdb/migration.rb', line 5

def self.load_batch klass, startkey, limit
  skip = startkey.nil? ? 0 : 1
  result = RelaxDB.rf_view "#{klass}_all", :startkey => startkey, 
    :raw => true, :limit => limit, :skip => skip
  ids = result["rows"].map { |h| h["id"] }
  RelaxDB.load! ids      
end

+ (Object) run(klass, limit = 1000)



13
14
15
16
17
18
19
20
# File 'lib/relaxdb/migration.rb', line 13

def self.run klass, limit = 1000
  objs = load_batch klass, nil, limit
  until objs.empty?
    migrated = objs.map { |o| yield o }.flatten.compact
    RelaxDB.bulk_save! *migrated
    objs = load_batch klass, objs[-1]._id, limit
  end
end

+ (Object) run_all(file_names, action = lambda { |fn| require fn })

Runs all outstanding migrations in a given directory

Example

RelaxDB::Migration.run_all Dir["couchdb/migrations/**/*.rb"]


28
29
30
31
32
33
34
35
# File 'lib/relaxdb/migration.rb', line 28

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