Module: Siringa

Defined in:
lib/siringa/dumps.rb,
lib/siringa/engine.rb,
lib/siringa/version.rb,
lib/siringa/definitions.rb,
lib/siringa/configuration.rb,
lib/generators/siringa/install_generator.rb,
app/controllers/siringa/siringa_controller.rb,
app/controllers/siringa/application_controller.rb

Defined Under Namespace

Modules: Generators Classes: ApplicationController, Configuration, Engine, SiringaController

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configuration ⇒ Object

Returns the value of attribute configuration.



6
7
8
# File 'lib/siringa/configuration.rb', line 6

def configuration
  @configuration
end

Class Method Details

.adapter_config ⇒ Hash

Return ActiveRecord adapter config

Returns:

  • (Hash)


108
109
110
# File 'lib/siringa/dumps.rb', line 108

def self.adapter_config
  ActiveRecord::Base.connection.instance_values["config"]
end

.add_definition(name, &block) ⇒ Object

Add a definition

Parameters:

  • name (Symbol) —

    of the definition

  • code (Proc) —

    to run



23
24
25
26
27
28
29
# File 'lib/siringa/definitions.rb', line 23

def self.add_definition(name, &block)
  if !exists_definition?(name)
    @definitions[name] = Proc.new(&block)
  else
    raise ArgumentError, "Definition #{name.to_s} already exists."
  end
end

.configure {|configuration| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/siringa/configuration.rb', line 13

def self.configure
  yield(configuration)
end

.dump_file_name ⇒ String

Generate dump file name

Returns:

  • (String)


8
9
10
# File 'lib/siringa/dumps.rb', line 8

def self.dump_file_name
  "#{Siringa.configuration.dumps_path}/db_#{Time.now.strftime('%Y%m%d%H%M%S')}.dump"
end

.dump_to(dump_path) ⇒ Object

Create a DB dump

Parameters:

  • dump (String) —

    path

Returns:

  • (Object)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/siringa/dumps.rb', line 16

def self.dump_to(dump_path)
  result = {}
  case adapter_config[:adapter]
  when "mysql", "mysql2"
    _, result[:error], result[:status] = Open3.capture3(mysql_dump_command(adapter_config[:database], dump_path))
  when "sqlite3"
    _, result[:error], result[:status] = Open3.capture3(sqlite_dump_command(adapter_config[:database], dump_path))
  else
    raise NotImplementedError, "Unknown adapter type '#{adapter_config[:adapter]}'"
  end

  { :status => result[:status].success?, :error => result[:error], :dump_path => dump_path }
end

.exists_definition?(name) ⇒ Boolean

Check if a definition already exists

Parameters:

  • name (Symbol) —

    of the definition

Returns:

  • (Boolean)


35
36
37
# File 'lib/siringa/definitions.rb', line 35

def self.exists_definition?(name)
  !@definitions[name].nil?
end

.keep_five_dumps(dump_files) ⇒ Boolean

Delete oldest dump files, keep 5 dump files

Parameters:

  • file (Array) —

    names

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/siringa/dumps.rb', line 52

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

.load_definition(name) ⇒ Object

Load a definition and run its code

Parameters:

  • name (Symbol) —

    of the definition



11
12
13
14
15
16
17
# File 'lib/siringa/definitions.rb', line 11

def self.load_definition(name)
  if exists_definition?(name)
    @definitions[name].call
  else
    raise ArgumentError, "Definition #{name.to_s} does not exist.", caller
  end
end

.load_definitions ⇒ Object

Load definitions from files



4
5
6
# File 'lib/siringa/definitions.rb', line 4

def self.load_definitions
  Dir[Rails.root.join("#{self.configuration.definitions_path}/**/*.rb")].each { |f| require f }
end

.mysql_dump_command(database, dump_path) ⇒ String

Return a string with the dump command for MySql

Parameters:

  • database (String)
  • dump_path (String)

Returns:

  • (String)


75
76
77
# File 'lib/siringa/dumps.rb', line 75

def self.mysql_dump_command(database, dump_path)
  "/usr/bin/env mysqldump -uroot #{database} > #{dump_path}"
end

.mysql_restore_command(database, dump_path) ⇒ String

Return a string with the restore command for MySql

Parameters:

  • database (String)
  • dump_path (String)

Returns:

  • (String)


84
85
86
# File 'lib/siringa/dumps.rb', line 84

def self.mysql_restore_command(database, dump_path)
  "/usr/bin/env mysql -uroot #{database} < #{dump_path}"
end

.ordered_dumps ⇒ Array

Retrieve a collection of dump files sorted by creation date

Returns:

  • (Array)


64
65
66
# File 'lib/siringa/dumps.rb', line 64

def self.ordered_dumps
  Dir.glob("#{Siringa.configuration.dumps_path}/db_*.dump").sort_by { |f| File.mtime(f) }
end

.restore_from(dump_path) ⇒ Object

Restore from a DB dump

Parameters:

  • dump (String) —

    path

Returns:

  • (Object)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/siringa/dumps.rb', line 34

def self.restore_from(dump_path)
  result = {}
  case adapter_config[:adapter]
  when "mysql", "mysql2"
    _, result[:error], result[:status] = Open3.capture3(mysql_restore_command(adapter_config[:database], dump_path))
  when "sqlite3"
    _, result[:error], result[:status] = Open3.capture3(sqlite_restore_command(adapter_config[:database], dump_path))
  else
    raise NotImplementedError, "Unknown adapter type '#{adapter_config[:adapter]}'"
  end

  { :status => result[:status].success?, :error => result[:error], :dump_path => dump_path }
end

.sqlite_dump_command(database, dump_path) ⇒ String

Return a string with the dump command for Sqlite

Parameters:

  • database (String)
  • dump_path (String)

Returns:

  • (String)


93
94
95
# File 'lib/siringa/dumps.rb', line 93

def self.sqlite_dump_command(database, dump_path)
  "/usr/bin/env sqlite3 #{database} '.backup #{dump_path}'"
end

.sqlite_restore_command(database, dump_path) ⇒ String

Return a string with the restore command for Sqlite

Parameters:

  • database (String)
  • dump_path (String)

Returns:

  • (String)


102
103
104
# File 'lib/siringa/dumps.rb', line 102

def self.sqlite_restore_command(database, dump_path)
  "/usr/bin/env sqlite3 #{database} '.restore #{dump_path}'"
end