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.7"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.adapter_config ⇒ Hash
Return ActiveRecord adapter config.
-
.add_definition(name, &block) ⇒ Object
Add a definition.
- .configure {|configuration| ... } ⇒ Object
-
.dump_file_name ⇒ String
Generate dump file name.
-
.dump_to(dump_path) ⇒ Object
Create a DB dump.
-
.exists_definition?(name) ⇒ Boolean
Check if a definition already exists.
-
.keep_five_dumps(dump_files) ⇒ Boolean
Delete oldest dump files, keep 5 dump files.
-
.load_definition(name, options) ⇒ Object
Load a definition and run its code.
-
.load_definitions ⇒ Object
Load definitions from files.
-
.mysql_dump_command(host, database, username, password, dump_path) ⇒ String
Return a string with the dump command for MySql.
-
.mysql_restore_command(host, database, username, password, dump_path) ⇒ String
Return a string with the restore command for MySql.
-
.ordered_dumps ⇒ Array
Retrieve a collection of dump files sorted by creation date.
-
.password_param(password) ⇒ String
Return a string with the password parameter.
-
.restore_from(dump_path) ⇒ Object
Restore from a DB dump.
-
.sqlite_dump_command(database, dump_path) ⇒ String
Return a string with the dump command for Sqlite.
-
.sqlite_restore_command(database, dump_path) ⇒ String
Return a string with the restore command for Sqlite.
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
131 132 133 |
# File 'lib/siringa/dumps.rb', line 131 def self.adapter_config ActiveRecord::Base.connection.instance_values["config"] end |
.add_definition(name, &block) ⇒ Object
Add a definition
24 25 26 27 28 29 30 |
# File 'lib/siringa/definitions.rb', line 24 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
13 14 15 |
# File 'lib/siringa/configuration.rb', line 13 def self.configure yield(configuration) end |
.dump_file_name ⇒ String
Generate dump file name
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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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[:host], adapter_config[:database], adapter_config[:username], adapter_config[:password], 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
36 37 38 |
# File 'lib/siringa/definitions.rb', line 36 def self.exists_definition?(name) !@definitions[name].nil? end |
.keep_five_dumps(dump_files) ⇒ Boolean
Delete oldest dump files, keep 5 dump files
60 61 62 63 64 65 66 67 |
# File 'lib/siringa/dumps.rb', line 60 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, options) ⇒ Object
Load a definition and run its code
12 13 14 15 16 17 18 |
# File 'lib/siringa/definitions.rb', line 12 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(host, database, username, password, dump_path) ⇒ String
Return a string with the dump command for MySql
86 87 88 |
# File 'lib/siringa/dumps.rb', line 86 def self.mysql_dump_command(host, database, username, password, dump_path) "/usr/bin/env mysqldump -h#{host} -u#{username} #{password_param(password)} #{database} > #{dump_path}" end |
.mysql_restore_command(host, database, username, password, dump_path) ⇒ String
Return a string with the restore command for MySql
98 99 100 |
# File 'lib/siringa/dumps.rb', line 98 def self.mysql_restore_command(host, database, username, password, dump_path) "/usr/bin/env mysql -h#{host} -u#{username} #{password_param(password)} #{database} < #{dump_path}" end |
.ordered_dumps ⇒ Array
Retrieve a collection of dump files sorted by creation date
72 73 74 |
# File 'lib/siringa/dumps.rb', line 72 def self.ordered_dumps Dir.glob("#{Siringa.configuration.dumps_path}/db_*.dump").sort_by { |f| File.mtime(f) } end |
.password_param(password) ⇒ String
Return a string with the password parameter
106 107 108 |
# File 'lib/siringa/dumps.rb', line 106 def self.password_param(password) !password.nil? ? "-p#{password}" : "" end |
.restore_from(dump_path) ⇒ Object
Restore from a DB dump
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/siringa/dumps.rb', line 38 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[:host], adapter_config[:database], adapter_config[:username], adapter_config[:password], 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
115 116 117 |
# File 'lib/siringa/dumps.rb', line 115 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
124 125 126 |
# File 'lib/siringa/dumps.rb', line 124 def self.sqlite_restore_command(database, dump_path) "/usr/bin/env sqlite3 #{database} '.restore #{dump_path}'" end |