cap_db_dump 1.2.2 → 1.3.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cap_db_dump/recipes.rb +98 -17
  3. metadata +39 -14
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 010ed3cf256f446566acc86e8cbf49aa2d4965ea5f0a057dd436c4f41c53dea0
4
+ data.tar.gz: 40adc98e605ef94aee244c90ad859bfc01551dc8f6c62a95a1335455e8851c6b
5
+ SHA512:
6
+ metadata.gz: 39ac68fc4e29e92b771ed8ccdb2cda3efe517d2b0b99d91db9bbe0b59034bec653334954e93e9098ff4ebd977419596c52b39e63af510dfa37124dc78028e53b
7
+ data.tar.gz: 8b48771c21890446cc650b1e1bd33e82390ec25933ef08c85fba01fff043a20d0b3793e9ef3638631866e137f28734f8ec22b8f7501d483d547543e30fd159e7
@@ -1,9 +1,17 @@
1
1
  Capistrano::Configuration.instance(:must_exist).load do
2
2
  namespace :database do
3
+ DATABASE_ENGINES = [
4
+ MYSQL = :mysql,
5
+ POSTGRES = :psql,
6
+ ]
7
+
3
8
  # a list of tables for which only the schema, but no data should be dumped.
4
9
  set :schema_only_tables, []
5
10
  set :dump_root_path, "/tmp"
6
11
  set :formatted_time, Time.now.utc.strftime("%Y-%m-%d-%H:%M:%S")
12
+ set :database_engine, :mysql # specify :mysql | :psql
13
+ # https://www.postgresql.org/docs/12/app-pgdump.html
14
+ set :pg_dump_format, :c # specify c: 'compressed' (aka -Fc), :d 'directory', or set to nil for plain text
7
15
 
8
16
  module CapDbDumpHelpers
9
17
  def dump_path
@@ -30,6 +38,10 @@ Capistrano::Configuration.instance(:must_exist).load do
30
38
  database_yml_in_env["password"]
31
39
  end
32
40
 
41
+ def database_port
42
+ database_yml_in_env["port"]
43
+ end
44
+
33
45
  def database_yml_in_env
34
46
  database_yml[rails_env]
35
47
  end
@@ -47,29 +59,86 @@ Capistrano::Configuration.instance(:must_exist).load do
47
59
 
48
60
  task :read_db_yml, tasks_matching_for_db_dump do
49
61
  @database_yml ||= begin
50
- yaml = capture("cat #{shared_path}/config/database.yml")
51
- YAML.load(yaml)
62
+ if dry_run
63
+ raise "Cannot be run in dry_run mode!"
64
+ end
65
+
66
+ yaml = capture("cat #{current_path}/config/database.yml")
67
+ YAML.safe_load(yaml, aliases: true)
52
68
  end
53
69
  end
54
70
 
55
- def password_field
56
- database_password && database_password.length > 0 ? "-p#{database_password}" : ""
71
+ def database_password?
72
+ database_password && !database_password.to_s.empty?
57
73
  end
58
74
 
59
- task :create_dump, tasks_matching_for_db_dump do
60
- ignored_tables = schema_only_tables.map { |table_name|
61
- "--ignore-table=#{database_name}.#{table_name}"
62
- }
75
+ # --defaults-extra-file must be the first option given to mysqldump
76
+ def mysql_password_field
77
+ database_password? ? "--defaults-extra-file=/dev/stdin" : ""
78
+ end
63
79
 
64
- ignored_tables = ignored_tables.join(" ")
80
+ def mysql_password_stdin
81
+ escaped_password = database_password.to_s.gsub("\\") { "\\\\" }
82
+ "[client]\npassword=\"#{escaped_password}\"\n"
83
+ end
84
+
85
+ def postgres_port
86
+ database_port ? "-p #{database_port}" : ""
87
+ end
65
88
 
