Sha256: 1740c21ee2bebc88605db9c75c53bed9a6cce9045d5ab78624d09dbd8f735ed1

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 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 from database `#{@connection.current_database}`..."
      @table_samples.values.map &:sample!
      warn "Ensuring referential integrity..."
      begin
        new_dependencies = 0
        @table_samples.values.each do |table_sample|
          if table_sample.ensure_referential_integrity(@table_samples)
            new_dependencies += 1 
            warn "  Found new dependents for table `#{table_sample.table_name}`"
          end
        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

2 entries across 2 versions & 1 rubygems

Version Path
data_sampler-0.0.5 lib/data_sampler/sample.rb
data_sampler-0.0.4 lib/data_sampler/sample.rb