From 824c789f710ec38a31e10b22fc8fd94af71643bc Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 14 Aug 2008 21:21:31 +0000 Subject: [PATCH] - Changed update() so that it only updates fields passed in, and the defaults for assets are processed in addRevision() instead. - Changed update() so it can autodetect missing fields in asset tables and log them nicely instead of crashing. --- docs/changelog/7.x.x.txt | 4 ++++ lib/WebGUI/Asset.pm | 29 +++++++++++++++-------------- lib/WebGUI/AssetVersioning.pm | 15 ++++++++++++--- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 42054aa05..ccc24fce8 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,4 +1,8 @@ 7.5.21 + - Changed update() so that it only updates fields passed in, and the defaults + for assets are processed in addRevision() instead. + - Changed update() so it can autodetect missing fields in asset tables and + log them nicely instead of crashing. 7.5.20 - fixed: DataForm acknowledgement screen shows incorrect value for Date/Time fields diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index ee561b12e..6f02bae75 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -2225,30 +2225,31 @@ sub update { } # check the definition of all properties against what was given to us - - # get a DB object for the two conditionals below, and the description - my %assetDataFields; - my $sth = $self->session->db->read('DESCRIBE `assetData`'); - while (my ($col) = $sth->array) { - $assetDataFields{$col} = 1; - } - foreach my $definition (reverse @{$self->definition($self->session)}) { my %setPairs = (); + # get a list of the fields available in this table so we don't try to insert + # something for a field that doesn't exist + my %tableFields = (); + my $sth = $self->session->db->read('DESCRIBE `'.$definition->{tableName}.'`'); + while (my ($col) = $sth->array) { + $tableFields{$col} = 1; + } + # deal with all the properties in this part of the definition foreach my $property (keys %{$definition->{properties}}) { - # skip a property unless it was specified to be set by the properties field or has a default value - next unless (exists $properties->{$property} || exists $definition->{properties}{$property}{defaultValue}); +# # skip a property unless it was specified to be set by the properties field or has a default value +# next unless (exists $properties->{$property} || exists $definition->{properties}{$property}{defaultValue}); + # skip a property unless it was specified to be set by the properties field + next unless (exists $properties->{$property}); # skip a property if it has the display only flag set next if ($definition->{properties}{$property}{displayOnly}); - # if this is the new-to-7.5 isExportable field, check if the - # database field for it exists. if not, setting it will break, so - # skip it. this facilitates updating from previous versions. - if ($definition->{tableName} eq 'assetData' && !exists $assetDataFields{$property}) { + # skip properties that aren't yet in the table + if (!exists $tableFields{$property}) { + $self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$definition->{tableName}."'"); next; } diff --git a/lib/WebGUI/AssetVersioning.pm b/lib/WebGUI/AssetVersioning.pm index 4a22dbe79..ccdfeaa7f 100644 --- a/lib/WebGUI/AssetVersioning.pm +++ b/lib/WebGUI/AssetVersioning.pm @@ -120,7 +120,15 @@ sub addRevision { $self->getId, ]); + my %defaults = (); foreach my $definition (@{$self->definition($self->session)}) { + + # get the default values of each property + foreach my $property (keys %{$definition->{properties}}) { + $defaults{$property} = $definition->{properties}{$property}{defaultValue}; + } + + # prime the tables unless ($definition->{tableName} eq "assetData") { $self->session->db->write( "insert into ".$definition->{tableName}." (assetId,revisionDate) values (?,?)", @@ -129,15 +137,16 @@ sub addRevision { } } $self->session->db->commit; + + # merge the defaults, current values, and the user set properties + my %mergedProperties = (%defaults, %{$self->get}, %{$properties}, (status => 'pending')); #Instantiate new revision and fill with real data my $newVersion = WebGUI::Asset->new($self->session,$self->getId, $self->get("className"), $now); $newVersion->setSkipNotification if ($options->{skipNotification}); $newVersion->updateHistory("created revision"); - $newVersion->update($self->get); $newVersion->setVersionLock; - $properties->{status} = 'pending'; - $newVersion->update($properties); + $newVersion->update(\%mergedProperties); $newVersion->setAutoCommitTag($workingTag) if (defined $autoCommitId); return $newVersion;