Skip to content

Commit

Permalink
fix skipping of unless nil
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreh committed Jul 9, 2013
1 parent 8f55b71 commit d870580
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
15 changes: 13 additions & 2 deletions lib/my_obfuscate/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ class Postgres
# We wrap it in an array to keep it consistent with MySql bulk
# obfuscation (multiple rows per insert statement)
def rows_to_be_inserted(line)
[line.split(/\t/)]
line = line[0..-2]
row = line.split(/\t/)

row.collect! do |value|
if value == "\\N"
nil
else
value
end
end

[row]
end

def parse_copy_statement(line)
Expand All @@ -28,7 +39,7 @@ def make_insert_statement(table_name, column_names, values)

def make_valid_value_string(value)
if value.nil?
"\N"
"\\N"
else
value
end
Expand Down
45 changes: 25 additions & 20 deletions spec/my_obfuscate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@
describe "when using Postgres" do
let(:dump) do
StringIO.new(<<-SQL)
COPY some_table (id, email, name, something, age) FROM stdin;
1 hello monkey moose 14
\.
COPY single_column_table (id) FROM stdin;
1
2
\.
COPY another_table (a, b, c, d) FROM stdin;
1 2 3 4
1 2 3 4
\.
COPY some_table_to_keep (a, b) FROM stdin;
5 6
\.
COPY some_table (id, email, name, something, age) FROM stdin;
1 hello monkey moose 14
\.
COPY single_column_table (id) FROM stdin;
1
2
\\N
\.
COPY another_table (a, b, c, d) FROM stdin;
1 2 3 4
1 2 3 4
\.
COPY some_table_to_keep (a, b) FROM stdin;
5 6
\.
SQL
end

Expand All @@ -53,10 +54,10 @@
:some_table => {
:email => {:type => :email, :skip_regexes => [/^[\w\.\_]+@honk\.com$/i, /^[email protected]$/]},
:name => {:type => :string, :length => 8, :chars => MyObfuscate::USERNAME_CHARS},
:age => {:type => :integer, :between => 10...80}
:age => {:type => :integer, :between => 10...80, :unless => :nil },
},
:single_column_table => {
:id => {:type => :integer, :between => 1...8}
:id => {:type => :integer, :between => 1...8, :unless => :nil}
},
:another_table => :truncate,
:some_table_to_keep => :keep
Expand Down Expand Up @@ -86,11 +87,15 @@
output_string.should match(/1\t.*\t\S{8}\tmoose\t\d{2}\n/)
end

it "can skip nils" do
output_string.should match(/\d\n\d\n\\N/)
end

it "is able to keep tables" do
output_string.should include("5\t6")
end

context "when dump contains statement" do
context "when dump contains INSERT statement" do
let(:dump) do
StringIO.new(<<-SQL)
INSERT INTO some_table (email, name, something, age) VALUES ('','', '', 25);
Expand Down

0 comments on commit d870580

Please sign in to comment.