Sha256: e259f4a4d320ec049b1ea01a45fb6841117f20accda86171f6fc1885b2a7691b

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module Siringa

  # Generate dump file name
  #
  # @return [String]
  def self.dump_file_name
    "#{Siringa.configuration.dumps_path}/db_#{Time.now.strftime('%Y%m%d%H%M%S')}.dump"
  end

  # Create a DB dump
  #
  # @param [String] dump path
  # @return [Object]
  def self.dump_to(dump_path)
    adapter_config = ActiveRecord::Base.connection.instance_values["config"]
    case adapter_config[:adapter]
    when "mysql", "mysql2"
      output = %x(/usr/bin/env mysqldump -uroot #{adapter_config[:database]} > #{dump_path})
    when "sqlite3"
      output = %x(/usr/bin/env sqlite3 #{adapter_config[:database]} '.backup #{dump_path}')
    else
      raise NotImplementedError, "Unknown adapter type '#{adapter_config[:adapter]}'"
    end

    { :success => $?.success?, :output => output, :dump_path => dump_path }
  end

  # Restore from a DB dump
  #
  # @param [String] dump path
  # @return [Object]
  def self.restore_from(dump_path)
    adapter_config = ActiveRecord::Base.connection.instance_values["config"]
    case adapter_config[:adapter]
    when "mysql", "mysql2"
      output = %x(/usr/bin/env mysql -uroot #{adapter_config[:database]} < #{dump_path})
    when "sqlite3"
      output = %x(/usr/bin/env sqlite3 #{adapter_config[:database]} '.restore #{dump_path}')
    else
      raise NotImplementedError, "Unknown adapter type '#{adapter_config[:adapter]}'"
    end

    { :success => $?.success?, :output => output, :dump_path => dump_path }
  end

  # Delete oldest dump files, keep 5 dump files
  #
  # @param [Array] file names
  # @return [Boolean]
  def self.keep_five_dumps(dump_files)
    if dump_files.length > 5
      dump_files.first(dump_files.length - 5).each { |f| File.delete(f) }
      return true
    else
      return false
    end
  end

  # Retrieve a collection of dump files sorted by creation date
  #
  # @return [Array]
  def self.ordered_dumps
    Dir.glob("#{Siringa.configuration.dumps_path}/db_*.dump").sort_by { |f| File.mtime(f) }
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
siringa-0.0.1 lib/siringa/dumps.rb