Class: SerializationHelper::Dump

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

Direct Known Subclasses

CsvDb::Dump, JsonDb::Dump, YamlDb::Dump

Class Method Summary collapse

Class Method Details

.after_table(io, table) ⇒ Object



229
230
231
# File 'lib/serialization_helper.rb', line 229

def self.after_table(io, table)

end

.before_table(io, table) ⇒ Object



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

def self.before_table(io, table)

end

.dump(io) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/serialization_helper.rb', line 221

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

.dump_table(io, table) ⇒ Object



237
238
239
240
241
242
# File 'lib/serialization_helper.rb', line 237

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/serialization_helper.rb', line 249

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)
  json_columns = SerializationHelper::Utils.json_columns(table)
  quoted_table_name = SerializationHelper::Utils.quote_table(table)

  (0..pages).to_a.each_with_index do |page, index|
    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)
    records = SerializationHelper::Utils.convert_jsons(records, json_columns)

    page_types = []
    page_types << :first if index == 0
    page_types << :last if index == pages

    yield records, page_types
  end
end

.table_column_names(table) ⇒ Object



244
245
246
# File 'lib/serialization_helper.rb', line 244

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

.table_record_count(table) ⇒ Object



271
272
273
# File 'lib/serialization_helper.rb', line 271

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



233
234
235
# File 'lib/serialization_helper.rb', line 233

def self.tables
  ActiveRecord::Base.connection.tables
end