@@ -, +, @@ - Ensure you have at least one article request created with the FreeForm backend - Check the metadata for the request: => TEST: You should have properties such as 'article_title', 'article_author' populated => TEST: You should NOT have properties such as 'container_title' & 'pages' - Run the upgrade => TEST: For the same requests you should now have the following properties: - container_title (this should correspond with 'title') - title (this should correspond with 'article_title') - pages (this should correspond with 'article_pages') - author (this should correspond with 'article_author') --- .../bug_21079_map_illrequestattributes.perl | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 installer/data/mysql/atomicupdate/bug_21079_map_illrequestattributes.perl --- a/installer/data/mysql/atomicupdate/bug_21079_map_illrequestattributes.perl +++ a/installer/data/mysql/atomicupdate/bug_21079_map_illrequestattributes.perl @@ -0,0 +1,94 @@ +use Try::Tiny; +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + + # All attributes we're potentially interested in + my $ff_req = $dbh->selectall_arrayref( + 'SELECT a.illrequest_id, a.type, a.value '. + 'FROM illrequests_temp r, illrequestattributes_temp a '. + 'WHERE r.illrequest_id = a.illrequest_id '. + 'AND r.backend = "FreeForm"', + { Slice => {} } + ); + + # Before we go any further, identify whether we've done + # this before, we test for the presence of "container_title" + # We stop as soon as we find one + foreach my $req(@{$ff_req}) { + if ($req->{type} eq 'container_title') { + warn "Upgrade already carried out"; + } + } + + # Transform into a hashref with the key of the request ID + my $requests = {}; + foreach my $request(@{$ff_req}) { + my $id = $request->{illrequest_id}; + if (!exists $requests->{$id}) { + $requests->{$id} = {}; + } + $requests->{$id}->{$request->{type}} = $request->{value}; + } + + # Transform any article requests + my $transformed = {}; + foreach my $id(keys %{$requests}) { + if (lc($requests->{$id}->{type}) eq 'article') { + $transformed->{$id} = $requests->{$id}; + $transformed->{$id}->{type} = 'article'; + $transformed->{$id}->{container_title} = $transformed->{$id}->{title} + if defined $transformed->{$id}->{title} && + length $transformed->{$id}->{title} > 0; + $transformed->{$id}->{title} = $transformed->{$id}->{article_title} + if defined $transformed->{$id}->{article_title} && + length $transformed->{$id}->{article_title} > 0; + $transformed->{$id}->{author} = $transformed->{$id}->{article_author} + if defined $transformed->{$id}->{article_author} && + length $transformed->{$id}->{article_author} > 0; + $transformed->{$id}->{pages} = $transformed->{$id}->{article_pages} + if defined $transformed->{$id}->{article_pages} && + length $transformed->{$id}->{article_pages} > 0; + } + } + + # Now write back the transformed data + # Rather than selectively replace, we just remove all attributes we've + # transformed and re-write them + my @changed = keys %{$transformed}; + my $changed_str = join(',', @changed); + + if (scalar @changed > 0) { + $dbh->{AutoCommit} = 0; + $dbh->{RaiseError} = 1; + try { + my $del = $dbh->do( + "DELETE FROM illrequestattributes_temp ". + "WHERE illrequest_id IN ($changed_str)" + ); + foreach my $reqid(keys %{$transformed}) { + my $attr = $transformed->{$reqid}; + foreach my $key(keys %{$attr}) { + my $sth = $dbh->prepare( + 'INSERT INTO illrequestattributes_temp '. + '(illrequest_id, type, value, readonly) '. + 'VALUES '. + '(?, ?, ?, ?)' + ); + $sth->execute( + $reqid, + $key, + $attr->{$key}, + 0 + ); + } + } + $dbh->commit; + } catch { + warn "Upgrade to $DBversion failed: $_"; + eval { $dbh->rollback }; + }; + } + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 21079 - Unify metadata schema across backends)\n"; +} --