database_sanitizer 0.0.6 → 0.0.7
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/lib/database_sanitizer.rb +23 -9
- data/lib/database_sanitizer/version.rb +1 -1
- data/spec/database_exporter_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e3c8cc11038ed94c46e29f7b66b1289372bed89
|
4
|
+
data.tar.gz: b9c1d829f7635a7ea1a53db9cce6fb04adb0e4c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96c5f9a2548522100570d8125520a8997bf6dc7c7b7e36be4c22616a3ac41d8e86e10cf2e365cd18b2f2da5d2f7e1b7884a35972b8764c6f868fab4ebc60da88
|
7
|
+
data.tar.gz: 52f4150d8cd04a646d18f5a0fb5aba5df4163769377c0b789fa0e3d2831ad2cae1d1fc1f128744f2b7238708082f8831dd678e603427bda4704421c695da1176
|
data/lib/database_sanitizer.rb
CHANGED
@@ -3,6 +3,7 @@ require 'active_record/comments'
|
|
3
3
|
require 'progress'
|
4
4
|
|
5
5
|
module DatabaseSanitizer
|
6
|
+
CHUNK_SIZE = 500
|
6
7
|
class Source < ActiveRecord::Base
|
7
8
|
end
|
8
9
|
end
|
@@ -42,22 +43,35 @@ module DatabaseSanitizer
|
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
46
|
+
def get_chunks table
|
47
|
+
conn = Source.connection
|
48
|
+
query = "SELECT count(*) FROM #{conn.quote_table_name table}"
|
49
|
+
pg_query = "SELECT reltuples FROM pg_class WHERE relname=#{conn.quote table}"
|
50
|
+
res = conn.adapter_name == 'PostgreSQL' ? (conn.exec_query(pg_query) rescue false) : false
|
51
|
+
res ||= conn.exec_query(query)
|
52
|
+
res.rows[0][0].to_i % CHUNK_SIZE + 1
|
53
|
+
end
|
54
|
+
|
45
55
|
def export src, dest, opts={}
|
46
56
|
duplicate_schema opts[:schema]
|
47
57
|
tables = (opts[:tables] || src.tables.collect(&:to_s)) - (opts[:exclude] || [])
|
48
58
|
transformers = read_comments dest, tables
|
49
|
-
|
50
59
|
max_tbl_name_len = transformers.keys.map(&:length).sort.last || 0
|
60
|
+
|
51
61
|
tables.with_progress('Exporting').each do |table|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
result
|
56
|
-
|
57
|
-
|
58
|
-
|
62
|
+
q_table = dest.quote_table_name table
|
63
|
+
query = "SELECT * FROM #{q_table} LIMIT #{CHUNK_SIZE} OFFSET "
|
64
|
+
get_chunks(table).times_with_progress(table.rjust max_tbl_name_len) do |chunk_i|
|
65
|
+
result = src.exec_query query + (chunk_i*CHUNK_SIZE).to_s
|
66
|
+
cols = result.columns.map { |col| dest.quote_column_name col }.join ','
|
67
|
+
dest.transaction do
|
68
|
+
result.rows.with_progress.each_with_index do |src_row, row_i|
|
69
|
+
values = result.columns.each_with_index.map do |col, col_i|
|
70
|
+
transformer = transformers[table.to_sym][col.to_sym]
|
71
|
+
dest.quote transformer ? transformer.(row_i, src_row[col_i]) : src_row[col_i]
|
72
|
+
end
|
73
|
+
dest.insert_sql "INSERT INTO #{q_table} (#{cols}) VALUES (#{values.join ','})"
|
59
74
|
end
|
60
|
-
dest.insert_sql "INSERT INTO #{dest.quote_table_name table} (#{cols}) VALUES (#{values.join ','})"
|
61
75
|
end
|
62
76
|
end
|
63
77
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marton Somogyi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|