data_keeper 0.1.6 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 281f71c099696e1d51c88dd57e2cd0edf5f028bbbf4340c4457923e3a17b553a
4
- data.tar.gz: 1592db229f9407735630803bff1a97bd85ed051ec30b878029952ffcfb272759
3
+ metadata.gz: 8aefdb5a66266a15337e2f65a59595a3017c744c6532d97d8c4377eb64f82d43
4
+ data.tar.gz: 49e39de5ecc7fbdf4355bf25c6d5300b5027c1413450aee696a51683d930065e
5
5
  SHA512:
6
- metadata.gz: cf10459525a1cd3f210c6ef926b2b6d87a217505c8d8c8c155e99021d7a8837312fbf0038302987a683bf2a66f335d7507071451eff213ce004536ea42d26ca9
7
- data.tar.gz: 30ff5218bb48851d938b96afba015e9076226b25342171e35ac47b5d78ecc8d8c3308afc2ffd6a94ded725495303da9a65f4394530a63cb193fcf005a90d4952
6
+ metadata.gz: d91d76636fd6eadab472f6442cca9484b95130209a8ba2adef6197c600ab31e43b054121991f6e96ef7200cc4a4ba4f23215ccdf5bf8d6a599d5495786832b51
7
+ data.tar.gz: d13e22eb926add303417d44fe1ac802b69b3839eb8349903fb10059139bd583e0d91f9e4aaa3a0e9f67ad2782d8aec4628290f48e95b92b64c1facfe1ac70a8f
@@ -37,9 +37,12 @@ module DataKeeper
37
37
  end
38
38
 
39
39
  def evaluate!
40
- instance_eval(&@definition_block) if @definition_block
40
+ @definition ||=
41
+ begin
42
+ instance_eval(&@definition_block) if @definition_block
41
43
 
42
- Definition.new(@type, @tables, @raw_sqls, @on_after_load_block)
44
+ Definition.new(@type, @tables, @raw_sqls, @on_after_load_block)
45
+ end
43
46
  end
44
47
 
45
48
  def table(name)
@@ -48,6 +48,7 @@ module DataKeeper
48
48
  dump_schema(tar)
49
49
  dump_partial_tables(tar)
50
50
  dump_sqls(tar)
51
+ dump_sequences(tar)
51
52
  end
52
53
  end
53
54
 
@@ -136,8 +137,47 @@ module DataKeeper
136
137
  end
137
138
  end
138
139
 
140
+ def dump_sequences(tar)
141
+ Tempfile.create do |sequences_dump_file|
142
+ sequences_dump_file.binmode
143
+
144
+ sequences_args = all_sequences_to_export.map { |table| "-t #{table}" }.join(' ')
145
+ cmd = Terrapin::CommandLine.new(
146
+ 'pg_dump',
147
+ "#{connection_args} -x -Fc :database #{sequences_args} > :output_path",
148
+ environment: psql_env
149
+ )
150
+
151
+ cmd.run(database: database, host: host, port: port, output_path: sequences_dump_file.path)
152
+
153
+ tar.add_file_simple("sequences.dump", 0644, File.size(sequences_dump_file.path)) do |io|
154
+ sequences_dump_file.reopen(sequences_dump_file)
155
+
156
+ while !sequences_dump_file.eof?
157
+ io.write(sequences_dump_file.read(2048))
158
+ end
159
+ end
160
+ end
161
+ end
162
+
139
163
  def filename
140
164
  "#{@dump_name}-#{Time.now.strftime("%Y%m%d-%H%M")}"
141
165
  end
166
+
167
+ def all_sequences_to_export
168
+ cmd = Terrapin::CommandLine.new(
169
+ 'psql',
170
+ "#{connection_args} -d :database -c :sql -A -R ',' -t",
171
+ environment: psql_env
172
+ )
173
+
174
+ sequences = cmd.run(
175
+ database: database,
176
+ host: host,
177
+ port: port,
178
+ sql: "SELECT sequencename from pg_sequences;"
179
+ )
180
+ sequences.split(',').map{|x| x.strip}
181
+ end
142
182
  end
143
183
  end
@@ -76,7 +76,9 @@ module DataKeeper
76
76
  end
77
77
 
78
78
  def load_partial_database!
79
- inflate(@file.path) do |schema_path, tables_path, sql_files|
79
+ ensure_schema_compatibility!
80
+
81
+ inflate(@file.path) do |schema_path, tables_path, sql_files, sequences_path|
80
82
  pg_restore = Terrapin::CommandLine.new(
81
83
  'pg_restore',
82
84
  "#{connection_args} -j 4 --no-owner -s --dbname :database #{schema_path}#{log_redirect}",
@@ -117,6 +119,18 @@ module DataKeeper
117
119
  )
118
120
  end
119
121
 
122
+ pg_restore = Terrapin::CommandLine.new(
123
+ 'pg_restore',
124
+ "#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{sequences_path}#{log_redirect}",
125
+ environment: psql_env
126
+ )
127
+
128
+ pg_restore.run(
129
+ database: database,
130
+ host: host,
131
+ port: port
132
+ )
133
+
120
134
  set_ar_internal_metadata!
121
135
  end
122
136
  end
