cap_db_dump 1.2.1 → 1.3.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b17670872f50b5f6e4ced82d27850b86d69f5f544c16ffd2255f9be0550d0f5
4
+ data.tar.gz: 95a520425a6edf34da30890fb9cc898fac58b20c9ba5c152b6ca0e7dd7fe89ec
5
+ SHA512:
6
+ metadata.gz: 4e9a93f1adcbbf42ceeff9f4da05913f2df61ca31be12afa6c42fb44dd0334258f633cc0ce3f7cf91ad5938668b0031ea27df22962f8f9bafc4874983e7208ba
7
+ data.tar.gz: a990d7fc1d50cf589c1d0b0af8be81aa238f6c1e75a7f55855d28170213a3f9357aebcd63fbd248a2988fa5ba5afe8e038aa5d2edfcaa362adb2270fec6cddd9
@@ -0,0 +1,167 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :database do
3
+ DATABASE_ENGINES = [
4
+ MYSQL = :mysql,
5
+ POSTGRES = :psql,
6
+ ]
7
+
8
+ # a list of tables for which only the schema, but no data should be dumped.
9
+ set :schema_only_tables, []
10
+ set :dump_root_path, "/tmp"
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
15
+
16
+ module CapDbDumpHelpers
17
+ def dump_path
18
+ "#{dump_root_path}/#{database_name}_dump_#{formatted_time}.sql"
19
+ end
20
+
21
+ def give_description(desc_string)
22
+ puts " ** #{desc_string}"
23
+ end
24
+
25
+ def database_name
26
+ database_yml_in_env["database"]
27
+ end
28
+
29
+ def database_username
30
+ database_yml_in_env["username"]
31
+ end
32
+
33
+ def database_host
34
+ database_yml_in_env["host"] || "localhost"
35
+ end
36
+
37
+ def database_password
38
+ database_yml_in_env["password"]
39
+ end
40
+
41
+ def database_port
42
+ database_yml_in_env["port"]
43
+ end
44
+
45
+ def database_yml_in_env
46
+ database_yml[rails_env]
47
+ end
48
+
49
+ def database_yml
50
+ @database_yml ||= read_db_yml
51
+ end
52
+
53
+ def tasks_matching_for_db_dump
54
+ { :only => { :db_dump => true } }
55
+ end
56
+ end
57
+
58
+ extend CapDbDumpHelpers
59
+
60
+ task :read_db_yml, tasks_matching_for_db_dump do
61
+ @database_yml ||= begin
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)
68
+ end
69
+ end
70
+
71
+ def mysql_password_field
72
+ database_password && database_password.length > 0 ? "-p#{database_password}" : ""
73
+ end
74
+
75
+ def postgres_port
76
+ database_port ? "-p #{database_port}" : ""
77
+ end
78
+
79
+ def pg_password
80
+ database_password && !database_password.empty? ?
81
+ "PGPASSWORD=#{database_password}" :
82
+ ""
83
+ end
84
+
85
+ def pg_port
86
+ database_port && !database_port.empty? ? "-p #{database_port}" : ""
87
+ end
88
+
89
+ task :create_dump, tasks_matching_for_db_dump do
90
+ if database_engine == MYSQL
91
+ ignored_tables = schema_only_tables.map { |table_name|
92
+ "--ignore-table=#{database_name}.#{table_name}"
93
+ }
94
+
95
+ ignored_tables = ignored_tables.join(" ")
96
+
97
+ command = "mysqldump -u #{database_username} -h #{database_host} #{mysql_password_field} -Q "
98
+ command << "--add-drop-table -O add-locks=FALSE --lock-tables=FALSE --single-transaction "
99
+ command << "#{ignored_tables} #{database_name} > #{dump_path}"
100
+ elsif database_engine == POSTGRES
101
+ ignored_tables = schema_only_tables.map { |table_name|
102
+ "--exclude-table=#{database_name}.#{table_name}"
103
+ }
104
+
105
+ ignored_tables = ignored_tables.join(" ")
106
+
107
+ command = "#{pg_password} pg_dump -U #{database_username} -h #{database_host} #{postgres_port} "
108
+ if pg_dump_format
109
+ command << "-F#{pg_dump_format} "
110
+ end
111
+ command << "#{ignored_tables} #{database_name} > #{dump_path}"
112
+ else
113
+ raise "Unknown database engine. use one of: #{DATABASE_ENGINES.inspect}"
114
+ end
115
+
116
+ give_description "About to dump production DB"
117
+
118
+ run command
119
+ dump_schema_tables if schema_only_tables.any?
120
+ end
121
+
122
+ task :dump_schema_tables, tasks_matching_for_db_dump do
123
+ if schema_only_tables.any?
124
+ table_names = schema_only_tables.join(" ")
125
+
126
+ if database_engine == MYSQL
127
+ command = "mysqldump -u #{database_username} -h #{database_host} #{mysql_password_field} "
128
+ command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
129
+ elsif database_engine == POSTGRES
130
+ raise "not yet supported. PR's welcome! (https://github.com/smtlaissezfaire/cap_db_dump)"
131
+ else
132
+ raise "Unknown database engine. use one of: #{DATABASE_ENGINES.inspect}"
133
+ end
134
+
135
+ give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
136
+ run command
137
+ end
138
+ end
139
+
140
+ desc "Create a dump of the production database"
141
+ task :dump, tasks_matching_for_db_dump do
142
+ create_dump
143
+
144
+ cmd = "gzip -9 #{dump_path}"
145
+
146
+ give_description "Gzip'ing the file"
147
+ run cmd
148
+ end
149
+
150
+ desc "Make a production dump, transfer it to this machine"
151
+ task :dump_and_transfer, tasks_matching_for_db_dump do
152
+ dump
153
+ transfer
154
+ end
155
+
156
+ task :transfer, tasks_matching_for_db_dump do
157
+ # TODO: should look in /tmp for latest file
158
+ give_description "Grabbing the dump"
159
+ gzip_file = "#{dump_path}.gz"
160
+ download(gzip_file, ".", :via => :scp)
161
+
162
+ give_description "Symlinking locally"
163
+ base_name = File.basename(gzip_file)
164
+ `ln -sf #{base_name} current.sql.gz`
165
+ end
166
+ end
167
+ 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.1
5
- prerelease:
4
+ version: 1.3.0
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: 2024-05-24 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
@@ -18,29 +45,27 @@ executables: []
18
45
  extensions: []
