cap_db_dump 1.3.1 → 1.3.2
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 +4 -4
- data/lib/cap_db_dump/recipes.rb +49 -4
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 634f242534913169dcad7483e973eabf67248bc7615a3efa44db678195e9a9ae
|
|
4
|
+
data.tar.gz: 87cd8de8ad173684cee9963b6c14717a6135affb8db7c45cba3959978cd1b7ae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 39108992543cb7bd245f2fbd18ba463599bbd16d5f310675c860b0e5ef3ad37f349bd8f698fe35bb2c5c4180982cb9a354e1169aa110efef200e19a065722e44
|
|
7
|
+
data.tar.gz: 4375688a259683891cf56f77c43e3624b50c7ca024a061f09c813432199922758922f801d3019492565b0644badcbd3eaa1f168e69241ee223467ed3866ffdca
|
data/lib/cap_db_dump/recipes.rb
CHANGED
|
@@ -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
|
-
|
|
67
|
-
|
|
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
|
|
152
|
+
database_port? ? "-p #{database_port}" : ""
|
|
108
153
|
end
|
|
109
154
|
|
|
110
155
|
task :create_dump, tasks_matching_for_db_dump do
|
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.
|
|
4
|
+
version: 1.3.2
|
|
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-
|
|
11
|
+
date: 2026-10-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: capistrano
|