66
- command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} -Q "
67
- command << "--add-drop-table -O add-locks=FALSE --lock-tables=FALSE --single-transaction "
68
- command << "#{ignored_tables} #{database_name} > #{dump_path}"
89
+ def pg_password
90
+ database_password? ? "IFS= read -r PGPASSWORD && export PGPASSWORD &&" : ""
91
+ end
92
+
93
+ def pg_password_stdin
94
+ "#{database_password}\n"
95
+ end
96
+
97
+ # A pty would echo the password on stdin back into the (logged) output.
98
+ def run_with_password(command, stdin_data)
99
+ if database_password?
100
+ run command, :data => stdin_data, :eof => true, :pty => false
101
+ else
102
+ run command
103
+ end
104
+ end
105
+
106
+ def pg_port
107
+ database_port && !database_port.empty? ? "-p #{database_port}" : ""
108
+ end
109
+
110
+ task :create_dump, tasks_matching_for_db_dump do
111
+ if database_engine == MYSQL
112
+ ignored_tables = schema_only_tables.map { |table_name|
113
+ "--ignore-table=#{database_name}.#{table_name}"
114
+ }
115
+
116
+ ignored_tables = ignored_tables.join(" ")
117
+
118
+ command = "mysqldump #{mysql_password_field} -u #{database_username} -h #{database_host} -Q "
119
+ command << "--add-drop-table -O add-locks=FALSE --lock-tables=FALSE --single-transaction "
120
+ command << "#{ignored_tables} #{database_name} > #{dump_path}"
121
+ password_stdin = mysql_password_stdin
122
+ elsif database_engine == POSTGRES
123
+ ignored_tables = schema_only_tables.map { |table_name|
124
+ "--exclude-table=#{database_name}.#{table_name}"
125
+ }
126
+
127
+ ignored_tables = ignored_tables.join(" ")
128
+
129
+ command = "#{pg_password} pg_dump -U #{database_username} -h #{database_host} #{postgres_port} "
130
+ if pg_dump_format
131
+ command << "-F#{pg_dump_format} "
132
+ end
133
+ command << "#{ignored_tables} #{database_name} > #{dump_path}"
134
+ password_stdin = pg_password_stdin
135
+ else
136
+ raise "Unknown database engine. use one of: #{DATABASE_ENGINES.inspect}"
137
+ end
69
138
 
70
139
  give_description "About to dump production DB"
71
140
 
72
- run command
141
+ run_with_password command, password_stdin
73
142
  dump_schema_tables if schema_only_tables.any?
74
143
  end
75
144
 
@@ -77,11 +146,17 @@ Capistrano::Configuration.instance(:must_exist).load do
77
146
  if schema_only_tables.any?
78
147
  table_names = schema_only_tables.join(" ")
79
148
 
80
- command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} "
81
- command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
149
+ if database_engine == MYSQL
150
+ command = "mysqldump #{mysql_password_field} -u #{database_username} -h #{database_host} "
151
+ command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
152
+ elsif database_engine == POSTGRES
153
+ raise "not yet supported. PR's welcome! (https://github.com/smtlaissezfaire/cap_db_dump)"
154
+ else
155
+ raise "Unknown database engine. use one of: #{DATABASE_ENGINES.inspect}"
156
+ end
82
157
 
83
158
  give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
84
- run command
159
+ run_with_password command, mysql_password_stdin
85
160
  end
86
161
  end
87
162
 
@@ -102,8 +177,14 @@ Capistrano::Configuration.instance(:must_exist).load do
102
177
  end
103
178
 
104
179
  task :transfer, tasks_matching_for_db_dump do
180
+ # TODO: should look in /tmp for latest file
105
181
  give_description "Grabbing the dump"
106
- download("#{dump_path}.gz", ".", :via => :scp)
182
+ gzip_file = "#{dump_path}.gz"
183
+ download(gzip_file, ".", :via => :scp)
184
+
185
+ give_description "Symlinking locally"
186
+ base_name = File.basename(gzip_file)
187
+ `ln -sf #{base_name} current.sql.gz`
107
188
  end
108
189
  end
109
190
  end
metadata CHANGED
@@ -1,16 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap_db_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
5
- prerelease:
4
+ version: 1.3.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Scott Taylor
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2026-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.15.10
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.15.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 7.2.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 7.2.3
14
41
  description: Capistrano tasks for dumping your mysql database + transfering to your
15
42
  local machine
16
43
  email: scott@railsnewbie.com
@@ -21,26 +48,24 @@ files:
21
48
  - lib/cap_db_dump/recipes.rb
22
49
  homepage: https://github.com/smtlaissezfaire/cap_db_dump
23
50
  licenses: []
24
- post_install_message:
51
+ metadata: {}
52
+ post_install_message:
25
53
  rdoc_options: []
26
54
  require_paths:
27
55
  - lib
28
56
  required_ruby_version: !ruby/object:Gem::Requirement
29
- none: false
30
57
  requirements:
31
- - - ! '>='
58
+ - - ">="
32
59
  - !ruby/object:Gem::Version
33
60
  version: '0'
34
61
  required_rubygems_version: !ruby/object:Gem::Requirement
35
- none: false
36
62
  requirements:
37
- - - ! '>='
63
+ - - ">="
38
64
  - !ruby/object:Gem::Version
39
65
  version: '0'
40
66
  requirements: []
41
- rubyforge_project:
42
- rubygems_version: 1.8.24
43
- signing_key:
44
- specification_version: 3
67
+ rubygems_version: 3.5.9
68
+ signing_key:
69
+ specification_version: 4
45
70
  summary: cap_db_dump
46
71
  test_files: []