capistrano-container-db 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6329984c7c290ed95ac3328fb8c6235a7a5d950b
4
- data.tar.gz: 25c9226e10aa0aba6ff49350f80bffc232543bb0
3
+ metadata.gz: 4f236a486192089b4688459ad3439ab874821f17
4
+ data.tar.gz: 37a43ae5ec9750be66df21d5d49094736c8d033d
5
5
  SHA512:
6
- metadata.gz: dfc3672495ff77d1744288fb276dd269b6471bc6c947d7e440326240e9e960ba4760756350cb20ed3b2f8612566c8259ffeedea2092d2ebf7bfb54aeeb20339b
7
- data.tar.gz: a565b87674d837c868d0953250421e08b1128397012e84d661db5f3ba2b672fdcd489d21aa53edbabe5fa224823bcdec322e197d34bf09563b8a41391199772a
6
+ metadata.gz: 4f3cca20d9bb697923e2d1d572f8848eb8c05a73ea8baf28712f19737478a1272950f275cceac9954950904a5dac4f211256f07ff091debd731eb9c03a978d38
7
+ data.tar.gz: d54bdf8c2518359144d3b4c37ccaa4ef3abdbb0fccddb5871de4d0f85c0a3d832bb8a725af252dbabc3ced11bf2d954c85d1d1d351ec43ace652631641bdff66
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Helps managing databases on local and remote stages, also on remote [docker](https://www.docker.com/) container for Capistrano 3.x.
4
4
 
5
- This project is in an early stage but helps me alot dealing with my container deployments and keeps my code clean. It is not only ment for mysql, but at the moment there only supports mysql, feel free to contribute =)
5
+ This project is in an early stage but helps me alot dealing with my container deployments and keeps my code clean. It is not only ment for mysql, but at the moment there is only support for mysql, feel free to contribute =)
6
6
 
