Class: SerializationHelper::Dump

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

Direct Known Subclasses

CsvDb::Dump, YamlDb::Dump

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.filter_table_namesObject

Returns the value of attribute filter_table_names.



153
154
155
# File 'lib/serialization_helper.rb', line 153

def filter_table_names
  @filter_table_names
end

Class Method Details

.after_table(io, table) ⇒ Object



168
169
170
# File 'lib/serialization_helper.rb', line 168

def self.after_table(io, table)

end

.before_table(io, table) ⇒ Object



156
157
158
# File 'lib/serialization_helper.rb', line 156

def self.before_table(io, table)

end

.dump(io) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/serialization_helper.rb', line 160

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



178
179
180
181
182
183
# File 'lib/serialization_helper.rb', line 178

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/serialization_helper.rb', line 190

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.to_sql)
    records = SerializationHelper::Utils.convert_booleans(records, boolean_columns)
    yield records
  end
end

.table_column_names(table) ⇒ Object



185
186
187
# File 'lib/serialization_helper.rb', line 185

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

.table_record_count(table) ⇒ Object



205
206
207
# File 'lib/serialization_helper.rb', line 205

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



172
173
174
175
176
# File 'lib/serialization_helper.rb', line 172

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

  filter_table_names ? all_tables.grep(Regexp.new(filter_table_names)) : all_tables
end