Class: GitFriendlyDumper

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/git_friendly_dumper.rb,
lib/git_friendly_dumper/railtie.rb,
lib/git_friendly_dumper/version.rb

Overview

Database independent and git friendly replacement for mysqldump for rails projects

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitFriendlyDumper

Returns a new instance of GitFriendlyDumper.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_friendly_dumper.rb', line 29

def initialize(options = {})
  options.assert_valid_keys(:root, :path, :connection, :connection_name, :tables, :force, :include_schema, :show_progress, :clobber_fixtures, :limit, :raise_error, :fixtures, :fixtures_file)

  self.root = options[:root] || (defined?(Rails) && Rails.root) || pwd
  
  if options[:fixtures_file]
    raise ArgumentError, "GitFriendlyDumper cannot specify both :fixtures and :fixtures_file" if options[:fixtures].present?
    options[:fixtures] = File.read(options[:fixtures_file]).split("\n").map(&:squish).reject(&:blank?)
  end
  
  if options[:fixtures] && (options[:include_schema] || options[:clobber_fixtures])
    raise ArgumentError, "GitFriendlyDumper if :fixtures option given, neither :include_schema nor :clobber_fixtures can be given"
  end
  
  if options[:show_progress] && !defined?(ProgressBar)
    raise RuntimeError, "GitFriendlyDumper requires the progressbar gem for progress option.\n  sudo gem install progressbar"
  end
  
  self.path             = File.expand_path(options[:path] || 'db/dump')
  self.tables           = options[:tables]
  self.fixtures         = options[:fixtures]
  self.limit            = options.key?(:limit) ? options[:limit].to_i : 2500
  self.raise_error      = options.key?(:raise_error) ? options[:raise_error] : true
  self.force            = options.key?(:force) ? options[:force] : false
  self.include_schema   = options.key?(:include_schema) ? options[:include_schema] : false
  self.show_progress    = options.key?(:show_progress) ? options[:show_progress] : false
  self.clobber_fixtures = options.key?(:clobber_fixtures) ? options[:clobber_fixtures] : (options[:tables].blank? ? true : false)
  self.connection       = options[:connection] || begin
    if options[:connection_name]
      ActiveRecord::Base.establish_connection(options[:connection_name])
    end
    ActiveRecord::Base.connection
  end
end

Instance Attribute Details

#clobber_fixturesObject Also known as: clobber_fixtures?

Returns the value of attribute clobber_fixtures.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def clobber_fixtures
  @clobber_fixtures
end

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def connection
  @connection
end

#fixturesObject

Returns the value of attribute fixtures.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def fixtures
  @fixtures
end

#forceObject Also known as: force?

Returns the value of attribute force.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def force
  @force
end

#include_schemaObject Also known as: include_schema?

Returns the value of attribute include_schema.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def include_schema
  @include_schema
end

#limitObject

Returns the value of attribute limit.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def limit
  @limit
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def path
  @path
end

#raise_errorObject Also known as: raise_error?

Returns the value of attribute raise_error.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def raise_error
  @raise_error
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def root
  @root
end

#show_progressObject Also known as: show_progress?

Returns the value of attribute show_progress.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def show_progress
  @show_progress
end

#tablesObject

Returns the value of attribute tables.



12
13
14
# File 'lib/git_friendly_dumper.rb', line 12

def tables
  @tables
end

Class Method Details

.dump(options = {}) ⇒ Object



20
21
22
# File 'lib/git_friendly_dumper.rb', line 20

def dump(options = {})
  new(options).dump
end

.load(options = {}) ⇒ Object



24
25
26
# File 'lib/git_friendly_dumper.rb', line 24

def load(options = {})
  new(options).load
end

Instance Method Details

#dumpObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/git_friendly_dumper.rb', line 64

def dump
  if fixtures
    raise ArgumentError, "Cannot dump when :fixtures option is given"
  end
  self.tables ||= db_tables
  tables.delete('schema_migrations') unless include_schema?
  if force? || (tables & fixtures_tables).empty? || confirm?(:dump)
    puts "Dumping data#{' and structure' if include_schema?} from #{current_database_name} to #{path.sub("#{root}/",'')}\n"
    clobber_all_fixtures if clobber_fixtures?
    connection.transaction do
      tables.each {|table| dump_table(table) }
    end
  end
end

#loadObject



79
80
81
# File 'lib/git_friendly_dumper.rb', line 79

def load
  fixtures ? load_fixtures : load_tables
end