db_dumper 0.5.5 → 0.5.6
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/README.md +5 -3
- data/lib/db_dumper.rb +4 -3
- data/lib/db_dumper/configuration.rb +2 -2
- data/lib/db_dumper/configuration/postgres.rb +6 -1
- data/lib/db_dumper/query_builder.rb +16 -8
- data/lib/db_dumper/remote_machine.rb +17 -32
- data/lib/db_dumper/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed06d69d92559ef72d5d35f59dcf349dc41b77d88cdc6cb5e52463586fb3de31
|
4
|
+
data.tar.gz: bdeb2734063ecc60cab6bb3cd763c72c7c518a379383c6baa3b4a3f598ac372b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 682c19468845eb7b8990d17de0464f6d47c6b33cc5299d5de3fc3e7855949e8c4e3dd0bcb66c965757aa9f06f2583d95e6639bc6a172ba4c7f4866bf1b60f0bc
|
7
|
+
data.tar.gz: a27fb374bcfef21c04f504eb98f3744869b3b606af13441b82cad5f6c3dd5b4ec01e4148d6150f90c6d33115b8c46813d2fd84333e0611ace2f38a8916a002d7
|
data/README.md
CHANGED
@@ -28,11 +28,13 @@ gemfile do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
DbDumper.dump do
|
31
|
+
dump 'roles'
|
32
|
+
|
31
33
|
user_id = 1
|
32
|
-
|
34
|
+
copy q('users').where(id: user_id)
|
33
35
|
campaigns_q = q('campaigns').where('user_id = ? OR for_all IS TRUE', user_id)
|
34
|
-
|
35
|
-
|
36
|
+
copy campaigns_q
|
37
|
+
copy q('offices').where(campaign_id: campaigns_q.ar)
|
36
38
|
end
|
37
39
|
```
|
38
40
|
|
data/lib/db_dumper.rb
CHANGED
@@ -11,9 +11,10 @@ require_relative 'db_dumper/query_builder'
|
|
11
11
|
require_relative 'db_dumper/remote_machine'
|
12
12
|
|
13
13
|
module DbDumper
|
14
|
-
module_function def dump(config_file_path = 'config/
|
14
|
+
module_function def dump(config_file_path = 'config/dumper.yml', &block)
|
15
15
|
config = Configuration.new(config_file_path)
|
16
|
-
|
17
|
-
|
16
|
+
query = QueryBuilder.build(config, &block)
|
17
|
+
|
18
|
+
RemoteMachine.new(config, query.dumping_tables, query.copy_commands).dump
|
18
19
|
end
|
19
20
|
end
|
@@ -6,11 +6,11 @@ require_relative 'configuration/postgres'
|
|
6
6
|
module DbDumper
|
7
7
|
|
8
8
|
# Configuration class, by default loads from config/application.yml file
|
9
|
-
# see config/
|
9
|
+
# see config/dumper.sample.yml for format details
|
10
10
|
class Configuration
|
11
11
|
SshUser = Struct.new(:name, :host, :ssh_keys, :passphrase, keyword_init: true)
|
12
12
|
RemoteDB = Struct.new(:adapter, :host, :port, :database, :username, :password,
|
13
|
-
:dump_schema_options, :dump_data_options,
|
13
|
+
:dump_schema_options, :dump_data_options, :dump_copy_options,
|
14
14
|
keyword_init: true)
|
15
15
|
RemoteMachine = Struct.new(:dest_path, keyword_init: true) do
|
16
16
|
def data_path
|
@@ -9,8 +9,13 @@ module DbDumper
|
|
9
9
|
util_command('pg_dump', "#{db_config.dump_schema_options} -f #{dump_schema_file_path}")
|
10
10
|
end
|
11
11
|
|
12
|
+
def dump_table_data_command(dumped_tables, dump_table_data_file_path)
|
13
|
+
joined_tables = "-t #{dumped_tables.join(' -t ')}"
|
14
|
+
util_command('pg_dump', "#{db_config.dump_data_options} #{joined_tables} -f #{dump_table_data_file_path}")
|
15
|
+
end
|
16
|
+
|
12
17
|
def dump_data_command(dump_data_file_path)
|
13
|
-
util_command('psql', "#{db_config.
|
18
|
+
util_command('psql', "#{db_config.dump_copy_options} -c \"#{dump_data_file_path}\"")
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
@@ -6,19 +6,31 @@ module DbDumper
|
|
6
6
|
class QueryBuilder
|
7
7
|
attr_reader :config
|
8
8
|
|
9
|
-
def self.
|
9
|
+
def self.build(config, &block)
|
10
10
|
establish_connection
|
11
11
|
|
12
|
-
new(config).
|
12
|
+
new(config).tap do |instance|
|
13
13
|
instance.instance_eval(&block)
|
14
|
-
instance.send(:to_sql)
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
17
|
+
def dumping_tables
|
18
|
+
@dumping_tables ||= []
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_commands
|
22
|
+
queries.map(&method(:build_copy_command))
|
23
|
+
end
|
24
|
+
|
18
25
|
# DSL start
|
19
26
|
|
27
|
+
# Dump whole table to sql file
|
28
|
+
def dump(table_name)
|
29
|
+
dumping_tables << table_name
|
30
|
+
end
|
31
|
+
|
20
32
|
# add query to current dump
|
21
|
-
def
|
33
|
+
def copy(query)
|
22
34
|
queries << query
|
23
35
|
end
|
24
36
|
|
@@ -39,10 +51,6 @@ module DbDumper
|
|
39
51
|
|
40
52
|
private
|
41
53
|
|
42
|
-
def to_sql
|
43
|
-
queries.map(&method(:build_copy_command)).join("\n")
|
44
|
-
end
|
45
|
-
|
46
54
|
def initialize(config)
|
47
55
|
@config = config
|
48
56
|
end
|
@@ -2,20 +2,16 @@ require_relative 'remote_machine/ssh_agent'
|
|
2
2
|
|
3
3
|
module DbDumper
|
4
4
|
class RemoteMachine
|
5
|
-
attr_reader :config, :
|
5
|
+
attr_reader :config, :dumped_tables, :copy_commands
|
6
6
|
|
7
|
-
def initialize(config,
|
7
|
+
def initialize(config, dumped_tables, copy_commands)
|
8
8
|
@config = config
|
9
|
-
@
|
9
|
+
@dumped_tables = dumped_tables
|
10
|
+
@copy_commands = copy_commands
|
10
11
|
end
|
11
12
|
|
12
13
|
def dump
|
13
|
-
config.log('save sql commands to local machine')
|
14
|
-
save_commands_sql_to_tmp_file
|
15
|
-
|
16
14
|
with_ssh do |ssh|
|
17
|
-
upload_commands_sql_to_remote_machine(ssh)
|
18
|
-
|
19
15
|
dump_schema(ssh)
|
20
16
|
dump_data(ssh)
|
21
17
|
|
@@ -33,23 +29,17 @@ module DbDumper
|
|
33
29
|
yield(ssh_agent)
|
34
30
|
end
|
35
31
|
|
36
|
-
def save_commands_sql_to_tmp_file
|
37
|
-
File.open(local_commands_sql_file_path, 'w') do |file|
|
38
|
-
file.write(copy_commands_sql)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def upload_commands_sql_to_remote_machine(ssh)
|
43
|
-
ssh.upload!(local_commands_sql_file_path, remote_commands_sql_file_path)
|
44
|
-
end
|
45
|
-
|
46
32
|
def dump_schema(ssh)
|
47
33
|
ssh.exec!(db_utils.dump_schema_command(remote_machine_schema_file_path))
|
48
34
|
end
|
49
35
|
|
50
36
|
def dump_data(ssh)
|
51
37
|
ssh.exec!("mkdir -p #{remote_machine_data_path}")
|
52
|
-
|
38
|
+
|
39
|
+
ssh.exec!(db_utils.dump_table_data_command(dumped_tables, remote_machine_tables_data_file_path))
|
40
|
+
copy_commands.each do |copy_command|
|
41
|
+
ssh.exec!(db_utils.dump_data_command(copy_command))
|
42
|
+
end
|
53
43
|
end
|
54
44
|
|
55
45
|
def download_schema(ssh)
|
@@ -58,14 +48,13 @@ module DbDumper
|
|
58
48
|
|
59
49
|
def download_data(ssh)
|
60
50
|
ssh.download!(remote_machine_data_path, dest_path, recursive: true)
|
51
|
+
ssh.download!(remote_machine_tables_data_file_path, dest_path)
|
61
52
|
end
|
62
53
|
|
63
54
|
def clean(ssh)
|
64
|
-
ssh.exec! "rm #{remote_commands_sql_file_path}"
|
65
55
|
ssh.exec! "rm #{remote_machine_schema_file_path}"
|
56
|
+
ssh.exec! "rm #{remote_machine_tables_data_file_path}"
|
66
57
|
ssh.exec! "rm -rf #{remote_machine_data_path}"
|
67
|
-
|
68
|
-
File.delete(local_commands_sql_file_path)
|
69
58
|
end
|
70
59
|
|
71
60
|
def db_utils
|
@@ -76,12 +65,8 @@ module DbDumper
|
|
76
65
|
"#{remote_machine_dest_path}/#{dump_schema_fname}"
|
77
66
|
end
|
78
67
|
|
79
|
-
def
|
80
|
-
"#{remote_machine_dest_path}/#{
|
81
|
-
end
|
82
|
-
|
83
|
-
def local_commands_sql_file_path
|
84
|
-
"#{dest_path}/#{commands_sql_fname}"
|
68
|
+
def remote_machine_tables_data_file_path
|
69
|
+
"#{remote_machine_dest_path}/#{dump_data_fname}"
|
85
70
|
end
|
86
71
|
|
87
72
|
def remote_machine_data_path
|
@@ -96,12 +81,12 @@ module DbDumper
|
|
96
81
|
config.local_machine.dest_path
|
97
82
|
end
|
98
83
|
|
99
|
-
def commands_sql_fname
|
100
|
-
@commands_sql_fname ||= "#{Digest::MD5.hexdigest(copy_commands_sql)}.sql"
|
101
|
-
end
|
102
|
-
|
103
84
|
def dump_schema_fname
|
104
85
|
'schema_dump.sql'
|
105
86
|
end
|
87
|
+
|
88
|
+
def dump_data_fname
|
89
|
+
'data_dump.sql'
|
90
|
+
end
|
106
91
|
end
|
107
92
|
end
|
data/lib/db_dumper/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_dumper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey Lukyanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -138,7 +138,7 @@ licenses:
|
|
138
138
|
metadata:
|
139
139
|
bug_tracker_uri: https://github.com/alukyanov/db_dumper/issues
|
140
140
|
changelog_uri: https://github.com/alukyanov/db_dumper/CHANGELOG.md
|
141
|
-
documentation_uri: http://www.rubydoc.info/gems/db_dumper/0.5.
|
141
|
+
documentation_uri: http://www.rubydoc.info/gems/db_dumper/0.5.6
|
142
142
|
homepage_uri: https://github.com/alukyanov/db_dumper
|
143
143
|
source_code_uri: https://github.com/alukyanov/db_dumper
|
144
144
|
wiki_uri: https://github.com/alukyanov/db_dumper/wiki
|