From f520342244e0b3b2af96a88aa6ea1f55ae2de761 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Fri, 18 Dec 2015 11:20:27 +1100 Subject: [PATCH] prevent the importer overwriting primary key fields --- .../modules/BSE/Importer/Target/Article.pm | 15 +++++- .../modules/BSE/Importer/Target/Product.pm | 15 +++++- t/130-importer/030-product.t | 51 ++++++++++++++++++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/site/cgi-bin/modules/BSE/Importer/Target/Article.pm b/site/cgi-bin/modules/BSE/Importer/Target/Article.pm index f68781f8..edf59ce3 100644 --- a/site/cgi-bin/modules/BSE/Importer/Target/Article.pm +++ b/site/cgi-bin/modules/BSE/Importer/Target/Article.pm @@ -6,7 +6,7 @@ use BSE::TB::Articles; use BSE::TB::Products; use BSE::TB::OtherParents; -our $VERSION = "1.012"; +our $VERSION = "1.013"; =head1 NAME @@ -213,6 +213,8 @@ sub row { $leaf = $self->find_leaf($leaf_id, $importer); } if ($leaf) { + # make sure id, articleId etc aren't overwritten + delete @$entry{$self->primary_key_fields}; @{$leaf}{keys %$entry} = values %$entry; $leaf->mark_modified(actor => $importer->actor); $leaf->save; @@ -613,6 +615,17 @@ sub validate_make_leaf { } } +=item primary_key_fields + +Fields we can't modify (or initialize) since the database generates +them. + +=cut + +sub primary_key_fields { + qw(id); +} + 1; =back diff --git a/site/cgi-bin/modules/BSE/Importer/Target/Product.pm b/site/cgi-bin/modules/BSE/Importer/Target/Product.pm index 59aaf0b6..f3640ae0 100644 --- a/site/cgi-bin/modules/BSE/Importer/Target/Product.pm +++ b/site/cgi-bin/modules/BSE/Importer/Target/Product.pm @@ -8,7 +8,7 @@ use BSE::TB::ProductOptions; use BSE::TB::ProductOptionValues; use BSE::TB::PriceTiers; -our $VERSION = "1.009"; +our $VERSION = "1.010"; =head1 NAME @@ -338,6 +338,19 @@ sub validate_make_leaf { } } +=item primary_key_fields + +Fields we can't modify (or initialize) since the database (or database +interface) generates them. + +=cut + +sub primary_key_fields { + my ($class) = @_; + + return ( $class->SUPER::primary_key_fields(), "articleId" ); +} + 1; =back diff --git a/t/130-importer/030-product.t b/t/130-importer/030-product.t index 16b250ac..99ae3364 100644 --- a/t/130-importer/030-product.t +++ b/t/130-importer/030-product.t @@ -10,8 +10,6 @@ BEGIN { or plan skip_all => "Text::CSV not available"; } -plan tests => 11; - BEGIN { unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules"); } @@ -49,6 +47,17 @@ skiplines=0 source=CSV target=Product +[import profile avoidcorrupt$when] +map_id=1 +map_articleId=2 +map_title=3 +map_product_code=4 +source=CSV +target=Product +sep_char=\\t +codes=1 +code_field=product_code +update_only=1 CFG { @@ -125,3 +134,41 @@ EOS $testa->remove($cfg) if $testa; } } + +{ + my $code = "D$when"; + my $testa = bse_make_product + ( + cfg => $cfg, + title => "test updates", + linkAlias => "P$when", + retailPrice => 500, + product_code => $code, + ); + diag "Product id ".$testa->id; + { + my $fh = File::Temp->new; + my $filename = $fh->filename; + + print $fh <new(cfg => $cfg, profile => "avoidcorrupt$when", + callback => sub { note @_ }); + $imp->process($filename); + my $testb = BSE::TB::Products->getByPkey($testa->id); + ok($testb, "managed to fully load the product"); + is($testb->title, "Testing", "check the title is updated"); + my $top = BSE::TB::Articles->getByPkey(1); + isnt($top->generator, "BSE::Generate::Product", + "make sure we didn't scribble over article 1"); + } + + END { + $testa->remove($cfg) if $testa; + } +} + +done_testing(); -- 2.30.2