7
7
  This gem depends on [capistrano-container](https://github.com/creative-workflow/capistrano-container).
8
8
 
@@ -62,13 +62,19 @@ Dont forget to add a server (even for local stage) with the role `db`. `server '
62
62
  ```ruby
63
63
  cap db:export # export a local, remote or remote container mysql db
64
64
  cap db:import # import a local, remote or remote container mysql db
65
+ cap db:execute # execute a mysql command local, remote or container host
65
66
  ```
66
67
 
67
68
  ### default configuration
68
69
  ```ruby
69
70
  set :db_user, 'root'
70
71
  set :db_pass, ''
72
+ set :db_additional_auth_args, []
71
73
  set :db_name, ''
74
+ set :db_additional_restore_args, []
75
+ # dont use --database statement, so no use '...' will be generated and we
76
+ # can have different db names local and remote
77
+ set :db_additional_dump_args, ['--no-create-db']
72
78
  set :db_remote_dump, '/tmp/dump.sql'
73
79
  set :db_local_dump, 'config/db/dump.sql'
74
80
  set :db_is_container, false
@@ -80,11 +86,17 @@ set :filter_on_import, lambda{ |sql_dump| return sql_dump } -> !not implemented
80
86
  ## TODO
81
87
  * adapter pattern for other db engines.
82
88
  * integration tests.
89
+ * :filter_on_import
90
+ * add db:execute task
83
91
 
84
92
  ## Changes
93
+ ### Version 0.0.5
94
+ * add "CREATE DATABASE IF NOT EXISTS" to import statements
95
+ * provide additional mysql auth/dump/restore args
96
+
85
97
  ### Version 0.0.4
86
98
  * readme
87
-
99
+
88
100
  ### Version 0.0.3
89
101
  * remove debug expression
90
102
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'capistrano-container-db'
7
- spec.version = '0.0.4'
7
+ spec.version = '0.0.5'
8
8
  spec.date = '2016-09-30'
9
9
  spec.summary = 'Helps managing databases on local and remote stages, also on remote docker container'
10
10
  spec.description = spec.summary
@@ -3,6 +3,15 @@ require_relative('../../db/dump_helper.rb')
3
3
  require_relative('../../db/load_helper.rb')
4
4
 
5
5
  namespace :db do
6
+ desc "execute a mysql command local, remote or container host"
7
+ task :execute do
8
+ on roles(:db, :container_host) do |host|
9
+ ask(:tmp_cmd, "mysql command")
10
+ Helper.execute_db_command_autodetect fetch(:tmp_cmd)
11
+ end
12
+ end
13
+
14
+
6
15
  desc "export a local, remote or remote container mysql db"
7
16
  task :export do
8
17
  on roles(:db, :container_host) do |host|
@@ -36,7 +45,12 @@ namespace :load do
36
45
  task :defaults do
37
46
  set :db_user, 'root'
38
47
  set :db_pass, ''
48
+ set :db_additional_auth_args, []
39
49
  set :db_name, ''
50
+ set :db_additional_restore_args, []
51
+ # dont use --database statement, so no use '...' will be generated and we
52
+ # can have different db names local and remote
53
+ set :db_additional_dump_args, ['--no-create-db']
40
54
  set :db_remote_dump, '/tmp/dump.sql'
41
55
  set :db_local_dump, 'config/db/dump.sql'
42
56
  set :db_is_container, false
@@ -2,7 +2,7 @@ require_relative "helper.rb"
2
2
 
3
3
  module DumpHelper
4
4
  def self.dump_on_local()
5
- args = Helper::mysql_args(['--no-create-db'])
5
+ args = Helper::mysql_dump_args
6
6
 
7
7
  run_locally do
8
8
  execute "mysqldump #{args} > #{fetch(:db_local_dump)}"
@@ -10,7 +10,7 @@ module DumpHelper
10
10
  end
11
11
 
12
12
  def self.dump_on_server_and_download()
13
- args = Helper::mysql_args(['--no-create-db'])
13
+ args = Helper::mysql_dump_args
14
14
 
15
15
  execute "mysqldump #{args} > #{fetch(:db_remote_dump)}"
16
16
 
@@ -18,7 +18,7 @@ module DumpHelper
18
18
  end
19
19
 
20
20
  def self.dump_on_container_and_download(container)
21
- args = Helper::mysql_args(['--no-create-db'])
21
+ args = Helper::mysql_dump_args
22
22
 
23
23
  container.execute("mysqldump #{args} > #{fetch(:db_remote_dump)}")
24
24
 
data/lib/db/helper.rb CHANGED
@@ -1,14 +1,25 @@
1
1
  module Helper
2
- def self.mysql_args(additional_args=[])
3
- command = " -u #{fetch(:db_user)}"
4
- command+= " -p#{fetch(:db_pass)}" unless fetch(:db_pass).empty?
5
- command+= " #{additional_args.join(' ')} "
2
+ def self.mysql_dump_args
3
+ command = mysql_auth_args
4
+ command+= " #{fetch(:db_additional_dump_args).join(' ')} "
5
+ command+= " #{fetch(:db_name)}" unless fetch(:db_name).empty?
6
+ command
7
+ end
6
8
 
7
- # dont use --database statement, so no use '...' will be generated
9
+ def self.mysql_restore_args
10
+ command = mysql_auth_args
11
+ command+= " #{fetch(:db_additional_restore_args).join(' ')} "
8
12
  command+= " #{fetch(:db_name)}" unless fetch(:db_name).empty?
9
13
  command
10
14
  end
11
15
 
16
+ def self.mysql_auth_args
17
+ command = " -u #{fetch(:db_user)}"
18
+ command+= " -p#{fetch(:db_pass)}" unless fetch(:db_pass).empty?
19
+ command+= " #{fetch(:db_additional_auth_args).join(' ')} "
20
+ command
21
+ end
22
+
12
23
  def self.append_stage_to_filename(file_name, stage = 'local')
13
24
  splitted = file_name.split('.')
14
25
  extension = splitted.pop
@@ -26,13 +37,16 @@ module Helper
26
37
  fetch(:local_stage_name).to_sym == fetch(:stage).to_sym
27
38
  end
28
39
 
29
- def execute_local_or_remote(cmd)
30
- if local_stage?
31
- run_locally do
32
- execute cmd
40
+ def self.execute_db_command_autodetect(cmd)
41
+ cmd = "mysql #{Helper::mysql_auth_args} -e \"#{cmd}\""
42
+
43
+ if fetch(:db_is_container)
44
+ db_container = container_by_name fetch(:db_container_name)
45
+ on_container db_container do |container|
46
+ container.execute cmd
33
47
  end
34
48
  else
35
- execute cmd
49
+ execute_local_or_remote cmd
36
50
  end
37
51
  end
38
52
  end
@@ -1,21 +1,32 @@
1
1
  require_relative "helper.rb"
2
+ require 'capistrano/container'
2
3
 
3
4
  module LoadHelper
4
5
  def self.import_on_local()
6
+ LoadHelper.create_db_if_not_exists fetch(:db_name)
7
+
5
8
  run_locally do
6
- execute "mysql #{Helper::mysql_args} < #{fetch(:db_local_dump)}"
9
+ execute "mysql #{Helper::mysql_restore_args} < #{fetch(:db_local_dump)}"
7
10
  end
8
11
  end
9
12
 
10
13
  def self.import_on_container(container)
11
14
  container.upload!(fetch(:db_local_dump), fetch(:db_remote_dump))
12
15
 
13
- container.execute("mysql #{Helper::mysql_args} < #{fetch(:db_remote_dump)}")
16
+ LoadHelper.create_db_if_not_exists fetch(:db_name)
17
+
18
+ container.execute "mysql #{Helper::mysql_restore_args} < #{fetch(:db_remote_dump)}"
14
19
  end
15
20
 
16
21
  def self.import_on_server()
17
22
  upload!(fetch(:db_local_dump), fetch(:db_remote_dump))
18
23
 
19
- execute("mysql #{Helper::mysql_args} < #{fetch(:db_remote_dump)}")
24
+ LoadHelper.create_db_if_not_exists fetch(:db_name)
25
+
26
+ execute("mysql #{Helper::mysql_restore_args} < #{fetch(:db_remote_dump)}")
27
+ end
28
+
29
+ def self.create_db_if_not_exists(db)
30
+ Helper.execute_db_command_autodetect "CREATE DATABASE IF NOT EXISTS #{db};"
20
31
  end
21
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-container-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Hanoldt