Class: Caster::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/caster/migrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ Migrator

Returns a new instance of Migrator.



10
11
12
13
14
# File 'lib/caster/migrator.rb', line 10

def initialize 
  @dbs = {}
  @metadata = 
  Caster.log.info { "using #{.desc} for storing metadata documents." }
end

Instance Method Details

#migrate_file(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/caster/migrator.rb', line 69

def migrate_file path
  Caster.log.info { "#{'<' * 5} executing script #{File.basename(path)}" }

  filename = File.basename path, '.cast'
  version, database = filename.split '.'
  current_version = @metadata.get_db_version database

  if current_version != nil and version <= current_version
    raise 'Cannot migrate down!'
  else
    migrate_script database, File.open(path).read
    @metadata.save_db_version database, version
  end

end

#migrate_in_dir(path, migrate_database = nil, max_version = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/caster/migrator.rb', line 16

def migrate_in_dir path, migrate_database = nil, max_version = nil

  all_migrations = {}

  path = path.sub /(\/)+$/, ''
  cast_files = Dir["#{path}/*.cast"]
  Caster.log.info { "found #{cast_files.length} migration scripts under #{path}:\n#{cast_files.to_yaml}" }

  cast_files.map do |file|
    migration_version, database = File.basename(file, '.cast').split '.'

    @dbs[database] = CouchRest.database! "http://#{Caster.config['host']}:#{Caster.config['port']}/#{database}" if @dbs[database] == nil

    all_migrations[database] = [] if all_migrations[database] == nil

    all_migrations[database] << {
        :current_version => @metadata.get_db_version(database),
        :version => migration_version,
        :filepath => file
    }
  end

  all_migrations.each_pair do |database, migrations|
    Caster.log.info { "#{'<' * 10} starting migrations for database #{database}" }
    Caster.log.info { "current database version: #{migrations.first[:current_version] || 'none' }" }

    db_filtered = (migrate_database == nil)? migrations.map : migrations.map do |migration|
      migration if migrate_database == database
    end.compact
    Caster.log.info { "no migration scripts found for database #{migrate_database}" } if migrate_database != nil and db_filtered.length == 0

    min_version_filtered = db_filtered.map do |migration|
      migration if migration[:current_version] == nil or migration[:version] > migration[:current_version]
    end.compact

    max_version_filtered = min_version_filtered.map do |migration|
      migration if max_version == nil or migration[:version] <= max_version
    end.compact

    filtered_and_sorted_files = max_version_filtered.map do |migration|
      migration[:filepath]
    end.sort
    Caster.log.info {( (filtered_and_sorted_files.length > 0)? "using migration scripts:\n#{ filtered_and_sorted_files.map{ |f| File.basename(f) }.to_yaml }" : "no applicable scripts found for database #{database}" )}

    filtered_and_sorted_files.each do |file|
      migrate_file file
    end

    Caster.log.info { (max_version_filtered.length > 0)? "#{'>' * 10} finished migrating database #{database} to version #{max_version_filtered.last[:version]}\n" : "did not run any migration on database #{database}\n" }
  end

end