Sha256: a2d295cccba9e7f9789dd9758f477fe2304548c1666a79d04fbdbb58aa4c4328

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

require "data_sampler/table_sample"

module DataSampler

  class Sample

    def initialize(connection, rows_per_table = 1000)
      @connection = connection
      @rows_per_table = rows_per_table
      @table_samples = {}
      @computed = false
    end

    def compute!
      @connection.tables.each do |table_name|
        # Workaround for inconsistent casing in table definitions (http://bugs.mysql.com/bug.php?id=60773)
        table_name.downcase!
        @table_samples[table_name] = TableSample.new(@connection, table_name, @rows_per_table)
      end
      warn "Sampling #{@table_samples.count} tables..."
      @table_samples.values.map &:sample!
      warn "Ensuring referential integrity..."
      begin
        new_dependencies = 0
        @table_samples.values.each do |table_sample|
          new_dependencies += 1 if table_sample.ensure_referential_integrity(@table_samples)
        end
        warn " - discovered #{new_dependencies} new dependencies" if new_dependencies > 0
      end while new_dependencies > 0
      warn " - referential integrity obtained"
      @computed = true
    end

    def to_sql
      compute! unless @computed
      @table_samples.values.collect(&:to_sql) * "\n"
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
data_sampler-0.0.3 lib/data_sampler/sample.rb
data_sampler-0.0.2 lib/data_sampler/sample.rb
data_sampler-0.0.1 lib/data_sampler/sample.rb