From 9d2b810a05bcb19a45ce803c19ee2c699b86b49f Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sun, 4 May 2008 04:32:58 +0000 Subject: [PATCH] conversion of Product from Wobjects to Skus with variants is done --- docs/upgrades/upgrade_7.5.10-7.5.11.pl | 28 +++++--------------------- lib/WebGUI/Asset/Sku/Product.pm | 9 +++++++-- t/Asset/Sku/ProductCollateral.t | 22 ++++++++++++++++---- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/docs/upgrades/upgrade_7.5.10-7.5.11.pl b/docs/upgrades/upgrade_7.5.10-7.5.11.pl index 2cf42dabe..75ac06043 100644 --- a/docs/upgrades/upgrade_7.5.10-7.5.11.pl +++ b/docs/upgrades/upgrade_7.5.10-7.5.11.pl @@ -636,21 +636,6 @@ sub migrateOldProduct { print "\tMigrate old Product to new SKU based Products.\n" unless ($quiet); # and here's our code ##Grab data from Wobject table, and move it into Sku and Product, as appropriate. - print "\t\tAdding new product variants table.\n" unless ($quiet); - $session->db->write(<<'EOSQL'); -CREATE TABLE Product_variants ( - variantId VARCHAR(22) BINARY NOT NULL PRIMARY KEY, - varSku VARCHAR(255) BINARY NOT NULL UNIQUE, - assetId VARCHAR(22) BINARY NOT NULL, - mastersku VARCHAR(22) BINARY NOT NULL, - varTitle VARCHAR(255) BINARY NOT NULL, - shortdesc VARCHAR(30), - price FLOAT, - weight FLOAT, - quantity INT, - sequenceNumber INT -); -EOSQL ##Have to change the className's in the db, too ## Wobject description -> Sku description ## Wobject displayTitle -> Sku displayTitle @@ -674,6 +659,8 @@ EOSQL $rmWobject->finish; $session->db->write(q!update asset set className='WebGUI::Asset::Sku::Product' where className='WebGUI::Asset::Wobject::Product'!); + ## Add variants collateral column to Sku/Product + $session->db->write('alter table Product add column variantsJSON mediumtext'); ##Build a variant for each Product. my $productQuery = $session->db->read(<hashRef()) { - my $productData; - while ( () ) { + while (my $productData = $productQuery->hashRef()) { ##Truncate title to 30 chars for short desc printf "Adding variant to %s\n", $productData->{title} unless $quiet; my $product = WebGUI::Asset::Sku::Product->new($session, $productData->{assetId}, 'WebGUI::Asset::Sku::Product', $productData->{revisionDate}); - $product->setCollateral('Product_variants', 'varSku', { + $product->setCollateral('variantsJSON', 'new', { varSku => ($productData->{productNumber} || $session->id->generate), - mastersku => $product->get('sku'), shortdesc => substr($productData->{title}, 0, 30), price => $productData->{price}, weight => 0, @@ -705,8 +689,6 @@ EOSQL1 $session->db->write('alter table Product drop column productNumber'); ## Remove price from Product since prices are now stored in variants $session->db->write('alter table Product drop column price'); - ## Add variants collateral column - $session->db->write('alter table Product add column variantsJSON mediumtext'); ## Update config file, deleting Wobject::Product and adding Sku::Product $session->config->deleteFromArray('assets', 'WebGUI::Asset::Wobject::Product'); diff --git a/lib/WebGUI/Asset/Sku/Product.pm b/lib/WebGUI/Asset/Sku/Product.pm index 71e423504..207624143 100644 --- a/lib/WebGUI/Asset/Sku/Product.pm +++ b/lib/WebGUI/Asset/Sku/Product.pm @@ -225,8 +225,13 @@ sub getAllCollateral { my $tableName = shift; return $self->{_collateral}->{$tableName} if exists $self->{_collateral}->{$tableName}; my $json = $self->get($tableName); - return [] unless $json; - my $table = from_json($json); + my $table; + if ($json) { + $table = from_json($json); + } + else { + $table = []; + } $self->{_collateral}->{$tableName} = $table; return $table; } diff --git a/t/Asset/Sku/ProductCollateral.t b/t/Asset/Sku/ProductCollateral.t index d54ce439b..bca52a8b5 100644 --- a/t/Asset/Sku/ProductCollateral.t +++ b/t/Asset/Sku/ProductCollateral.t @@ -35,15 +35,14 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 23; # Increment this number for each test you create +plan tests => 24; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here my $root = WebGUI::Asset->getRoot($session); my $product = $root->addChild({ - className =>"WebGUI::Asset::Sku::Product", - title =>"Test Donation", - price => 44.44, + className => "WebGUI::Asset::Sku::Product", + title => "Rock Hammer", }); isa_ok($product, "WebGUI::Asset::Sku::Product"); ok(! exists $product->{_collateral}, 'object cache does not exist yet'); @@ -226,6 +225,21 @@ cmp_deeply( ); $product->purge; +undef $product; + +my $product2 = $root->addChild({ + className => "WebGUI::Asset::Sku::Product", + title => "Bible", + }); + +$product2->setCollateral('variantsJSON', 'new', { s => 'scooby', d => 'doo'}); +cmp_deeply( + $product2->getCollateral('variantsJSON', 0), + { s => 'scooby', d => 'doo'}, + 'Doing a set before get works okay', +); + +$product2->purge; #---------------------------------------------------------------------------- # Cleanup