cap_db_dump 1.3.1 → 1.3.3

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 +4 -4
  2. data/lib/cap_db_dump/recipes.rb +54 -9
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 010ed3cf256f446566acc86e8cbf49aa2d4965ea5f0a057dd436c4f41c53dea0
4
- data.tar.gz: 40adc98e605ef94aee244c90ad859bfc01551dc8f6c62a95a1335455e8851c6b
3
+ metadata.gz: 822af40344c861597562ff8fdb51b7ad8af1ed30f34851220cd24441c16d9dc4
4
+ data.tar.gz: 6dab1976b58d667552c715de402cc5cfe6ea832e442e88ece57325a93e21283f
5
5
  SHA512:
6
- metadata.gz: 39ac68fc4e29e92b771ed8ccdb2cda3efe517d2b0b99d91db9bbe0b59034bec653334954e93e9098ff4ebd977419596c52b39e63af510dfa37124dc78028e53b
7
- data.tar.gz: 8b48771c21890446cc650b1e1bd33e82390ec25933ef08c85fba01fff043a20d0b3793e9ef3638631866e137f28734f8ec22b8f7501d483d547543e30fd159e7
6
+ metadata.gz: 28e621ce4e25b43097096957c9203eecc76971477026a351339d7a027e678bed39ecbe649ef43f96a35892faf86bbc8b5f9bb99c10d93659fcb532bc96e8f7dc
7
+ data.tar.gz: 024ab1acf0daf35d58b2564fbedba76d102a702a9d7ffe7f86adc77968cbee0dd8ecd2f20e206d6c75fe98341c3eda0be3ccab6466dbb9b4353cf7e00707b673
@@ -1,3 +1,6 @@
1
+ require "json"
2
+ require "yaml"
3
+
1
4
  Capistrano::Configuration.instance(:must_exist).load do
2
5
  namespace :database do
3
6
  DATABASE_ENGINES = [
@@ -12,6 +15,9 @@ Capistrano::Configuration.instance(:must_exist).load do
12
15
  set :database_engine, :mysql # specify :mysql | :psql
13
16
  # https://www.postgresql.org/docs/12/app-pgdump.html
14
17
  set :pg_dump_format, :c # specify c: 'compressed' (aka -Fc), :d 'directory', or set to nil for plain text
18
+ # :yaml reads config/database.yml as plain YAML, :rails asks `rails runner` on the server for the
19
+ # resolved config, and :auto uses :yaml unless database.yml contains ERB.
20
+ set :database_config_source, :auto
15
21
 
16
22
  module CapDbDumpHelpers
17
23
  def dump_path
@@ -50,6 +56,32 @@ Capistrano::Configuration.instance(:must_exist).load do
50
56
  @database_yml ||= read_db_yml
51
57
  end
52
58
 
59
+ def database_yml_path
60
+ "#{current_path}/config/database.yml"
61
+ end
62
+
63
+ def database_config_from_yaml(yaml)
64
+ YAML.safe_load(yaml, aliases: true)
65
+ end
66
+
67
+ # rvm-capistrano wraps commands in `rvm-shell '...' -c '...'`, so this must not contain single quotes.
68
+ def rails_runner_database_config_command
69
+ ruby = "puts :CAP_DB_DUMP_CONFIG_BEGIN, ActiveRecord::Base.connection_db_config.configuration_hash.to_json, :CAP_DB_DUMP_CONFIG_END"
70
+ "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rails runner \"#{ruby}\""
71
+ end
72
+
73
+ # No pty, so stderr stays out of the captured output and the output (which has the password) isn't echoed.
74
+ def database_config_from_rails
75
+ output = capture(rails_runner_database_config_command, :pty => false)
76
+ json = output[/CAP_DB_DUMP_CONFIG_BEGIN\s*(\{.*?\})\s*CAP_DB_DUMP_CONFIG_END/m, 1]
77
+
78
+ unless json
79
+ raise "Could not find the database config in the output of rails runner"
80
+ end
81
+
82
+ { rails_env.to_s => JSON.parse(json) }
83
+ end
84
+
53
85
  def tasks_matching_for_db_dump
54
86
  { :only => { :db_dump => true } }
55
87
  end
@@ -63,8 +95,17 @@ Capistrano::Configuration.instance(:must_exist).load do
63
95
  raise "Cannot be run in dry_run mode!"
64
96
  end
65
97
 
66
- yaml = capture("cat #{current_path}/config/database.yml")
67
- YAML.safe_load(yaml, aliases: true)
98
+ case database_config_source.to_sym
99
+ when :yaml
100
+ database_config_from_yaml(capture("cat #{database_yml_path}"))
101
+ when :rails
102
+ database_config_from_rails
103
+ when :auto
104
+ yaml = capture("cat #{database_yml_path}")
105
+ yaml.include?("<%") ? database_config_from_rails : database_config_from_yaml(yaml)
106
+ else
107
+ raise "Unknown database_config_source. use one of: [:yaml, :rails, :auto]"
108
+ end
68
109
  end
69
110
  end
70
111
 
@@ -82,8 +123,12 @@ Capistrano::Configuration.instance(:must_exist).load do
82
123
  "[client]\npassword=\"#{escaped_password}\"\n"
83
124
  end
84
125
 
126
+ def database_port?
127
+ database_port && !database_port.to_s.strip.empty?
128
+ end
129
+
85
130
  def postgres_port
86
- database_port ? "-p #{database_port}" : ""
131
+ database_port? ? "-p #{database_port}" : ""
87
132
  end
88
133
 
89
134
  def pg_password
@@ -104,7 +149,7 @@ Capistrano::Configuration.instance(:must_exist).load do
104
149
  end
105
150
 
106
151
  def pg_port
107
- database_port && !database_port.empty? ? "-p #{database_port}" : ""
152
+ database_port? ? "-p #{database_port}" : ""
108
153
  end
109
154
 
110
155
  task :create_dump, tasks_matching_for_db_dump do
@@ -121,7 +166,7 @@ Capistrano::Configuration.instance(:must_exist).load do
121
166
  password_stdin = mysql_password_stdin
122
167
  elsif database_engine == POSTGRES
123
168
  ignored_tables = schema_only_tables.map { |table_name|
124
- "--exclude-table=#{database_name}.#{table_name}"
169
+ "--exclude-table-data=#{table_name}"
125
170
  }
126
171
 
127
172
  ignored_tables = ignored_tables.join(" ")
@@ -149,14 +194,14 @@ Capistrano::Configuration.instance(:must_exist).load do
149
194
  if database_engine == MYSQL
150
195
  command = "mysqldump #{mysql_password_field} -u #{database_username} -h #{database_host} "
151
196
  command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
197
+
198
+ give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
199
+ run_with_password command, mysql_password_stdin
152
200
  elsif database_engine == POSTGRES
153
- raise "not yet supported. PR's welcome! (https://github.com/smtlaissezfaire/cap_db_dump)"
201
+ # pg_dump --exclude-table-data in create_dump already keeps the schema for these tables
154
202
  else
155
203
  raise "Unknown database engine. use one of: #{DATABASE_ENGINES.inspect}"
156
204
  end
157
-
158
- give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
159
- run_with_password command, mysql_password_stdin
160
205
  end
161
206
  end
162
207
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap_db_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Taylor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-03 00:00:00.000000000 Z
11
+ date: 2026-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubygems_version: 3.5.9
67
+ rubygems_version: 3.5.22
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: cap_db_dump