Skip to content

Commit

Permalink
Ensure the site can be restored with aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
melmothx committed Jan 14, 2024
1 parent fd72c9d commit dd059d7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
10 changes: 2 additions & 8 deletions lib/AmuseWikiFarm/Schema/Result/Aggregation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,7 @@ sub sqlt_deploy_hook {

sub titles {
my $self = shift;
my @uris = map { $_->{title_uri} }
$self->aggregation_titles->search(undef,
{
order_by => [qw/sorting_pos title_uri/],
columns => [qw/title_uri/],
result_class => 'DBIx::Class::ResultClass::HashRefInflator',
})->all;
my @uris = map { $_->{title_uri} } $self->aggregation_titles->title_uris->hri->all;
my %titles = map { $_->uri => $_ }
$self->site->titles->texts_only->status_is_published->by_uri(\@uris)->all;
my @out;
Expand Down Expand Up @@ -258,7 +252,7 @@ sub serialize {
foreach my $k (keys %vals) {
delete $vals{$k} unless defined $vals{$k};
}
$vals{titles} = [ map { $_->uri } $self->titles ];
$vals{titles} = [ map { $_->{title_uri} } $self->aggregation_titles->title_uris->hri->all ];
return \%vals;
}

Expand Down
1 change: 1 addition & 0 deletions lib/AmuseWikiFarm/Schema/Result/Site.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4345,6 +4345,7 @@ sub serialize_site {
comment_html => $_->comment_html,
alt_text => $_->alt_text,
} } $self->attachments->with_descriptions->all ];
$data{aggregations} = $self->serialize_aggregations;
return \%data;
}

Expand Down
9 changes: 9 additions & 0 deletions lib/AmuseWikiFarm/Schema/ResultSet/AggregationTitle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ sub by_title_uri {
$self->search({ "$me.title_uri" => $uri });
}

sub title_uris {
my $self = shift;
my $me = $self->current_source_alias;
$self->search(undef,
{
order_by => ["$me.sorting_pos", "$me.title_uri"],
});
}

1;
2 changes: 2 additions & 0 deletions lib/AmuseWikiFarm/Schema/ResultSet/Site.pm
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ sub deserialize_site {
my @users = @{ delete $hashref->{users} || [] };
my @nodes = @{ delete $hashref->{nodes} || [] };
my @attachments = @{ delete $hashref->{attachments} || [] };
my @aggregations = @{ delete $hashref->{aggregations} || [] };
my $site = $self->update_or_create($hashref);

# notably, tables without a non-auto PK, and where it makes sense
Expand Down Expand Up @@ -225,6 +226,7 @@ sub deserialize_site {
log_info { "$uri not found in the tree" };
}
}
$site->create_aggregation($_) for @aggregations;
$guard->commit;
$site->discard_changes;
return $site;
Expand Down
19 changes: 17 additions & 2 deletions t/aggregations.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BEGIN {
};

use Data::Dumper;
use Test::More tests => 62;
use Test::More tests => 67;
use AmuseWikiFarm::Schema;
use File::Spec::Functions qw/catfile catdir/;
use lib catdir(qw/t lib/);
Expand Down Expand Up @@ -94,7 +94,9 @@ $site->update_db_from_tree;
is $site->aggregations->count, 2;
is $site->aggregations->search_related('aggregation_titles')->count, 8;

is_deeply $site->serialize_aggregations, $copy;
# the last one is a duplicate
is pop @{$ag->[0]->{titles}}, "to-test-one";
is_deeply $site->serialize_aggregations, $ag;

sleep 1;

Expand Down Expand Up @@ -182,3 +184,16 @@ foreach my $title ($site->titles) {
}
# reset
$site->process_autoimport_files;

my $dump = $site->serialize_site;
$autoimport->remove_tree;
$site->delete;
{
my @save = @{$dump->{aggregations}};
my $newsite = $schema->resultset('Site')->deserialize_site($dump);
is scalar(@save), 2;
is_deeply $save[0]{titles}, [ 'to-test-one', 'to-test-two', 'to-test-three'];
is $save[1]{publication_date}, "Never";
is_deeply $newsite->serialize_aggregations, \@save;
diag Dumper(\@save);
}

0 comments on commit dd059d7

Please sign in to comment.