koine-db_bkp 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46f82e70cbe9936dd67098b62a2d60726b4a3de2
4
- data.tar.gz: 417e3bf56163661f599e0080387698a649144c79
3
+ metadata.gz: 9b2e5356b280e6bfb128566c9e83c4eb7d0780fe
4
+ data.tar.gz: 3c8cbb6454be752f108acf108a55508a2ffa0ab5
5
5
  SHA512:
6
- metadata.gz: ace6bcc0ef66c0c067ec18caf12cb66cf68d5c2a403bd7dc025d2fb9eb4ca94c0be08aed9115ca2775df60724ebf9a91349d68928c8b1cad304243fa01f6bd46
7
- data.tar.gz: e3242f32eafbdb8e5278e6d5fcd2e9640b8dbe91036a021957c5e0c4107434f194e61e3252ba7b94cc7b5e0e2be9b2c887db991698d1ea1106aac633fe9355ff
6
+ metadata.gz: 742a358cbe09f1edc8913aa114195b1f619656804a6d8b3234ba2c00bf81165ef0121c66d014b57215d6695fd05c4f7d6b15e68926f17d8c3e91dd0808f48ac5
7
+ data.tar.gz: 3ffa6e5151ea5fde3ac40d6f34d34d43a6ffdbd46c4ded4d105f2fbdb556b5a4f48dadd3b437397f4dcd3793340d4a4fc05475ab618a250c9e5d6a9bdf7dc994
data/README.md CHANGED
@@ -32,33 +32,45 @@ backup = Koine::DbBkp::Mysql::Dump.new(
32
32
  backup.to_sql_file('/bkp/file.sql')
33
33
  ```
34
34
 
35
- ### MySql on Rails
35
+ ### Rake tasks
36
36
 
37
37
  ```ruby
38
- require 'koine/db_bkp/mysql/rails_dump'
38
+ # Rakefile
39
39
 
40
- # credentials taken from current Rails.configuration.database_configuration[Rails.env]
41
- backup = Koine::DbBkp::Mysql::RailsDump.new
42
- backup.to_sql_file('/bkp/file.sql')
40
+ require "koine/db_bkp"
41
+
42
+ Koine::Tasks::MysqlDump.new do |t|
43
+ t.task_name = 'some_task' # defaults to 'mysql:dump'
44
+ t.output_file = 'backup_{timestamp}.sql' # defaults to ENV['MYSQL_BACKUP_FILE']
45
+ # {timestamp} is a placeholder for the autogenerated timestamp
46
+
47
+ # database config
48
+ t.url = 'mysql2://username:password@hostname/database_name' # defaults to ENV['DB_URL']
49
+
50
+ # or database config
51
+ t.hostname = 'hostname' # defaults to ENV['DB_HOST']
52
+ t.database = 'database_name' # defaults to ENV['DB_NAME']
53
+ t.username = 'username' # defaults to ENV['DB_USER']
54
+ t.password = 'password' # defaults to ENV['DB_PASSWORD']
55
+
56
+ # rake task dependencies
57
+ t.dependencies = [:environment] # defaults to []
58
+ end
43
59
  ```
44
60
 
45
- ### Rake tasks
61
+ #### Rails task
46
62
 
47
63
  ```ruby
64
+ # Rakefile
65
+
48
66
  require "koine/db_bkp"
49
67
 
50
- import 'lib/koine/tasks/mysql_dump.rake'
51
- import 'lib/koine/tasks/mysql_rails_dump.rake'
68
+ # same options of Koine::Tasks::RailsMysqlDump apply
69
+ Koine::Tasks::RailsMysqlDump.new
52
70
  ```
53
71
 
54
72
  ```bash
55
- export DB_HOST=some_host
56
- export DB_NAME=some_name
57
- export DB_USER=some_user
58
- export DB_PASSWORD=some_password
59
- export MYSQL_BACKUP_FILE='/foo/bar_{timestamp}.sql'
60
-
61
- rake koine:mysql:dump
73
+ rake rake mysql:dump
62
74
  ```
63
75
 
64
76
  ```bash
@@ -66,7 +78,7 @@ rake koine:mysql:dump
66
78
 
67
79
  export MYSQL_BACKUP_FILE='/foo/bar_{timestamp}.sql'
68
80
 
69
- rake koine:mysql:rails_dump
81
+ rake mysql:rails_dump
70
82
  ```
71
83
 
72
84
  ## Development
data/Rakefile CHANGED
@@ -3,8 +3,11 @@ require "rspec/core/rake_task"
3
3
 
4
4
  require "koine/db_bkp"
5
5
 
6
- import 'lib/koine/tasks/mysql_dump.rake'
7
- import 'lib/koine/tasks/mysql_rails_dump.rake'
6
+ Koine::Tasks::MysqlDump.new
7
+
8
+ Koine::Tasks::RailsMysqlDump.new do |t|
9
+ t.task_name = 'rails:mysql_dump'
10
+ end
8
11
 
9
12
  RSpec::Core::RakeTask.new(:spec)
10
13
 
@@ -1,12 +1,15 @@
1
+ require 'addressable/uri'
2
+
1
3
  module Koine
2
4
  module DbBkp
3
5
  module Mysql
4
6
  class Dump
5
7
  def initialize(config = {})
6
- config = config.reject { |_k, v| ['', nil].include?(v) }
8
+ config = normalize_config(config)
7
9
 
8
- @hostname = config[:hostname]
10
+ @hostname = config.fetch(:hostname)
9
11
  @database = config.fetch(:database)
12
+ @username = config[:username]
10
13
  @password = config[:password]
11
14
  @cli = Cli.new
12
15
  end
@@ -15,13 +18,45 @@ module Koine
15
18
  parts = ['mysqldump']
16
19
 
17
20
  parts.push("-h #{@hostname}") if @hostname
21
+ parts.push("-u #{@username}") if @username
18
22
  parts.push("-p#{@password}") if @password
19
23
 
20
24
  parts.push(@database)
25
+
26
+ file = FileName.new(file)
21
27
  parts.push("> #{file}")
22
28
 
23
29
  @cli.execute(parts.join(' '))
24
30
  end
31
+
32
+ private
33
+
34
+ def normalize_config(config)
35
+ config = config.reject { |_k, v| ['', nil].include?(v) }
36
+ merge_url(symbolize_keys(config))
37
+ end
38
+
39
+ def merge_url(config)
40
+ url = config.delete(:url)
41
+
42
+ return config unless url
43
+
44
+ url = Addressable::URI.parse(url)
45
+
46
+ config.merge(
47
+ adapter: url.scheme,
48
+ hostname: url.host,
49
+ database: url.path.split('/').join(''),
50
+ username: url.user,
51
+ password: url.password
52
+ )
53
+ end
54
+
55
+ def symbolize_keys(hash)
56
+ {}.tap do |new_hash|
57
+ hash.each { |key, value| new_hash[key.to_sym] = value }
58
+ end
59
+ end
25
60
  end
26
61
  end
27
62
  end
@@ -1,5 +1,5 @@
1
1
  module Koine
2
2
  module DbBkp
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
data/lib/koine/db_bkp.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require 'koine/db_bkp/version'
2
2
 
3
3
  module Koine
4
+ module Tasks
5
+ autoload :MysqlDump, 'koine/tasks/mysql_dump'
6
+ autoload :RailsMysqlDump, 'koine/tasks/rails_mysql_dump'
7
+ end
8
+
4
9
  module DbBkp
5
10
  autoload :Cli, 'koine/db_bkp/cli'
6
11
  autoload :FileName, 'koine/db_bkp/file_name'
@@ -0,0 +1,60 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module Koine
5
+ module Tasks
6
+ class MysqlDump < Rake::TaskLib
7
+ OutputFileNotSetError = Class.new(StandardError)
8
+
9
+ attr_accessor :task_name
10
+ attr_accessor :dependencies
11
+ attr_accessor :hostname
12
+ attr_accessor :database
13
+ attr_accessor :username
14
+ attr_accessor :password
15
+ attr_accessor :url
16
+ attr_writer :output_file
17
+
18
+ def initialize(env: ENV)
19
+ self.task_name ||= 'mysql:dump'
20
+ self.dependencies ||= []
21
+ self.url ||= env['DB_URL']
22
+ self.hostname ||= env['DB_HOST']
23
+ self.database ||= env['DB_NAME']
24
+ self.username ||= env['DB_USER']
25
+ self.password ||= env['DB_PASSWORD']
26
+ self.output_file = env['MYSQL_BACKUP_FILE']
27
+ yield(self) if block_given?
28
+ define_task
29
+ end
30
+
31
+ def output_file
32
+ @output_file || raise(OutputFileNotSetError)
33
+ end
34
+
35
+ private
36
+
37
+ def define_task
38
+ desc 'Mysqldump to backup file'
39
+ task task_name => dependencies do
40
+ execute_task
41
+ end
42
+ end
43
+
44
+ def execute_task
45
+ backup = Koine::DbBkp::Mysql::Dump.new(configuration)
46
+ backup.to_sql_file(output_file)
47
+ end
48
+
49
+ def configuration
50
+ {
51
+ url: url,
52
+ hostname: hostname,
53
+ database: database,
54
+ username: username,
55
+ password: password
56
+ }
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module Koine
5
+ module Tasks
6
+ class RailsMysqlDump < MysqlDump
7
+ def initialize(*args)
8
+ self.dependencies ||= [:environment]
9
+ super(*args)
10
+ end
11
+
12
+ private
13
+
14
+ def configuration
15
+ config = super.reject { |k, v| v.nil? }
16
+ rails_config.merge(config)
17
+ end
18
+
19
+ def rails_config
20
+ env = Rails.env.to_s
21
+ Rails.configuration.database_configuration[env].symbolize_keys
22
+ end
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koine-db_bkp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
@@ -88,10 +88,9 @@ files:
88
88
  - lib/koine/db_bkp/cli.rb
89
89
  - lib/koine/db_bkp/file_name.rb
90
90
  - lib/koine/db_bkp/mysql/dump.rb
91
- - lib/koine/db_bkp/mysql/rails_dump.rb
92
91
  - lib/koine/db_bkp/version.rb
93
- - lib/koine/tasks/mysql_dump.rake
94
- - lib/koine/tasks/mysql_rails_dump.rake
92
+ - lib/koine/tasks/mysql_dump.rb
93
+ - lib/koine/tasks/rails_mysql_dump.rb
95
94
  homepage: https://github.com/mjacobus/koine-db_bkp
96
95
  licenses:
97
96
  - MIT
@@ -1,62 +0,0 @@
1
- require 'addressable/uri'
2
-
3
- module Koine
4
- module DbBkp
5
- module Mysql
6
- class RailsDump
7
- def initialize(config = DatabaseConfig.new.to_h)
8
- @backup = Dump.new(config)
9
- end
10
-
11
- def to_sql_file(file)
12
- @backup.to_sql_file(file)
13
- end
14
-
15
- class DatabaseConfig
16
- InvalidAdapter = Class.new(StandardError)
17
- def to_h
18
- config = symbolize_keys(database_configuration[env].to_h)
19
- config = merge_url(config) if config[:url]
20
- assert_adapter(config[:adapter])
21
- config
22
- end
23
-
24
- def env
25
- Rails.env.to_s
26
- end
27
-
28
- def database_configuration
29
- Rails.configuration.database_configuration
30
- end
31
-
32
- private
33
-
34
- def symbolize_keys(hash)
35
- {}.tap do |new_hash|
36
- hash.each do |key, value|
37
- new_hash[key.to_sym] = value
38
- end
39
- end
40
- end
41
-
42
- def merge_url(config)
43
- url = config.delete(:url)
44
- url = Addressable::URI.parse(url)
45
-
46
- config.merge(
47
- adapter: url.scheme,
48
- hostname: url.host,
49
- database: url.path.split('/').join(''),
50
- username: url.user,
51
- password: url.password
52
- )
53
- end
54
-
55
- def assert_adapter(adapter)
56
- raise InvalidAdapter unless adapter.to_s =~ /mysql/
57
- end
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,26 +0,0 @@
1
- require 'rake'
2
-
3
- namespace :koine do
4
- namespace :mysql do
5
- desc 'mysqldump to backup file'
6
- task :dump do
7
-
8
- hostname = ENV.fetch('DB_HOST')
9
- database = ENV.fetch('DB_NAME')
10
- username = ENV.fetch('DB_USER')
11
- password = ENV.fetch('DB_PASSWORD') { nil }
12
- file_pattern = ENV.fetch('MYSQL_BACKUP_FILE')
13
-
14
- file_pattern = file
15
-
16
- backup = Koine::DbBkp::Mysql::Dump.new(
17
- hostname: hostname,
18
- database: database,
19
- username: username,
20
- password: password,
21
- )
22
-
23
- backup.to_sql_file(file)
24
- end
25
- end
26
- end
@@ -1,13 +0,0 @@
1
- require 'rake'
2
-
3
- namespace :koine do
4
- namespace :mysql do
5
- desc 'mysqldump to backup file from rails database'
6
- task rails_dump: [:environment] do
7
- file_pattern = ENV.fetch('MYSQL_BACKUP_FILE')
8
- file_pattern = file
9
-
10
- Koine::DbBkp::Mysql::RailsDump.new.to_sql_file(file)
11
- end
12
- end
13
- end