Class: DbSubsetter::Exporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExporter

Returns a new instance of Exporter.



56
57
58
59
# File 'lib/db_subsetter/exporter.rb', line 56

def initialize
  @scramblers = []
  @page_counts = {}
end

Instance Attribute Details

#filter=(value) ⇒ Object

Sets the attribute filter

Parameters:

  • value

    the value to set the attribute filter to.



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

def filter=(value)
  @filter = value
end

#max_filtered_rows=(value) ⇒ Object

Sets the attribute max_filtered_rows

Parameters:

  • value

    the value to set the attribute max_filtered_rows to.



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

def max_filtered_rows=(value)
  @max_filtered_rows = value
end

#max_unfiltered_rows=(value) ⇒ Object

Sets the attribute max_unfiltered_rows

Parameters:

  • value

    the value to set the attribute max_unfiltered_rows to.



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

def max_unfiltered_rows=(value)
  @max_unfiltered_rows = value
end

Instance Method Details

#add_scrambler(scrambler) ⇒ Object



52
53
54
# File 'lib/db_subsetter/exporter.rb', line 52

def add_scrambler(scrambler)
  @scramblers << scrambler
end

#all_tablesObject



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

def all_tables
  ActiveRecord::Base.connection.tables
end

#export(filename, verbose = true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/db_subsetter/exporter.rb', line 40

def export(filename, verbose = true)
  @verbose = verbose
  verify_exportability(verbose)

  puts "Exporting data...\n\n" if @verbose
  @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



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

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

#tablesObject



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

def tables
  filter.tables
end

#total_row_countsObject



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

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_exportability(verbose = true) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/db_subsetter/exporter.rb', line 30

def verify_exportability(verbose = true)
  puts "Verifying table exportability ...\n\n" if verbose
  errors = tables.map{|x| verify_table_exportability(x) }.flatten.compact
  if errors.count > 0
    puts errors.join("\n")
    raise ArgumentError.new "Some tables are not exportable"
  end
  puts "\n\n" if verbose
end