data_keeper 0.1.8 → 0.2.0
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 +4 -4
- data/README.md +23 -0
- data/lib/data_keeper/database_config.rb +19 -5
- data/lib/data_keeper/definition.rb +5 -2
- 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: 1f0067051d0aa4c4a42aba134510233f0b8cd9acdbc4fe07a827df4af1fbd48c
|
4
|
+
data.tar.gz: 67bae8fe0fb71d313229da366e47173ff06f0c0ffcf082d4f6743722be66c500
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33aa3ca995eef2f037bb4a2dfdfba5dc207d3265eb9a4ccc0568d592ed1ab3f609bd49ad3bc9732283a52814daa443302e125be1ae4219001d9e14f44e7084bf
|
7
|
+
data.tar.gz: 9a6a28ef62cf97960031c9b73d5d02aa942120692155c340dd7b73539d9df5c3695dd981c13bf15a8a6d04afef730f5c2ce1e4dee4c18851218ff8c81faa4e68
|
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,27 @@ 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 docker_env_params
|
14
|
+
psql_env.map do |k, v|
|
15
|
+
"-e #{k}=#{v}"
|
16
|
+
end.join(" ")
|
17
|
+
end
|
18
|
+
|
19
|
+
def username
|
20
|
+
DataKeeper.docker_config[:pg_user] || database_connection_config['username']
|
21
|
+
end
|
22
|
+
|
23
|
+
def password
|
24
|
+
DataKeeper.docker_config[:pg_password] || database_connection_config['password']
|
25
|
+
end
|
26
|
+
|
13
27
|
def host
|
14
|
-
database_connection_config['host'] || '127.0.0.1'
|
28
|
+
DataKeeper.docker_config[:pg_host] || database_connection_config['host'] || '127.0.0.1'
|
15
29
|
end
|
16
30
|
|
17
31
|
def database
|
@@ -19,12 +33,12 @@ module DataKeeper
|
|
19
33
|
end
|
20
34
|
|
21
35
|
def port
|
22
|
-
database_connection_config['port']
|
36
|
+
DataKeeper.docker_config[:pg_port] || database_connection_config['port'] || '5432'
|
23
37
|
end
|
24
38
|
|
25
39
|
def connection_args
|
26
40
|
connection_opts = '--host=:host'
|
27
|
-
connection_opts += ' --port=:port' if
|
41
|
+
connection_opts += ' --port=:port' if port
|
28
42
|
connection_opts
|
29
43
|
end
|
30
44
|
end
|
@@ -37,9 +37,12 @@ module DataKeeper
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def evaluate!
|
40
|
-
|
40
|
+
@definition ||=
|
41
|
+
begin
|
42
|
+
instance_eval(&@definition_block) if @definition_block
|
41
43
|
|
42
|
-
|
44
|
+
Definition.new(@type, @tables, @raw_sqls, @on_after_load_block)
|
45
|
+
end
|
43
46
|
end
|
44
47
|
|
45
48
|
def table(name)
|
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.
|
4
|
+
version: 0.2.0
|
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:
|
11
|
+
date: 2022-09-14 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: []
|