data_keeper 0.1.1 → 0.1.5

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: 268e784853f728ad8c4cefd36ca8cc2f09b2892ab8751f1eb0f16dc17bc72900
4
- data.tar.gz: 62ea9b6a3bf57ae952417db4fd149365f56f4baf63f67839bde26d3d2f352464
3
+ metadata.gz: 2fc356b01f1619f2ec815a556092bd13fc28b44b2b201707c6a820da00fcff70
4
+ data.tar.gz: 8f2d4dcc59035b7fee1131daa3cfed6dedb0bf5c635e781d48dc65c7c43a58e7
5
5
  SHA512:
6
- metadata.gz: 8ea7c0182fb8698c7d9c2be973cf7d3def1845222027feb90107adda8d333488ef01318268f8ae1356c2bd45d89419cb908443b5a9037df359cf5fc63376fca7
7
- data.tar.gz: 7110186baac099fe5ce341186b25594c4ad730a811bc4270c4cd8e1d14d3cf4858bce5eb8bed0e621152da6d5729223c4d47c4f74b8e558c9539bb5bfcecd0c1
6
+ metadata.gz: 5102f1e716045e8bc425d69c71b03c8cad46b805d3caf7db12a0eb9ff073f53c1c94cebfd70a9909b8d806f543c66e1ecd7f3c3d3d344a688bacf8bdf6d2f3f5
7
+ data.tar.gz: 0b4459b6441c79cb3768fa769271ccb7661f458cf4f69b75562a4fc92ebfa2e28502bf294102689112b399cf97938c25ee703775444132f39c2a38e6d402bfa4
data/README.md CHANGED
@@ -46,6 +46,9 @@ DataKeeper.storage = DataKeeper::LocalStorage.new(
46
46
  There's also support for storing the dumps in s3, using `DataKeeper::S3Storage` like in this example:
47
47
 
48
48
  ```ruby
49
+ # Explicit require is necessary
50
+ require 'data_keeper/s3_storage'
51
+
49
52
  DataKeeper.storage = DataKeeper::S3Storage.new(
50
53
  bucket: 'bucket-name',
51
54
  store_dir: 'dumps/',
@@ -26,21 +26,19 @@ module DataKeeper
26
26
  end
27
27
 
28
28
  class DefinitionBuilder
29
- attr_reader :tables, :raw_sqls, :on_after_load_block
29
+ def initialize(type, definition_block)
30
+ raise InvalidDumpType, "Invalid type! use :partial or :full" unless [:partial, :full].include?(type)
30
31
 
31
- def initialize(definition_block)
32
+ @type = type
32
33
  @tables = []
33
34
  @raw_sqls = {}
34
- instance_eval(&definition_block) if definition_block
35
+ @definition_block = definition_block
35
36
  end
36
37
 
37
- def self.build(type, block)
38
- @type = type
39
- raise InvalidDumpType, "Invalid type! use :partial or :full" unless [:partial, :full].include?(type)
40
-
41
- builder = new(block)
38
+ def evaluate!
39
+ instance_eval(&@definition_block) if @definition_block
42
40
 
43
- Definition.new(type, builder.tables, builder.raw_sqls, builder.on_after_load_block)
41
+ Definition.new(@type, @tables, @raw_sqls, @on_after_load_block)
44
42
  end
45
43
 
46
44
  def table(name)
@@ -35,7 +35,7 @@ module DataKeeper
35
35
  output_path: file.path
36
36
  )
37
37
 
38
- yield file, "#{filename}.dump"
38
+ yield File.open(file.path), "#{filename}.dump"
39
39
  end
40
40
  end
41
41
 
@@ -51,7 +51,7 @@ module DataKeeper
51
51
  end
52
52
  end
53
53
 
54
- yield file, "#{filename}.tar.gz"
54
+ yield File.open(file.path), "#{filename}.tar.gz"
55
55
  end
56
56
  end
57
57
 
@@ -7,6 +7,9 @@ module DataKeeper
7
7
  def initialize(dump, file)
8
8
  @dump = dump
9
9
  @file = file
10
+ @psql_version = Terrapin::CommandLine.new('psql', '--version').run
11
+ .match(/[0-9]{1,}\.[0-9]{1,}/)
12
+ .to_s.to_f
10
13
  end
11
14
 
12
15
  def load!
@@ -24,6 +27,8 @@ module DataKeeper
24
27
  private
25
28
 
26
29
  def load_full_database!
30
+ ensure_schema_compatibility!
31
+
27
32
  pg_restore = Terrapin::CommandLine.new(
28
33
  'pg_restore',
29
34
  "#{connection_args} -j 4 --no-owner --dbname #{database} #{@file.path} 2>/dev/null",
@@ -52,9 +57,11 @@ module DataKeeper
52
57
 
53
58
  def load_partial_database!
54
59
  inflate(@file.path) do |schema_path, tables_path, sql_files|
60
+ ensure_schema_compatibility!
61
+
55
62
  pg_restore = Terrapin::CommandLine.new(
56
63
  'pg_restore',
57
- "#{connection_args} -j 4 --no-owner --dbname #{database} #{schema_path} 2>/dev/null",
64
+ "#{connection_args} -j 4 --no-owner --dbname :database #{schema_path} 2>/dev/null",
58
65
  environment: psql_env
59
66
  )
60
67
 
@@ -66,7 +73,7 @@ module DataKeeper
66
73
 
67
74
  pg_restore = Terrapin::CommandLine.new(
68
75
  'pg_restore',
69
- "#{connection_args} -c -j 4 --no-owner --dbname #{database} #{tables_path} 2>/dev/null",
76
+ "#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{tables_path} 2>/dev/null",
70
77
  environment: psql_env
71
78
  )
72
79
 
@@ -88,7 +95,7 @@ module DataKeeper
88
95
  host: host,
89
96
  port: port,
90
97
  csv_path: csv_path,
91
- command: "COPY #{table} FROM stdin DELIMITER ',' CSV HEADER"
98
+ command: "ALTER TABLE #{table} DISABLE TRIGGER all; COPY #{table} FROM stdin DELIMITER ',' CSV HEADER"
92
99
  )
93
100
  end
94
101
 
@@ -96,6 +103,20 @@ module DataKeeper
96
103
  end
97
104
  end
98
105
 
106
+ def ensure_schema_compatibility!
107
+ cmd = Terrapin::CommandLine.new(
108
+ 'psql',
109
+ "#{connection_args} -d :database -c :command",
110
+ environment: psql_env
111
+ )
112
+
113
+ if @psql_version >= 11.0
114
+ cmd.run(database: database, host: host, port: port, command: "drop schema if exists public")
115
+ else
116
+ cmd.run(database: database, host: host, port: port, command: "create schema if not exists public")
117
+ end
118
+ end
119
+
99
120
  class InflatedFiles
100
121
  attr_reader :errors
101
122
 
@@ -1,3 +1,3 @@
1
1
  module DataKeeper
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/data_keeper.rb CHANGED
@@ -15,18 +15,20 @@ module DataKeeper
15
15
  DumpDoesNotExist = Class.new(Error)
16
16
  NoStorageDefined = Class.new(Error)
17
17
 
18
- @dumps = {}
18
+ @dump_definition_builders = {}
19
19
  @storage = nil
20
20
 
21
21
  def self.define_dump(name, type = :partial, &block)
22
- @dumps[name.to_sym] = DefinitionBuilder.build(type, block)
22
+ @dump_definition_builders[name.to_sym] = DefinitionBuilder.new(type, block)
23
23
  end
24
24
 
25
25
  def self.create_dump!(name)
26
26
  raise DumpDoesNotExist unless dump?(name)
27
27
  raise NoStorageDefined if @storage.nil?
28
28
 
29
- Dumper.new(name, @dumps[name.to_sym]).run! do |file, filename|
29
+ definition = @dump_definition_builders[name.to_sym].evaluate!
30
+
31
+ Dumper.new(name, definition).run! do |file, filename|
30
32
  @storage.save(file, filename, name)
31
33
  end
32
34
  end
@@ -34,9 +36,10 @@ module DataKeeper
34
36
  def self.fetch_and_load_dump!(name)
35
37
  raise DumpDoesNotExist unless dump?(name)
36
38
  raise NoStorageDefined if @storage.nil?
39
+ definition = @dump_definition_builders[name.to_sym].evaluate!
37
40
 
38
41
  @storage.retrieve(name) do |file|
39
- Loader.new(@dumps[name.to_sym], file).load!
42
+ Loader.new(definition, file).load!
40
43
  end
41
44
  end
42
45
 
@@ -44,11 +47,12 @@ module DataKeeper
44
47
  raise DumpDoesNotExist unless File.file?(path)
45
48
  raise NoStorageDefined if @storage.nil?
46
49
 
47
- Loader.new(@dumps[name.to_sym], File.open(path)).load!
50
+ definition = @dump_definition_builders[name.to_sym].evaluate!
51
+ Loader.new(definition, File.open(path)).load!
48
52
  end
49
53
 
50
54
  def self.dump?(name)
51
- @dumps.key?(name.to_sym)
55
+ @dump_definition_builders.key?(name.to_sym)
52
56
  end
53
57
 
54
58
  def self.storage=(value)
@@ -56,6 +60,6 @@ module DataKeeper
56
60
  end
57
61
 
58
62
  def self.clear_dumps!
59
- @dumps = {}
63
+ @dump_definition_builders = {}
60
64
  end
61
65
  end
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.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Campos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-16 00:00:00.000000000 Z
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubygems_version: 3.0.3
118
+ rubygems_version: 3.1.6
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: Easy management of database dumps for dev env