database_sanitizer 0.0.16 → 0.0.17

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
  SHA1:
3
- metadata.gz: 77d9e46bfe3e7f9f5bd05c51eb1925384a0eb69d
4
- data.tar.gz: 7ca1d69283a346ec4faecf93ea7c1e648c1a39ec
3
+ metadata.gz: f7e20ead716dbb042a748667237ab61e735e9ed3
4
+ data.tar.gz: abb7e002cb6798d6ede4d36d8a3a7770d211fd7b
5
5
  SHA512:
6
- metadata.gz: b0acd414b98a2100ec34b58520ac52e11927c8b4ba7981f6ab2ffec1ec85cea8c798d85913c7382a1ad9a17148086a87f3469097f1fe31daa22b9a8a855e77f0
7
- data.tar.gz: 53c1293ab81f599e72688cdf94c15d6846d760c0cb11bd38dc2c91da89ac13d29e60f2d0b8d9adc1a069e1d0fc5a0838d56b104ef5a0a7744660bc37e39e0980
6
+ metadata.gz: b980baff161186f7550d8ba0af19a67e81d71473f8f18f5516f52384db95f05c200f16c6ed9f4e1f952f73b1cf767c4ed89d29137b66823e7a1cd74e9a32fd48
7
+ data.tar.gz: c96d6cda1dbe0b75e33a1cb67d18b4d4b24fafc1c31173e43092c709ef0626cfbe7bb446859959a3272f82d0802de29433088883dfd40b36bb3c68f8facb51b4
@@ -59,23 +59,29 @@ module DatabaseSanitizer
59
59
  end
60
60
 
61
61
  def export opts={}
62
- src = Source.connection
63
- dest = Destination.connection
64
62
  duplicate_schema opts[:schema]
65
- tables = (opts[:tables] || src.tables.collect(&:to_s)) - (opts[:exclude] || [])
63
+ tables = (opts[:tables] || Source.connection.tables.collect(&:to_s)) - (opts[:exclude] || [])
66
64
  transformers = read_comments tables
67
65
  max_tbl_name_len = transformers.keys.map(&:length).sort.last || 0
68
66
 
69
67
  tables.with_progress('Exporting').each do |table|
70
- q_table = dest.quote_table_name table
71
- s_table = table.to_sym
72
- order_column = order_column_for s_table
73
- last_value = nil
68
+ export_table table, transformers, max_tbl_name_len
69
+ end
70
+
71
+ update_sequences
72
+ end
73
+
74
+ def export_table table, transformers, max_tbl_name_len
75
+ q_table = Destination.connection.quote_table_name table
76
+ s_table = table.to_sym
77
+ order_column = order_column_for s_table
78
+ last_value = nil
74
79
 
75
- get_chunks(src, table).times_with_progress(table.rjust max_tbl_name_len) do |chunk_i|
80
+ suspend_triggers table do
81
+ get_chunks(Source.connection, table).times_with_progress(table.rjust max_tbl_name_len) do |chunk_i|
76
82
  offset = chunk_i * CHUNK_SIZE
77
- result = src.exec_query select_query q_table, order_column, last_value, offset
78
- dest.execute insert_query q_table, s_table, transformers, result, offset
83
+ result = Source.connection.exec_query select_query q_table, order_column, last_value, offset
84
+ Destination.connection.execute insert_query q_table, s_table, transformers, result, offset
79
85
 
80
86
  last_value = result.last[order_column] if result.any?
81
87
  end
@@ -117,5 +123,28 @@ module DatabaseSanitizer
117
123
  nil
118
124
  end
119
125
  end
126
+
127
+ def suspend_triggers table
128
+ Destination.connection.execute "ALTER TABLE #{table} DISABLE TRIGGER ALL"
129
+ yield
130
+ Destination.connection.execute "ALTER TABLE #{table} ENABLE TRIGGER ALL"
131
+ end
132
+
133
+ def update_sequences
134
+ sequences = Source.connection.exec_query <<-SQL.strip_heredoc
135
+ SELECT s.relname, a.attname, t.relname
136
+ FROM pg_class s
137
+ JOIN pg_depend d ON d.objid = s.oid
138
+ JOIN pg_class t ON d.objid = s.oid AND d.refobjid = t.oid
139
+ JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
140
+ JOIN pg_namespace n ON n.oid = s.relnamespace
141
+ WHERE s.relkind = 'S'
142
+ AND n.nspname = 'public'
143
+ SQL
144
+
145
+ sequences.rows.each do |row|
146
+ Destination.connection.execute "SELECT setval('#{row[0]}', (SELECT MAX(#{row[1]}) FROM #{row[2]}))"
147
+ end
148
+ end
120
149
  end
121
150
  end
@@ -1,6 +1,6 @@
1
1
  module DatabaseSanitizer
2
2
  Transformers = {
3
- 'email' => ->(i, rec) { "email#{i.to_s.rjust(5, ?0)}@#{rec.split(?@)[1]}"},
3
+ 'email' => ->(i, rec) { rec.nil? ? rec : "email#{i.to_s.rjust(5, ?0)}@#{rec.split(?@)[1]}" },
4
4
  'wipe' => proc { nil },
5
5
  'zero' => proc { 0 },
6
6
  'empty_string' => proc { '' },
@@ -1,3 +1,3 @@
1
1
  module DatabaseSanitizer
2
- VERSION = '0.0.16'
2
+ VERSION = '0.0.17'
3
3
  end
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.16
4
+ version: 0.0.17
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-08-27 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler