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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 558cf65a63bbc27071a6b680172a07db13bee874e7a34b2dbe8cc4e63b3b41ea
4
- data.tar.gz: 15d9356a1a54c5481a7d6054e8d0f47b98dffbadab7db2a317ef64ed9eaf0e43
3
+ metadata.gz: ed06d69d92559ef72d5d35f59dcf349dc41b77d88cdc6cb5e52463586fb3de31
4
+ data.tar.gz: bdeb2734063ecc60cab6bb3cd763c72c7c518a379383c6baa3b4a3f598ac372b
5
5
  SHA512:
6
- metadata.gz: d65a9bc589e97f3c78d602555ab7c5cad0ba550b58be24228f4832bb8cf12f33953c1730361fe137644382d0a5253fd0ef667ccb80c99b97af6489b4dc3472f1
7
- data.tar.gz: 9a9d98d0e9463fd9fe97633f9349778e9bc5c536f70177e73c792b65a14fe8d4080cf480fd79b949acfa2f6e84f5ed4dbb5caa6e916213633addc9503fc53a5d
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
- dump q('users').where(id: user_id)
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
- dump campaigns_q
35
- dump q('offices').where(campaign_id: campaigns_q.ar)
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/application.yml', &block)
14
+ module_function def dump(config_file_path = 'config/dumper.yml', &block)
15
15
  config = Configuration.new(config_file_path)
16
- sql = QueryBuilder.to_sql(config, &block)
17
- RemoteMachine.new(config, sql).dump
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/application.sample.yml for format details
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.dump_data_options} -f #{dump_data_file_path}")
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.to_sql(config, &block)
9
+ def self.build(config, &block)
10
10
  establish_connection
11
11
 
12
- new(config).yield_self do |instance|
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 dump(query)
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, :copy_commands_sql
5
+ attr_reader :config, :dumped_tables, :copy_commands
6
6
 
7
- def initialize(config, copy_commands_sql)
7
+ def initialize(config, dumped_tables, copy_commands)
8
8
  @config = config
9
- @copy_commands_sql = copy_commands_sql
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
- ssh.exec!(db_utils.dump_data_command(remote_commands_sql_file_path))
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 remote_commands_sql_file_path
80
- "#{remote_machine_dest_path}/#{commands_sql_fname}"
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
@@ -1,3 +1,3 @@
1
1
  module DbDumper
2
- VERSION = '0.5.5'
2
+ VERSION = '0.5.6'
3
3
  end
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.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-28 00:00:00.000000000 Z
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.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