class DbStager::HammerOfTheGods

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/db_stager/cli.rb, line 17
def self.exit_on_failure?
  true
end
source_root() click to toggle source
# File lib/db_stager/cli.rb, line 13
def self.source_root
  File.join(File.dirname(__FILE__), '..', '..', 'templates')
end

Public Instance Methods

capture(dir=".") click to toggle source
# File lib/db_stager/cli.rb, line 23
def capture(dir=".")
  Dir.chdir(dir)

  run 'rake db:structure:dump'

  capture_file = File.join(Dir.pwd, "db", "structure.sql")
  File.open(capture_file, "a") do |file|
    db.tables.each do |table_name|
      next if excluded_tables.include?(table_name)
      items = db[table_name]
      items.each do |item|
        if fields[table_name]
          fields[table_name].each do |field_name, task|
            if task
              existing_value = item[field_name.to_sym]
              item[field_name.to_sym] = eval(task)
            else
              item[field_name.to_sym] = nil
            end
          end
          item
        end
        file.puts "#{items.insert_sql(item)};"
        file.puts
      end
    end
  end
end
load(dir=".") click to toggle source
# File lib/db_stager/cli.rb, line 60
def load(dir=".")
  Dir.chdir(dir)
  run 'rake db:structure:load'
end

Protected Instance Methods

config_file() click to toggle source
# File lib/db_stager/cli.rb, line 74
def config_file
  return @config_file if @config_file
  config_file_path = File.join(Dir.pwd, 'config', 'stage_hand.yml')
  if File.exists?(config_file_path)
    @config_file = Hashie::Mash.load(config_file_path)
  else
    @config_file = {}
  end
end
db() click to toggle source
# File lib/db_stager/cli.rb, line 70
def db
  @db ||= Sequel.connect(YAML.load(ERB.new(File.read(File.join(Dir.pwd, 'config', 'database.yml'))).result)[environment])
end
environment() click to toggle source
# File lib/db_stager/cli.rb, line 66
def environment
  @environment ||= options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
end
excluded_tables() click to toggle source
# File lib/db_stager/cli.rb, line 84
def excluded_tables
  return @excluded_tables if @excluded_tables
  @excluded_tables = config_file[:excluded_tables] || []
  @excluded_tables.collect! { |t| t.to_sym }
  # always skip schema_migrations, they get set by the structure file
  @excluded_tables = @excluded_tables | [:schema_migrations]
  return @excluded_tables
end
fields() click to toggle source
# File lib/db_stager/cli.rb, line 93
def fields
  return @fields if @fields
  # prototype is people: [{name: "Faker::Name.unique.name"}, {email: false}]
  # key is table name
  # fake can be any executable faker string
  # if fake is not set, no edit is made
  # skip defaults to false if not set
  # you should set either fake or skip, not both... duh
  @fields = config_file[:fields] || {}
  return @fields
end