class DataSet

A collection of data that can be inserted together into a database

Constants

DataPiece

Attributes

config[RW]
directory[RW]
downs[RW]
ups[RW]

Public Class Methods

new(directory, user_config = {}) click to toggle source

Creates a dataset using the SQL and the configuration contained in directory. The supplied user configuration overrides the configuration contained in the directory

# File lib/sqlloader.rb, line 27
def initialize(directory, user_config = {})
  @ups = []
  @downs = []
  @directory = directory
  populate() 
  @config = load_config(directory).merge(user_config)
  if insufficient(config)
    raise ArgumentError, 'No database name'
  end
end

Public Instance Methods

delete() click to toggle source

Executes the SQL scripts marked as reset scripts

# File lib/sqlloader.rb, line 62
def delete
  get_db_connection(@config) do |db_connection|
  @downs.each do |s|
      result = db_connection.exec(s.statement)
      puts "#{s.filename}: #{result.cmd_status}"
    end
  end
end
load() click to toggle source

Executes the SQL scripts associated with the dataset.

# File lib/sqlloader.rb, line 51
def load
  get_db_connection(@config) do |db_connection|
    @ups.each do |s|
      result = db_connection.exec(s.statement)
      puts "#{s.filename}: #{result.cmd_status}"
    end
  end
end
reset() click to toggle source

If there are reset scripts available, executes them before executing once again the regular scripts

# File lib/sqlloader.rb, line 41
def reset
  if @downs.empty?
     raise ArgumentError, 'There are no reset scripts to run'
   end
  delete
  load
 end
to_s() click to toggle source
# File lib/sqlloader.rb, line 71
def to_s
  File.basename(@directory)
end

Private Instance Methods

insufficient(config) click to toggle source

Returns true if the configuration does not contain enough information to connect to a database

# File lib/sqlloader.rb, line 101
def insufficient(config)
  return config[:dbname].nil? 
end
is_downs(path) click to toggle source
# File lib/sqlloader.rb, line 109
def is_downs(path)
  return File.basename(path) == 'reset.sql'
end
is_ups(path) click to toggle source
# File lib/sqlloader.rb, line 105
def is_ups(path)
  return File.extname(path) == '.sql' && File.basename(path) != 'reset.sql'
end
populate() click to toggle source

Fills the list of statements to execute from the information found by inspecting the directory contents

# File lib/sqlloader.rb, line 81
def populate()
  require 'find'
  Find.find(@directory) do |path|
    if File.file?(path) && File.extname(path) == '.sql'
      data = DataPiece.new(path, File.read(path))
      if is_downs path
        @downs << data
      elsif is_ups path
        @ups << data
      end
    end
  end
  
  @ups.sort! do |x, y|
    File.basename(x.filename) <=> File.basename(y.filename)
  end
end