Class: JIJI::Migration::Migrator

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

Overview

サーバー起動時にデータ移行を行うクラス。

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#migratorsObject

Returns the value of attribute migrators.



43
44
45
# File 'lib/jiji/migration/migrator.rb', line 43

def migrators
  @migrators
end

#registryObject

Returns the value of attribute registry.



46
47
48
# File 'lib/jiji/migration/migrator.rb', line 46

def registry
  @registry
end

#server_loggerObject

Returns the value of attribute server_logger.



44
45
46
# File 'lib/jiji/migration/migrator.rb', line 44

def server_logger
  @server_logger
end

#version_fileObject

Returns the value of attribute version_file.



45
46
47
# File 'lib/jiji/migration/migrator.rb', line 45

def version_file
  @version_file
end

Instance Method Details

#migrateObject

データを移行する。



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jiji/migration/migrator.rb', line 14

def migrate
  logger = server_logger
  begin
    data_version = version
    new_version = data_version
    tmp = nil
    @migrators.sort_by {|item| item[:version] }.each {|d|
      tmp = d[:version]
      if ( data_version < d[:version] )
        d[:migrator].migrate(registry)
        new_version = d[:version]
        logger.info "data migration succesful! new_version=#{new_version.to_s}."
      end
    }
  rescue Exception
    logger.error "data migration failed. current=#{data_version.to_s} new_version=#{tmp.to_s}"
    logger.error $!
  ensure
    FileLock.new( version_file ).writelock {|f| f << new_version.to_s }
  end
end

#versionObject

現在のデータバージョンを取得する。



37
38
39
40
41
# File 'lib/jiji/migration/migrator.rb', line 37

def version
  version_str = File.exist?(version_file) ? 
    FileLock.new( version_file ).readlock {|f| f.read } : "1.0.0"
  return Gem::Version.new( version_str )
end