Class: SerializationHelper::Dump

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

Direct Known Subclasses

CsvDb::Dump, YamlDb::Dump

Class Method Summary collapse

Class Method Details

.after_table(io, table) ⇒ Object



170
171
172
# File 'lib/serialization_helper.rb', line 170

def self.after_table(io, table)

end

.base_tablesObject



174
175
176
177
178
179
180
181
# File 'lib/serialization_helper.rb', line 174

def self.base_tables

  sql = "select table_name from information_schema.tables where table_schema='#{database}' and table_type='base table';"
  base_tables = ActiveRecord::Base.connection.execute(sql, 'SCHEMA').collect do |field|
    field.first
  end
  base_tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
end

.before_table(io, table) ⇒ Object



158
159
160
# File 'lib/serialization_helper.rb', line 158

def self.before_table(io, table)

end

.databaseObject



183
184
185
# File 'lib/serialization_helper.rb', line 183

def self.database
  Rails.configuration.database_configuration[Rails.env]["database"]
end

.dump(io) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/serialization_helper.rb', line 162

def self.dump(io)
  base_tables.each do |table|
    before_table(io, table)
    dump_table(io, table)
    after_table(io, table)
  end
end

.dump_table(io, table) ⇒ Object



191
192
193
194
195
196
# File 'lib/serialization_helper.rb', line 191

def self.dump_table(io, table)
  return if table_record_count(table).zero?

  dump_table_columns(io, table)
  dump_table_records(io, table)
end

.each_table_page(table, records_per_page = 1000) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/serialization_helper.rb', line 203

def self.each_table_page(table, records_per_page=1000)
  total_count = table_record_count(table)
  pages = (total_count.to_f / records_per_page).ceil - 1
  id = table_column_names(table).first
  boolean_columns = SerializationHelper::Utils.boolean_columns(table)
  quoted_table_name = SerializationHelper::Utils.quote_table(table)

  (0..pages).to_a.each do |page|
    query = Arel::Table.new(table).order(id).skip(records_per_page*page).take(records_per_page).project(Arel.sql('*'))
    records = ActiveRecord::Base.connection.select_all(query)
    records = SerializationHelper::Utils.convert_booleans(records, boolean_columns)
    yield records
  end
end

.table_column_names(table) ⇒ Object



198
199
200
# File 'lib/serialization_helper.rb', line 198

def self.table_column_names(table)
  ActiveRecord::Base.connection.columns(table).map { |c| c.name }
end

.table_record_count(table) ⇒ Object



218
219
220
# File 'lib/serialization_helper.rb', line 218

def self.table_record_count(table)
  ActiveRecord::Base.connection.select_one("SELECT COUNT(*) FROM #{SerializationHelper::Utils.quote_table(table)}").values.first.to_i
end

.tablesObject



187
188
189
# File 'lib/serialization_helper.rb', line 187

def self.tables
  ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
end