Class: DbSubsetter::Exporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filter=(value) ⇒ Object

Sets the attribute filter

Parameters:

  • value

    the value to set the attribute filter to.



5
6
7
# File 'lib/db_subsetter/exporter.rb', line 5

def filter=(value)
  @filter = value
end

Instance Method Details

#all_tablesObject



7
8
9
# File 'lib/db_subsetter/exporter.rb', line 7

def all_tables
  ActiveRecord::Base.connection.tables
end

#export(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/db_subsetter/exporter.rb', line 35

def export(filename)
  verify_exportability

  @output = SQLite3::Database.new(filename)
  @output.execute("CREATE TABLE tables (name TEXT, records_exported INTEGER, columns TEXT)")
  tables.each do |table|
    export_table(table)
  end
end

#filtered_row_countsObject



23
24
25
26
27
# File 'lib/db_subsetter/exporter.rb', line 23

def filtered_row_counts
  tables.each.map do |table|
    {table => filtered_row_count(table)}
  end
end

#tablesObject



11
12
13
# File 'lib/db_subsetter/exporter.rb', line 11

def tables
  filter.tables
end

#total_row_countsObject



15
16
17
18
19
20
21
# File 'lib/db_subsetter/exporter.rb', line 15

def total_row_counts
  tables.each.map do |table|
    query = Arel::Table.new(table, ActiveRecord::Base).project("count(1) AS num_rows")
    rows = ActiveRecord::Base.connection.select_one(query.to_sql)["num_rows"]
    {table => rows}
  end
end

#verify_exportabilityObject



29
30
31
32
33
# File 'lib/db_subsetter/exporter.rb', line 29

def verify_exportability
  tables.each do |table|
    verify_table_exportability(table)
  end
end