Class: Ferry::Exporter

Inherits:
Utilities show all
Defined in:
lib/ferry/exporter.rb

Instance Method Summary collapse

Methods inherited from Utilities

#check_valid_db, #check_valid_filetype, #db_connect, #execute, #print_version

Instance Method Details

#to_csv(environment, model) ⇒ Object



6
7
8
9
10
11
12
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
# File 'lib/ferry/exporter.rb', line 6

def to_csv(environment, model)
  db_type = db_connect(environment)
  FileUtils.mkdir "db" unless Dir["db"].present?
  FileUtils.mkdir "db/csv" unless Dir["db/csv"].present?
  homedir = "db/csv/#{environment}"
  FileUtils.mkdir homedir unless Dir[homedir].present?
  table = ActiveRecord::Base.connection.execute("SELECT * FROM #{model};")
  CSV.open("#{homedir}/#{model}.csv", "w") do |csv|
    case db_type
    when 'sqlite3'
      csv_bar = ProgressBar.new("to_csv", table.length)
      keys = table[0].keys.first(table[0].length / 2)
      csv << keys
      table.each do |row|
        csv << row.values_at(*keys)
        csv_bar.inc
      end
    when 'postgresql'
      csv_bar = ProgressBar.new("to_csv", table.num_tuples)
      keys = table[0].keys
      csv << keys
      table.each do |row|
        csv << row.values_at(*keys)
        csv_bar.inc
      end
    when 'mysql2'
      csv_bar = ProgressBar.new("to_csv", table.count)
      db_config = YAML::load(IO.read("config/database.yml"))
      columns = ActiveRecord::Base.connection.execute("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`= '#{db_config[environment]['database']}' AND `TABLE_NAME`='#{model}';")
      col_names=[]
      columns.each do |col|
        col_names.append(col[0])
      end
      csv << col_names
      table.each do |row|
        csv << row
        csv_bar.inc
      end
    else
      raise "#{db_type} is not supported by ferry at this time"
      return false
    end
  end
  puts ""
  puts "exported to db/csv/#{environment}"
end

#to_json(environment, model) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ferry/exporter.rb', line 118

def to_json(environment, model)
  db_type = db_connect(environment)
  FileUtils.mkdir "db" unless Dir["db"].present?
  FileUtils.mkdir "db/json" unless Dir["db/json"].present?
  homedir = "db/json/#{environment}"
  FileUtils.mkdir homedir unless Dir[homedir].present?
  table = ActiveRecord::Base.connection.execute("SELECT * FROM #{model};")
  db_config = YAML::load(IO.read("config/database.yml"))
  if db_type == "mysql2"
    keys = []
    columns = ActiveRecord::Base.connection.execute("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`= '#{db_config[environment]['database']}' AND `TABLE_NAME`='#{model}';")
    columns.each do |col|
      keys.append(col[0])
    end
    table_in_json = []
    table.each do |record|
      record_json = {}
      keys.each do |key|
        record_json[key] = record[keys.index(key)]
      end
      table_in_json << record_json
    end
    File.open("#{homedir}/#{model}.json",'a') do |file|
      file.write(table_in_json.to_json)
    end
  else
    File.open("#{homedir}/#{model}.json",'a') do |file|
      file.write(table.to_json)
    end
  end
end

#to_yaml(environment, model) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ferry/exporter.rb', line 53

def to_yaml(environment, model)
  db_type = db_connect(environment)
  FileUtils.mkdir "db" unless Dir["db"].present?
  FileUtils.mkdir "db/yaml" unless Dir["db/yaml"].present?
  homedir = "db/yaml/#{environment}"
  FileUtils.mkdir homedir unless Dir[homedir].present?
  table = ActiveRecord::Base.connection.execute("SELECT * FROM #{model};")
    db_object = {}
    db_output = {}
    case db_type
    when 'sqlite3'
      yaml_bar = ProgressBar.new("to_yaml", table.length)
      keys = table[0].keys.first(table[0].length / 2)
      db_object["columns"] = keys
      model_arr=[]
      table.each do |row|
        model_arr << row.values_at(*keys)
        yaml_bar.inc
      end
      db_object["records"] = model_arr
      db_output[model] = db_object
      File.open("#{homedir}/#{model}.yml",'a') do |file|
        YAML::dump(db_output, file)
      end
    when 'postgresql'
      yaml_bar = ProgressBar.new("to_yaml", table.num_tuples)
      keys = table[0].keys
      db_object["columns"] = keys
      model_arr=[]
      table.each do |row|
        model_arr << row.values_at(*keys)
        yaml_bar.inc
      end
      db_object["records"] = model_arr
      db_output[model] = db_object
      File.open("#{homedir}/#{model}.yml",'a') do |file|
        YAML::dump(db_output, file)
      end
    when 'mysql2'
      yaml_bar = ProgressBar.new("to_yaml", table.count)
      db_config = YAML::load(IO.read("config/database.yml"))
      columns = ActiveRecord::Base.connection.execute("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`= '#{db_config[environment]['database']}' AND `TABLE_NAME`='#{model}';")
      col_names=[]
      columns.each do |col|
        col_names.append(col[0])
      end
      db_object["columns"] = col_names
      model_arr=[]
      table.each do |row|
        model_arr << row
        yaml_bar.inc
      end
      db_object["records"] = model_arr
      db_output[model] = db_object
      File.open("#{homedir}/#{model}.yml",'a') do |file|
        YAML::dump(db_output, file)
      end
    else
      puts "error in db type"
      return false
    end
  puts ""
  puts "exported to db/yaml/#{environment}"
end