database_sanitizer 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ae899e9c69e083b9ad8adb9f427a384c898621d
4
- data.tar.gz: 704c9bb2b7ee445b459078edf86fd6198023cd28
3
+ metadata.gz: 1e3c8cc11038ed94c46e29f7b66b1289372bed89
4
+ data.tar.gz: b9c1d829f7635a7ea1a53db9cce6fb04adb0e4c9
5
5
  SHA512:
6
- metadata.gz: 0045968c62ea497b1ba352b60c5eb58c629ce2e1fcaf5480f73afabd68c8a8ba234b7a1e62a16e3563533a9e4d4e8f0fc54446af7efb09c6f0a57b6a62306d09
7
- data.tar.gz: c1ebeac6a04345da7edbef0cffb4beee17276e47d6bdd8add408f1a6ec9a7e4c31beac21a197667c369eac31773857996758e3899a9a87f9891da0260c4f88be
6
+ metadata.gz: 96c5f9a2548522100570d8125520a8997bf6dc7c7b7e36be4c22616a3ac41d8e86e10cf2e365cd18b2f2da5d2f7e1b7884a35972b8764c6f868fab4ebc60da88
7
+ data.tar.gz: 52f4150d8cd04a646d18f5a0fb5aba5df4163769377c0b789fa0e3d2831ad2cae1d1fc1f128744f2b7238708082f8831dd678e603427bda4704421c695da1176
@@ -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
- result = src.exec_query "SELECT * FROM #{table}"
53
- cols = result.columns.join ','
54
- dest.transaction do
55
- result.rows.with_progress(table.rjust max_tbl_name_len).each_with_index do |src_row, row_i|
56
- values = result.columns.each_with_index.map do |col, col_i|
57
- transformer = transformers[table.to_sym][col.to_sym]
58
- dest.quote transformer ? transformer.(row_i, src_row[col_i]) : src_row[col_i]
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
@@ -1,3 +1,3 @@
1
1
  module DatabaseSanitizer
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe DatabaseExporter do
3
+ describe DatabaseSanitizer do
4
4
  describe '#extract_transformer', nodb: true do
5
5
  context 'should return nil for no transformer' do
6
6
  ['no tag comment', nil, '', 'sanitize no tag'].each do |comment|
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ $: << File.expand_path('../lib', __FILE__)
3
3
  require 'yaml'
4
4
  require 'pry'
5
5
 
6
- require 'database_exporter'
6
+ require 'database_sanitizer'
7
7
 
8
8
  DBCONF = YAML::load(IO.read(File.expand_path('../config/database.yml', __FILE__)))
9
9
  ENV['DB'] ||= 'postgres'
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.6
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-06-26 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler