data_keeper 0.1.9 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/data_keeper/database_config.rb +43 -5
- data/lib/data_keeper/dumper.rb +12 -12
- data/lib/data_keeper/loader.rb +36 -35
- data/lib/data_keeper/version.rb +1 -1
- data/lib/data_keeper.rb +9 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ea02e8856f7dbdcc74cd8c82a57893d792a6a2aeb5c6963258d06b8707e9a2b
|
4
|
+
data.tar.gz: a6dbcdc1d8266d8a554ab6b7b1650dad9b810b5f91f8be3da1f157a877e537e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb567b6fd35e1c68f051ee3b06e712dd9ed17e9895362ac7f025940b74cff0973255b51d95cd1effb91a67a7bd92188e50a30a07bc77e1f354008fbeab9f1d39
|
7
|
+
data.tar.gz: eab10951b2cf77197c6d360d2676726ea01ddfc9c6696647e96806724d7cdaf370e33f046fbc2620ee1753b7ad697200e28d932ec45bb550bbd19a655f9b5f1b
|
data/README.md
CHANGED
@@ -110,6 +110,29 @@ in your current database. It will give you an error if you try to run this in a
|
|
110
110
|
Note when using raw sql, your statement is expected to return all columns for the configured table, in the default
|
111
111
|
order (`select *`). This uses pg's COPY from/to for the full table internally.
|
112
112
|
|
113
|
+
## Docker
|
114
|
+
|
115
|
+
If you're using pg under docker, you can configure also DataKeeper with the docker pg settings and then this gem
|
116
|
+
will use the binaries under that docker container (`psql`, `pg_restore`, etc.).
|
117
|
+
|
118
|
+
Since then the commands to execute are ran from within the docker instance, the port will be different
|
119
|
+
as the one the rails app uses, so you'll need to configure what's the pg access from the docker container, use:
|
120
|
+
|
121
|
+
```
|
122
|
+
DataKeeper.docker_config = {
|
123
|
+
instance_name: "pg_my_app",
|
124
|
+
pg_host: "localhost",
|
125
|
+
pg_port: "5432",
|
126
|
+
pg_user: "myapp",
|
127
|
+
pg_password: "myapp"
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
If the host, or user and password are the same as the ones used from the rails app, you can ignore them.
|
132
|
+
If you configure this `docker_config` hash, then data keeper will try to use docker via `docker exec` to
|
133
|
+
run the pg commands.
|
134
|
+
|
135
|
+
|
113
136
|
## Development
|
114
137
|
|
115
138
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -5,13 +5,41 @@ module DataKeeper
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def psql_env
|
8
|
-
env = { 'PGUSER' =>
|
9
|
-
env['PGPASSWORD'] =
|
8
|
+
env = { 'PGUSER' => username }
|
9
|
+
env['PGPASSWORD'] = password if password
|
10
10
|
env
|
11
11
|
end
|
12
12
|
|
13
|
+
def dumper_psql_env
|
14
|
+
env = { 'PGUSER' => server_username }
|
15
|
+
env['PGPASSWORD'] = server_password if server_password
|
16
|
+
env
|
17
|
+
end
|
18
|
+
|
19
|
+
def docker_env_params
|
20
|
+
psql_env.map do |k, v|
|
21
|
+
"-e #{k}=#{v}"
|
22
|
+
end.join(" ")
|
23
|
+
end
|
24
|
+
|
25
|
+
def server_username
|
26
|
+
database_connection_config['username']
|
27
|
+
end
|
28
|
+
|
29
|
+
def server_password
|
30
|
+
database_connection_config['password']
|
31
|
+
end
|
32
|
+
|
33
|
+
def username
|
34
|
+
DataKeeper.docker_config[:pg_user] || database_connection_config['username']
|
35
|
+
end
|
36
|
+
|
37
|
+
def password
|
38
|
+
DataKeeper.docker_config[:pg_password] || database_connection_config['password']
|
39
|
+
end
|
40
|
+
|
13
41
|
def host
|
14
|
-
database_connection_config['host'] || '127.0.0.1'
|
42
|
+
DataKeeper.docker_config[:pg_host] || database_connection_config['host'] || '127.0.0.1'
|
15
43
|
end
|
16
44
|
|
17
45
|
def database
|
@@ -19,12 +47,22 @@ module DataKeeper
|
|
19
47
|
end
|
20
48
|
|
21
49
|
def port
|
22
|
-
database_connection_config['port']
|
50
|
+
DataKeeper.docker_config[:pg_port] || database_connection_config['port'] || '5432'
|
51
|
+
end
|
52
|
+
|
53
|
+
def server_port
|
54
|
+
database_connection_config['port'] || '5432'
|
23
55
|
end
|
24
56
|
|
25
57
|
def connection_args
|
26
58
|
connection_opts = '--host=:host'
|
27
|
-
connection_opts += ' --port=:port' if
|
59
|
+
connection_opts += ' --port=:port' if port
|
60
|
+
connection_opts
|
61
|
+
end
|
62
|
+
|
63
|
+
def dumper_connection_args
|
64
|
+
connection_opts = '--host=:host'
|
65
|
+
connection_opts += ' --port=:port' if server_port
|
28
66
|
connection_opts
|
29
67
|
end
|
30
68
|
end
|
data/lib/data_keeper/dumper.rb
CHANGED
@@ -24,8 +24,8 @@ module DataKeeper
|
|
24
24
|
Tempfile.create do |file|
|
25
25
|
cmd = Terrapin::CommandLine.new(
|
26
26
|
'pg_dump',
|
27
|
-
"#{
|
28
|
-
environment:
|
27
|
+
"#{dumper_connection_args} -x -Fc :database > :output_path",
|
28
|
+
environment: dumper_psql_env
|
29
29
|
)
|
30
30
|
|
31
31
|
cmd.run(
|
@@ -61,8 +61,8 @@ module DataKeeper
|
|
61
61
|
Tempfile.create do |table_file|
|
62
62
|
cmd = Terrapin::CommandLine.new(
|
63
63
|
'psql',
|
64
|
-
"#{
|
65
|
-
environment:
|
64
|
+
"#{dumper_connection_args} -d :database -c :command > #{table_file.path}",
|
65
|
+
environment: dumper_psql_env
|
66
66
|
)
|
67
67
|
|
68
68
|
cmd.run(
|
@@ -89,8 +89,8 @@ module DataKeeper
|
|
89
89
|
table_args = @definition.full_tables_to_export.map { |table| "-t #{table}" }.join(' ')
|
90
90
|
cmd = Terrapin::CommandLine.new(
|
91
91
|
'pg_dump',
|
92
|
-
"#{
|
93
|
-
environment:
|
92
|
+
"#{dumper_connection_args} -x -Fc :database #{table_args} > :output_path",
|
93
|
+
environment: dumper_psql_env
|
94
94
|
)
|
95
95
|
|
96
96
|
cmd.run(
|
@@ -116,8 +116,8 @@ module DataKeeper
|
|
116
116
|
|
117
117
|
cmd = Terrapin::CommandLine.new(
|
118
118
|
'pg_dump',
|
119
|
-
"#{
|
120
|
-
environment:
|
119
|
+
"#{dumper_connection_args} -x --schema-only -Fc :database > :output_path",
|
120
|
+
environment: dumper_psql_env
|
121
121
|
)
|
122
122
|
|
123
123
|
cmd.run(
|
@@ -144,8 +144,8 @@ module DataKeeper
|
|
144
144
|
sequences_args = all_sequences_to_export.map { |table| "-t #{table}" }.join(' ')
|
145
145
|
cmd = Terrapin::CommandLine.new(
|
146
146
|
'pg_dump',
|
147
|
-
"#{
|
148
|
-
environment:
|
147
|
+
"#{dumper_connection_args} -x -Fc :database #{sequences_args} > :output_path",
|
148
|
+
environment: dumper_psql_env
|
149
149
|
)
|
150
150
|
|
151
151
|
cmd.run(database: database, host: host, port: port, output_path: sequences_dump_file.path)
|
@@ -167,8 +167,8 @@ module DataKeeper
|
|
167
167
|
def all_sequences_to_export
|
168
168
|
cmd = Terrapin::CommandLine.new(
|
169
169
|
'psql',
|
170
|
-
"#{
|
171
|
-
environment:
|
170
|
+
"#{dumper_connection_args} -d :database -c :sql -A -R ',' -t",
|
171
|
+
environment: dumper_psql_env
|
172
172
|
)
|
173
173
|
|
174
174
|
sequences = cmd.run(
|
data/lib/data_keeper/loader.rb
CHANGED
@@ -7,9 +7,9 @@ module DataKeeper
|
|
7
7
|
def initialize(dump, file)
|
8
8
|
@dump = dump
|
9
9
|
@file = file
|
10
|
-
@psql_version =
|
11
|
-
|
12
|
-
|
10
|
+
@psql_version = build_terrapin_command('psql', '--version').run
|
11
|
+
.match(/[0-9]{1,}\.[0-9]{1,}/)
|
12
|
+
.to_s.to_f
|
13
13
|
end
|
14
14
|
|
15
15
|
def load!
|
@@ -35,12 +35,23 @@ module DataKeeper
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
def build_terrapin_command(binary, args, docker_args = args)
|
39
|
+
if DataKeeper.docker_config.any?
|
40
|
+
Terrapin::CommandLine.new(
|
41
|
+
'docker',
|
42
|
+
"exec #{docker_env_params} -i #{DataKeeper.docker_config[:instance_name]} #{binary} #{docker_args}"
|
43
|
+
)
|
44
|
+
else
|
45
|
+
Terrapin::CommandLine.new(
|
46
|
+
binary,
|
47
|
+
args,
|
48
|
+
environment: psql_env
|
49
|
+
)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
38
53
|
def set_ar_internal_metadata!
|
39
|
-
cmd =
|
40
|
-
'psql',
|
41
|
-
"#{connection_args} -d :database -c :sql",
|
42
|
-
environment: psql_env
|
43
|
-
)
|
54
|
+
cmd = build_terrapin_command("psql", "#{connection_args} -d :database -c :sql")
|
44
55
|
|
45
56
|
cmd.run(
|
46
57
|
database: database,
|
@@ -58,12 +69,10 @@ module DataKeeper
|
|
58
69
|
end
|
59
70
|
|
60
71
|
def load_full_database!
|
61
|
-
|
62
|
-
|
63
|
-
pg_restore = Terrapin::CommandLine.new(
|
64
|
-
'pg_restore',
|
72
|
+
pg_restore = build_terrapin_command(
|
73
|
+
"pg_restore",
|
65
74
|
"#{connection_args} -j 4 --no-owner --dbname #{database} #{@file.path}#{log_redirect}",
|
66
|
-
|
75
|
+
"#{connection_args} --no-owner --dbname #{database} < #{@file.path}#{log_redirect}"
|
67
76
|
)
|
68
77
|
|
69
78
|
pg_restore.run(
|
@@ -79,10 +88,10 @@ module DataKeeper
|
|
79
88
|
ensure_schema_compatibility!
|
80
89
|
|
81
90
|
inflate(@file.path) do |schema_path, tables_path, sql_files, sequences_path|
|
82
|
-
pg_restore =
|
83
|
-
|
91
|
+
pg_restore = build_terrapin_command(
|
92
|
+
"pg_restore",
|
84
93
|
"#{connection_args} -j 4 --no-owner -s --dbname :database #{schema_path}#{log_redirect}",
|
85
|
-
|
94
|
+
"#{connection_args} --no-owner -s --dbname :database < #{schema_path}#{log_redirect}"
|
86
95
|
)
|
87
96
|
|
88
97
|
pg_restore.run(
|
@@ -91,10 +100,10 @@ module DataKeeper
|
|
91
100
|
port: port
|
92
101
|
)
|
93
102
|
|
94
|
-
pg_restore =
|
95
|
-
|
103
|
+
pg_restore = build_terrapin_command(
|
104
|
+
"pg_restore",
|
96
105
|
"#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{tables_path}#{log_redirect}",
|
97
|
-
|
106
|
+
"#{connection_args} --data-only --no-owner --disable-triggers --dbname :database < #{tables_path}#{log_redirect}"
|
98
107
|
)
|
99
108
|
|
100
109
|
pg_restore.run(
|
@@ -104,11 +113,7 @@ module DataKeeper
|
|
104
113
|
)
|
105
114
|
|
106
115
|
sql_files.each do |table, csv_path|
|
107
|
-
cmd =
|
108
|
-
'psql',
|
109
|
-
"#{connection_args} -d :database -c :command < :csv_path",
|
110
|
-
environment: psql_env
|
111
|
-
)
|
116
|
+
cmd = build_terrapin_command("psql", "#{connection_args} -d :database -c :command < :csv_path")
|
112
117
|
|
113
118
|
cmd.run(
|
114
119
|
database: database,
|
@@ -119,10 +124,10 @@ module DataKeeper
|
|
119
124
|
)
|
120
125
|
end
|
121
126
|
|
122
|
-
pg_restore =
|
123
|
-
|
127
|
+
pg_restore = build_terrapin_command(
|
128
|
+
"pg_restore",
|
124
129
|
"#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{sequences_path}#{log_redirect}",
|
125
|
-
|
130
|
+
"#{connection_args} --data-only --no-owner --disable-triggers --dbname :database < #{sequences_path}#{log_redirect}"
|
126
131
|
)
|
127
132
|
|
128
133
|
pg_restore.run(
|
@@ -136,11 +141,7 @@ module DataKeeper
|
|
136
141
|
end
|
137
142
|
|
138
143
|
def ensure_schema_compatibility!
|
139
|
-
cmd =
|
140
|
-
'psql',
|
141
|
-
"#{connection_args} -d :database -c :command",
|
142
|
-
environment: psql_env
|
143
|
-
)
|
144
|
+
cmd = build_terrapin_command("psql", "#{connection_args} -d :database -c :command")
|
144
145
|
|
145
146
|
if @psql_version >= 11.0
|
146
147
|
cmd.run(database: database, host: host, port: port, command: "drop schema if exists public")
|
@@ -207,9 +208,9 @@ module DataKeeper
|
|
207
208
|
|
208
209
|
yield(
|
209
210
|
inflated_files.schema_path,
|
210
|
-
|
211
|
-
|
212
|
-
|
211
|
+
inflated_files.tables_path,
|
212
|
+
inflated_files.sql_dumps,
|
213
|
+
inflated_files.sequences_path
|
213
214
|
)
|
214
215
|
end
|
215
216
|
end
|
data/lib/data_keeper/version.rb
CHANGED
data/lib/data_keeper.rb
CHANGED
@@ -17,6 +17,7 @@ module DataKeeper
|
|
17
17
|
|
18
18
|
@dump_definition_builders = {}
|
19
19
|
@storage = nil
|
20
|
+
@docker_config = {}
|
20
21
|
@database_config = -> { Rails.configuration.database_configuration[Rails.env] }
|
21
22
|
|
22
23
|
def self.define_dump(name, type = :partial, &block)
|
@@ -69,6 +70,10 @@ module DataKeeper
|
|
69
70
|
@storage = value
|
70
71
|
end
|
71
72
|
|
73
|
+
def self.docker_config=(value)
|
74
|
+
@docker_config = value
|
75
|
+
end
|
76
|
+
|
72
77
|
def self.database_config=(value)
|
73
78
|
@database_config = value
|
74
79
|
end
|
@@ -77,6 +82,10 @@ module DataKeeper
|
|
77
82
|
@database_config
|
78
83
|
end
|
79
84
|
|
85
|
+
def self.docker_config
|
86
|
+
@docker_config
|
87
|
+
end
|
88
|
+
|
80
89
|
def self.clear_dumps!
|
81
90
|
@dump_definition_builders = {}
|
82
91
|
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
|
4
|
+
version: 0.2.1
|
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: 2022-
|
11
|
+
date: 2022-09-15 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: []
|