@@ -149,9 +163,8 @@ module DataKeeper
149
163
 
150
164
  validate("Schema file is missing") { !!schema_path } &&
151
165
  validate("Tables file is missing") { !!tables_path } &&
152
- validate("Not all sql custom dumps are present") do
153
- sql_dumps.size == @dump.sqls.keys.size
154
- end
166
+ validate("Not all sql custom dumps are present") { sql_dumps.size == @dump.sqls.keys.size } &&
167
+ validate("Sequences file is missing") { !!sequences_path }
155
168
  end
156
169
 
157
170
  def schema_path
@@ -162,6 +175,10 @@ module DataKeeper
162
175
  @tables_path ||= @paths.find { |x| File.basename(x) == "tables.dump" }
163
176
  end
164
177
 
178
+ def sequences_path
179
+ @sequences_path ||= @paths.find { |x| File.basename(x) == "sequences.dump" }
180
+ end
181
+
165
182
  def sql_dumps
166
183
  @sql_dumps ||= @dump.sqls.map do |name, (table, _proc)|
167
184
  path = @paths.find { |x| File.basename(x) == "#{name}.csv" }
@@ -191,7 +208,8 @@ module DataKeeper
191
208
  yield(
192
209
  inflated_files.schema_path,
193
210
  inflated_files.tables_path,
194
- inflated_files.sql_dumps
211
+ inflated_files.sql_dumps,
212
+ inflated_files.sequences_path
195
213
  )
196
214
  end
197
215
  end
@@ -21,14 +21,15 @@ module DataKeeper
21
21
  def retrieve(dump_name)
22
22
  tempfile = Tempfile.new
23
23
  local_store_dir = @local_store_dir
24
+ last_dump_filename = nil
24
25
 
25
26
  on complete_host do
26
- last_dump = capture :ls, "-1t #{File.join(local_store_dir, dump_name.to_s)} | head -n 1"
27
+ last_dump_filename = capture :ls, "-1t #{File.join(local_store_dir, dump_name.to_s)} | head -n 1"
27
28
 
28
- download! File.join(local_store_dir, dump_name.to_s, last_dump), tempfile.path
29
+ download! File.join(local_store_dir, dump_name.to_s, last_dump_filename), tempfile.path
29
30
  end
30
31
 
31
- yield(tempfile)
32
+ yield(tempfile, last_dump_filename)
32
33
  ensure
33
34
  tempfile.delete
34
35
  end
@@ -78,7 +78,7 @@ module DataKeeper
78
78
  end
79
79
 
80
80
  def retrieve(dump_name)
81
- prefix = "#{@store_dir}#{dump_name.to_s}"
81
+ prefix = "#{@store_dir}#{dump_name.to_s}/"
82
82
  last_dump = s3_client.list_contents(prefix).sort_by(&:last_modified).reverse.first
83
83
 
84
84
  Tempfile.create do |tmp_file|
@@ -86,7 +86,7 @@ module DataKeeper
86
86
  s3_client.stream_to_io(last_dump.key, tmp_file)
87
87
  tmp_file.flush
88
88
 
89
- yield(tmp_file)
89
+ yield(tmp_file, File.basename(last_dump.key))
90
90
  end
91
91
  end
92
92
 
@@ -35,4 +35,15 @@ namespace :data_keeper do
35
35
 
36
36
  DataKeeper.load_dump!(name, path)
37
37
  end
38
+
39
+ desc "Downloads the given dump raw file in the current directory"
40
+ task :download, [:name] => [:environment] do |_t, args|
41
+ name = args[:name]
42
+
43
+ if name.blank? || !DataKeeper.dump?(name)
44
+ raise "Please use this rake task giving a name of a configured dump. Ex: bin/rake data_keeper:pull[full]"
45
+ end
46
+
47
+ DataKeeper.download_dump!(name, ".")
48
+ end
38
49
  end
@@ -1,3 +1,3 @@
1
1
  module DataKeeper
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.9"
3
3
  end
data/lib/data_keeper.rb CHANGED
@@ -44,6 +44,15 @@ module DataKeeper
44
44
  end
45
45
  end
46
46
 
47
+ def self.download_dump!(name, path)
48
+ raise DumpDoesNotExist unless dump?(name)
49
+ raise NoStorageDefined if @storage.nil?
50
+
51
+ @storage.retrieve(name) do |file, filename|
52
+ FileUtils.cp file.path, File.join(path, filename)
53
+ end
54
+ end
55
+
47
56
  def self.load_dump!(name, path)
48
57
  raise DumpDoesNotExist unless File.file?(path)
49
58
  raise NoStorageDefined if @storage.nil?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_keeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Campos
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-20 00:00:00.000000000 Z
11
+ date: 2022-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -100,7 +100,7 @@ licenses:
100
100
  - MIT
101
101
  metadata:
102
102
  homepage_uri: https://github.com/rogercampos/data_keeper
103
- post_install_message:
103
+ post_install_message:
104
104
  rdoc_options: []
105
105
  require_paths:
106
106
  - lib
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubygems_version: 3.1.6
119
- signing_key:
119
+ signing_key:
120
120
  specification_version: 4
121
121
  summary: Easy management of database dumps for dev env
122
122
  test_files: []