Class: Seed::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/seed/snapshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Snapshot

Returns a new instance of Snapshot.



3
4
5
# File 'lib/seed/snapshot.rb', line 3

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#cleanObject



38
39
40
41
42
43
44
45
46
# File 'lib/seed/snapshot.rb', line 38

def clean
  unless exist_path?
    puts "Dump file does not exist for current schema."
    return
  end

  version_path = @configuration.current_version_path
  File.delete(version_path)
end

#dump(classes = [], ignore_classes = [], force_dump = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/seed/snapshot.rb', line 7

def dump(classes = [], ignore_classes = [], force_dump = false)
  @configuration.make_tmp_dir

  if exist_path? && !force_dump
    puts 'Dump file already exists for current schema.'
    return
  end

  Mysql.dump(
    @configuration.current_version_path,
    options.merge({
      tables: tables(classes),
      ignore_tables: ignore_tables(ignore_classes)
    })
  )
end

#exist_path?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/seed/snapshot.rb', line 48

def exist_path?
  version_path = @configuration.current_version_path
  File.exist?(version_path)
end

#ignore_tables(classes) ⇒ Object



70
71
72
73
# File 'lib/seed/snapshot.rb', line 70

def ignore_tables(classes)
  db = @configuration.database_options[:database]
  tables(classes).push("#{db}.schema_migrations")
end

#optionsObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/seed/snapshot.rb', line 53

def options
  db = @configuration.database_options
  {
    username: db[:username],
    password: db[:password],
    host:     db[:host],
    port:     db[:port],
    database: db[:database]
  }
end

#restoreObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/seed/snapshot.rb', line 24

def restore
  @configuration.make_tmp_dir

  unless exist_path?
    puts "Dump file does not exist for current schema."
    return
  end

  Mysql.restore(
    @configuration.current_version_path,
    options
  )
end

#tables(classes) ⇒ Object



64
65
66
67
68
# File 'lib/seed/snapshot.rb', line 64

def tables(classes)
  classes.map do |cls|
    cls.table_name
  end
end