19
46
  extra_rdoc_files: []
20
47
  files:
21
- - lib/cap_db_dump.rb
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: []
data/lib/cap_db_dump.rb DELETED
@@ -1,109 +0,0 @@
1
- Capistrano::Configuration.instance(:must_exist).load do
2
- namespace :database do
3
- # a list of tables for which only the schema, but no data should be dumped.
4
- set :schema_only_tables, []
5
- set :dump_root_path, "/tmp"
6
- set :formatted_time, Time.now.utc.strftime("%Y-%m-%d-%H:%M:%S")
7
-
8
- module CapDbDumpHelpers
9
- def dump_path
10
- "#{dump_root_path}/#{database_name}_dump_#{formatted_time}.sql"
11
- end
12
-
13
- def give_description(desc_string)
14
- puts " ** #{desc_string}"
15
- end
16
-
17
- def database_name
18
- database_yml_in_env["database"]
19
- end
20
-
21
- def database_username
22
- database_yml_in_env["username"]
23
- end
24
-
25
- def database_host
26
- database_yml_in_env["host"] || "localhost"
27
- end
28
-
29
- def database_password
30
- database_yml_in_env["password"]
31
- end
32
-
33
- def database_yml_in_env
34
- database_yml[rails_env]
35
- end
36
-
37
- def database_yml
38
- @database_yml ||= read_db_yml
39
- end
40
-
41
- def tasks_matching_for_db_dump
42
- { :only => { :db_dump => true } }
43
- end
44
- end
45
-
46
- extend CapDbDumpHelpers
47
-
48
- task :read_db_yml, tasks_matching_for_db_dump do
49
- @database_yml ||= begin
50
- yaml = capture("cat #{shared_path}/config/database.yml")
51
- YAML.load(yaml)
52
- end
53
- end
54
-
55
- def password_field
56
- database_password && database_password.length > 0 ? "-p#{database_password}" : ""
57
- end
58
-
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
- }
63
-
64
- ignored_tables = ignored_tables.join(" ")
65
-
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}"
69
-
70
- give_description "About to dump production DB"
71
-
72
- run command
73
- dump_schema_tables if schema_only_tables.any?
74
- end
75
-
76
- task :dump_schema_tables, tasks_matching_for_db_dump do
77
- if schema_only_tables.any?
78
- table_names = schema_only_tables.join(" ")
79
-
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}"
82
-
83
- give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
84
- run command
85
- end
86
- end
87
-
88
- desc "Create a dump of the production database"
89
- task :dump, tasks_matching_for_db_dump do
90
- create_dump
91
-
92
- cmd = "gzip -9 #{dump_path}"
93
-
94
- give_description "Gzip'ing the file"
95
- run cmd
96
- end
97
-
98
- desc "Make a production dump, transfer it to this machine"
99
- task :dump_and_transfer, tasks_matching_for_db_dump do
100
- dump
101
- transfer
102
- end
103
-
104
- task :transfer, tasks_matching_for_db_dump do
105
- give_description "Grabbing the dump"
106
- download("#{dump_path}.gz", ".", :via => :scp)
107
- end
108
- end
109
- end