Sha256: 79529a1a13cba4e1c0ef6baf93d5bf9d932e20e14de5e28d70073acbafa1a212

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

class Storey::Dumper
  def self.dump(options={})
    case Rails.configuration.active_record.schema_format
    when :sql  ; self.dump_structure_sql(options)
    when :ruby ; self.dump_schema_rb(options)
    end
  end

  def self.dump_schema_rb(options={})
    require 'active_record/schema_dumper'
    filename = options[:file] || "#{Rails.root}/db/schema.rb"
    File.open(filename, "w:utf-8") do |file|
      ActiveRecord::Base.establish_connection(Rails.env)
      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
    end
  end

  def self.dump_structure_sql(options={})
    abcs = ActiveRecord::Base.configurations
    filename = options[:file] || File.join(Rails.root, "db", "structure.sql")
    set_psql_env(abcs[Rails.env])
    search_path = abcs[Rails.env]['schema_search_path']
    unless search_path.blank?
      search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
    end
    `pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(abcs[Rails.env]['database'])}`
    raise 'Error dumping database' if $?.exitstatus == 1
    File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
  end

  private

  def self.set_psql_env(config)
    ENV['PGHOST']     = config['host']          if config['host']
    ENV['PGPORT']     = config['port'].to_s     if config['port']
    ENV['PGPASSWORD'] = config['password'].to_s if config['password']
    ENV['PGUSER']     = config['username'].to_s if config['username']
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
storey-0.3.4 lib/storey/dumper.rb
storey-0.3.3 lib/storey/dumper.rb
storey-0.3.1 lib/storey/dumper.rb