Class: Arxutils::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/arxutils/migrate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrate_base_dir, config_dir, dbconfig, log_fname, forced = false) ⇒ Migrate

Returns a new instance of Migrate.



54
55
56
57
58
59
60
61
62
63
# File 'lib/arxutils/migrate.rb', line 54

def initialize( migrate_base_dir , config_dir , dbconfig, log_fname, forced = false )
  @dbinit = Dbutil::Dbinit.new( migrate_base_dir , config_dir , dbconfig, log_fname, forced )
  @dbconfig_dest_path = @dbinit.dbconfig_dest_path
  @dbconfig_src_path = @dbinit.dbconfig_src_path
  @dbconfig_src_fname = @dbinit.dbconfig_src_fname

  @migrate_dir = @dbinit.migrate_dir
  @src_path = Arxutils.templatedir
  @src_config_path = Arxutils.configdir
end

Instance Attribute Details

#dbconfig_dest_fnameObject

Returns the value of attribute dbconfig_dest_fname.



11
12
13
# File 'lib/arxutils/migrate.rb', line 11

def dbconfig_dest_fname
  @dbconfig_dest_fname
end

#dbconfig_dest_pathObject

Returns the value of attribute dbconfig_dest_path.



11
12
13
# File 'lib/arxutils/migrate.rb', line 11

def dbconfig_dest_path
  @dbconfig_dest_path
end

#dbconfig_src_fnameObject

Returns the value of attribute dbconfig_src_fname.



11
12
13
# File 'lib/arxutils/migrate.rb', line 11

def dbconfig_src_fname
  @dbconfig_src_fname
end

#dbconfig_src_pathObject

Returns the value of attribute dbconfig_src_path.



11
12
13
# File 'lib/arxutils/migrate.rb', line 11

def dbconfig_src_path
  @dbconfig_src_path
end

#dbinitObject

Returns the value of attribute dbinit.



11
12
13
# File 'lib/arxutils/migrate.rb', line 11

def dbinit
  @dbinit
end

Class Method Details

.migrate(data_ary, relation_def_fpath, module_name, count_classname_downcase, count_field, dbconfig, forced) ⇒ Object



13
14
15
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
# File 'lib/arxutils/migrate.rb', line 13

def Migrate.migrate( data_ary , relation_def_fpath , module_name, count_classname_downcase , count_field , dbconfig , forced )
  src_config_dir = Arxutils.configdir
  mig = Migrate.new( Dbutil::MIGRATE_DIR , src_config_dir , dbconfig, Dbutil::DATABASELOG, forced )
  # dbconfigのテンプレートは内容が固定である。convertを呼び出し、Arxのインスタンスを作成するときに、適切なdata_aryの要素を与える必要がある(ただしテンプレートへの埋め込みには用いられない
  mig.make_dbconfig( dbconfig )
  
  data_ary.reduce(0) { |next_num , x| 
    mig.make( next_num , x )
  }

  content_array = data_ary.map { |x| 
    mig.make_relation( x , "count", "end_count_id" )
  }.select{ |x| x.size > 0 }
  need_count_class_plural = content_array.reduce([]){ |s,x|
    s << x[:need_count_class_plural] if x[:need_count_class_plural] != nil
    s
  }
  if content_array.size > 0
    data_count = {count_classname: "Count" ,
                  count_field: count_field,
                  need_count_class_plural: need_count_class_plural,
                 }
    ary = content_array.collect{|x| x[:content] }.flatten
    count_content = mig.convert_count_class_relation( data_count , "relation_count.tmpl" )
    ary.unshift( count_content )
    content_array = ary
  end
  File.open( relation_def_fpath , 'w' , {:encoding => Encoding::UTF_8}){ |f|
    f.puts("module #{module_name}")
    content_array.map{ |content|
      f.puts( content )
      f.puts( "\n" )
    }
    f.puts("end")
  }
  
  Dbutil::DbMgr.setup( mig.dbinit )

  mig.migrate
end

Instance Method Details

#convert(data, src_dir, src_fname) ⇒ Object



69
70
71
72
# File 'lib/arxutils/migrate.rb', line 69

def convert( data , src_dir , src_fname )
  arx = Arx.new( data , File.join( src_dir, src_fname ) )
  arx.create
end

#convert_count_class_relation(data, src_fname) ⇒ Object



65
66
67
# File 'lib/arxutils/migrate.rb', line 65

def convert_count_class_relation( data , src_fname )
  convert( data , @src_path , src_fname )
end

#make(next_num, data) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/arxutils/migrate.rb', line 103

def make( next_num , data )
  data[:flist].reduce(next_num) do |idy , x|
    idy += 10
    content = convert( data , @src_path , "#{x}.tmpl" )
    case x
    when "base" , "noitem"
      additional = ""
    else
      additional = x
    end
    fname = File.join( @migrate_dir , sprintf("%03d_create_%s%s.rb" , idy , additional , data[:classname_downcase]) )
    File.open( fname , 'w' , {:encoding => Encoding::UTF_8}){ |f|
      f.puts( content )
    }
    idy
  end
end

#make_dbconfig(data) ⇒ Object



74
75
76
77
78
79
# File 'lib/arxutils/migrate.rb', line 74

def make_dbconfig( data )
  content = convert( data , @src_config_path , @dbconfig_src_fname )
  File.open( @dbconfig_dest_path , 'w' , {:encoding => Encoding::UTF_8}){ |f|
    f.puts( content )
  }
end

#make_relation(data, count_classname_downcase, count_field) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/arxutils/migrate.rb', line 81

def make_relation( data , count_classname_downcase , count_field )
  if data[:classname_downcase] != count_classname_downcase
    data[:flist].reduce( { content: [], need_count_class: nil } ){ |s, x|
      case x
      when "base" , "noitem"
        name_base = "relation"
        data[:relation] = [] unless data[:relation]
      else
        data[:count_classname_downcase] = count_classname_downcase
        data[:count_field] = count_field
        name_base = "relation_#{x}"
        s[:need_count_class_plural] ||= data[:plural]
      end
      content = convert( data , @src_path , "#{name_base}.tmpl" )
      s[:content] << content
      s
    }
  else
    {}
  end
end

#migrateObject



121
122
123
# File 'lib/arxutils/migrate.rb', line 121

def migrate
  ActiveRecord::Migrator.migrate(@migrate_dir ,  ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
end