Class: SerializationHelper::Dump

Inherits:
Object
  • Object
show all
Defined in:
lib/fidius-common/yamldb/serialization_helper.rb

Direct Known Subclasses

YamlDb::Dump

Class Method Summary collapse

Class Method Details

.after_table(io, table) ⇒ Object



212
213
214
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 212

def self.after_table(io, table)

end

.before_table(io, table) ⇒ Object



200
201
202
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 200

def self.before_table(io, table)

end

.dump(io) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 204

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



220
221
222
223
224
225
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 220

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



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 232

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|
    sql = ActiveRecord::Base.connection.add_limit_offset!("SELECT * FROM #{quoted_table_name} ORDER BY #{id}",
                                                          :limit => records_per_page, :offset => records_per_page * page
                                                          )
    records = ActiveRecord::Base.connection.select_all(sql)
    records = SerializationHelper::Utils.convert_booleans(records, boolean_columns)
    yield records
  end
end

.table_column_names(table) ⇒ Object



227
228
229
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 227

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

.table_record_count(table) ⇒ Object



249
250
251
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 249

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



216
217
218
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 216

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