data_keeper 0.1.2 → 0.1.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: 246804bd313245cf8d56ab51d8784545efa1985af2303ec3827a7db48a0b8401
4
- data.tar.gz: 1833267238192680890e3f021fc6cfc67a0677567df97517eb65a59680bb38e1
3
+ metadata.gz: 281f71c099696e1d51c88dd57e2cd0edf5f028bbbf4340c4457923e3a17b553a
4
+ data.tar.gz: 1592db229f9407735630803bff1a97bd85ed051ec30b878029952ffcfb272759
5
5
  SHA512:
6
- metadata.gz: fb3182638b4eb1713a6d567051e6c66f90aa0c25c247d8e5e6250525525721a893f74838ed8754b34d3bf957a47d22678e067992157e731bb7b68e74e6bdbb75
7
- data.tar.gz: 26a5cadb9d451db4af9069167b9fb605f6dbbc545e623d5d3eeb42d3360120688d9f6767332e93df2b5cb0fc343e105b4209e899ff2bbd91037330790b8782ab
6
+ metadata.gz: cf10459525a1cd3f210c6ef926b2b6d87a217505c8d8c8c155e99021d7a8837312fbf0038302987a683bf2a66f335d7507071451eff213ce004536ea42d26ca9
7
+ data.tar.gz: 30ff5218bb48851d938b96afba015e9076226b25342171e35ac47b5d78ecc8d8c3308afc2ffd6a94ded725495303da9a65f4394530a63cb193fcf005a90d4952
data/.gitignore CHANGED
@@ -6,5 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /lib/tmp
9
10
 
10
11
  Gemfile.lock
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "minitest", "~> 5.0"
8
+ gem 'pg'
@@ -1,7 +1,7 @@
1
1
  module DataKeeper
2
2
  module DatabaseConfig
3
3
  def database_connection_config
4
- Rails.configuration.database_configuration[Rails.env]
4
+ @database_connection_config ||= DataKeeper.database_config.call
5
5
  end
6
6
 
7
7
  def psql_env
@@ -33,6 +33,7 @@ module DataKeeper
33
33
  @tables = []
34
34
  @raw_sqls = {}
35
35
  @definition_block = definition_block
36
+ @on_after_load_block = nil
36
37
  end
37
38
 
38
39
  def evaluate!
@@ -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!
@@ -17,44 +20,66 @@ module DataKeeper
17
20
  end
18
21
 
19
22
  if @dump.on_after_load_block
23
+ ActiveRecord::Base.establish_connection
20
24
  @dump.on_after_load_block.call
21
25
  end
22
26
  end
23
27
 
24
28
  private
25
29
 
26
- def load_full_database!
27
- pg_restore = Terrapin::CommandLine.new(
28
- 'pg_restore',
29
- "#{connection_args} -j 4 --no-owner --dbname #{database} #{@file.path} 2>/dev/null",
30
+ def log_redirect
31
+ if Terrapin::CommandLine.logger
32
+ ""
33
+ else
34
+ " 2>/dev/null"
35
+ end
36
+ end
37
+
38
+ def set_ar_internal_metadata!
39
+ cmd = Terrapin::CommandLine.new(
40
+ 'psql',
41
+ "#{connection_args} -d :database -c :sql",
30
42
  environment: psql_env
31
43
  )
32
44
 
33
- pg_restore.run(
45
+ cmd.run(
34
46
  database: database,
35
47
  host: host,
36
- port: port
48
+ port: port,
49
+ sql: "DELETE from ar_internal_metadata"
37
50
  )
38
51
 
39
- cmd = Terrapin::CommandLine.new(
40
- 'psql',
41
- "#{connection_args} -d :database -c :sql",
52
+ cmd.run(
53
+ database: database,
54
+ host: host,
55
+ port: port,
56
+ sql: "INSERT into ar_internal_metadata (key, value, created_at, updated_at) VALUES ('environment', 'development', '2020-04-03 12:25:54.094209', '2020-04-03 12:25:54.094209')"
57
+ )
58
+ end
59
+
60
+ def load_full_database!
61
+ ensure_schema_compatibility!
62
+
63
+ pg_restore = Terrapin::CommandLine.new(
64
+ 'pg_restore',
65
+ "#{connection_args} -j 4 --no-owner --dbname #{database} #{@file.path}#{log_redirect}",
42
66
  environment: psql_env
43
67
  )
44
68
 
45
- cmd.run(
69
+ pg_restore.run(
46
70
  database: database,
47
71
  host: host,
48
- port: port,
49
- sql: "UPDATE ar_internal_metadata SET value = 'development'"
72
+ port: port
50
73
  )
74
+
75
+ set_ar_internal_metadata!
51
76
  end
52
77
 
53
78
  def load_partial_database!
54
79
  inflate(@file.path) do |schema_path, tables_path, sql_files|
55
80
  pg_restore = Terrapin::CommandLine.new(
56
81
  'pg_restore',
57
- "#{connection_args} -j 4 --no-owner --dbname :database #{schema_path} 2>/dev/null",
82
+ "#{connection_args} -j 4 --no-owner -s --dbname :database #{schema_path}#{log_redirect}",
58
83
  environment: psql_env
59
84
  )
60
85
 
@@ -66,7 +91,7 @@ module DataKeeper
66
91
 
67
92
  pg_restore = Terrapin::CommandLine.new(
68
93
  'pg_restore',
69
- "#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{tables_path} 2>/dev/null",
94
+ "#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{tables_path}#{log_redirect}",
70
95
  environment: psql_env
71
96
  )
72
97
 
@@ -92,7 +117,21 @@ module DataKeeper
92
117
  )
93
118
  end
94
119
 
95
- Rake::Task['db:environment:set'].invoke
120
+ set_ar_internal_metadata!
121
+ end
122
+ end
123
+
124
+ def ensure_schema_compatibility!
125
+ cmd = Terrapin::CommandLine.new(
126
+ 'psql',
127
+ "#{connection_args} -d :database -c :command",
128
+ environment: psql_env
129
+ )
130
+
131
+ if @psql_version >= 11.0
132
+ cmd.run(database: database, host: host, port: port, command: "drop schema if exists public")
133
+ else
134
+ cmd.run(database: database, host: host, port: port, command: "create schema if not exists public")
96
135
  end
97
136
  end
98
137
 
@@ -1,3 +1,3 @@
1
1
  module DataKeeper
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.6"
3
3
  end
data/lib/data_keeper.rb CHANGED
@@ -17,6 +17,7 @@ module DataKeeper
17
17
 
18
18
  @dump_definition_builders = {}
19
19
  @storage = nil
20
+ @database_config = -> { Rails.configuration.database_configuration[Rails.env] }
20
21
 
21
22
  def self.define_dump(name, type = :partial, &block)
22
23
  @dump_definition_builders[name.to_sym] = DefinitionBuilder.new(type, block)
@@ -59,6 +60,14 @@ module DataKeeper
59
60
  @storage = value
60
61
  end
61
62
 
63
+ def self.database_config=(value)
64
+ @database_config = value
65
+ end
66
+
67
+ def self.database_config
68
+ @database_config
69
+ end
70
+
62
71
  def self.clear_dumps!
63
72
  @dump_definition_builders = {}
64
73
  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.2
4
+ version: 0.1.6
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-23 00:00:00.000000000 Z
11
+ date: 2021-09-20 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