Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Build ID to ::Dists model
This will allow us to rebuild modules database right into the live database and keep in RAM info for just one dist at a time instead of (potentially) thousands... MILLIONS!... of dists.
  • Loading branch information
zoffixznet committed Nov 21, 2015
1 parent d9f62a2 commit 1b637f1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
29 changes: 28 additions & 1 deletion web/lib/ModulesPerl6/Model/Dists.pm
Expand Up @@ -65,7 +65,7 @@ sub add {
},
map +( $_ => $dist->{$_} ),
qw/name url description logo stars issues kwalitee
date_updated date_added/,
date_updated date_added build_id/,
});
}

Expand All @@ -83,13 +83,23 @@ sub find {
my $self = shift;
return $self->_find(1, @_);
}

sub remove {
my $self = shift;
$self->_find(0, @_)->delete_all;

$self;
}

sub remove_old {
my ( $self, $build_id ) = @_;
length $build_id or croak 'Missing Build ID to keep';

$self->_find(0, { build_id => { '!=', $build_id } })->delete_all;

$self;
}

1;

__END__
Expand All @@ -106,6 +116,7 @@ ModulesPerl6::Model::Dists - model representing Perl 6 distributions
$m->add( $dist );
say $_->{url} for $m->find({ name => 'Dist1' })->each;
$m->remove({ name => 'Dist1' });
$m->remove_old('rvOZAHmQ5RGKE79B+wjaYA==')
=head1 DESCRIPTION
Expand Down Expand Up @@ -147,6 +158,7 @@ if set, or C<modulesperl6.db>.
issues => 12,
date_updated => 1446999664,
date_added => 1446694664,
build_id => 'rvOZAHmQ5RGKE79B+wjaYA==',
});
$m->add( $dist1, $dist2 );
Expand Down Expand Up @@ -213,6 +225,12 @@ commit on GitHUb)
Unix epoch of when the dist was added to the Perl 6 Ecosystem (this is NOT
the same as when the repo was first created on GitHub).
=head3 C<build_id>
A string of text indentifying the build ID: a random string used
by the database updater script to identify each run. You'll likely want
to use something like L<Data::GUID/"to_base64"> as the ID.
=head2 C<deploy>
$m->deploy
Expand Down Expand Up @@ -266,6 +284,15 @@ Search by the Travis CI status
B<Takes> the same argument as L</find> and any matching dists will be deleted
from the database. B<Returns> its invocant.
=head2 c<remove_old>
$m->remove_old('rvOZAHmQ5RGKE79B+wjaYA==');
B<Takes> a mandatory argument, which is the build ID of the dists to
KEEP in the database. Any build IDs that do not match this will be deleted.
A likely usage of this would be to call this method after rebuilding
the database to remove any dists that were not in the new build list.
=head1 PRIVATE ATTRIBUTES
B<These attributes are documented for developers working on this module.
Expand Down
31 changes: 31 additions & 0 deletions web/lib/ModulesPerl6/Model/Dists/Schema/Result/BuildId.pm
@@ -0,0 +1,31 @@
package ModulesPerl6::Model::Dists::Schema::Result::BuildId;
use ModulesPerl6::Model::ResultClass;

primary_column id => { data_type => 'text' };

has_many dists
=> 'ModulesPerl6::Model::Dists::Schema::Result::Dist'
=> { 'foreign.build_id' => 'self.id' };

1;

__END__

=encoding utf8
=head1 NAME
ModulesPerl6::Model::Dists::Schema::Result::TravisStatus - Travis-CI statuses
=head1 DESCRIPTION
This table stores L<https://travis-ci.org/> statuses for the dists.
=head1 CONTACT INFORMATION
Original version of this module was written by Zoffix Znet
(L<https://github.com/zoffixznet/>, C<Zoffix> on irc.freenode.net).
=head1 LICENSE
You can use and distribute this module under the same terms as Perl itself.
5 changes: 5 additions & 0 deletions web/lib/ModulesPerl6/Model/Dists/Schema/Result/Dist.pm
Expand Up @@ -3,6 +3,7 @@ use ModulesPerl6::Model::ResultClass;

primary_column name => { data_type => 'text' };
primary_column author_id => { data_type => 'text', is_foreign_key => 1 };
column build_id => { data_type => 'text', is_foreign_key => 1 };
column travis_status => { data_type => 'text', is_foreign_key => 1 };
column url => { data_type => 'text' };
column description => { data_type => 'text' };
Expand All @@ -21,6 +22,10 @@ belongs_to travis
=> 'ModulesPerl6::Model::Dists::Schema::Result::TravisStatus'
=> { status => 'travis_status' };

belongs_to build_id
=> 'ModulesPerl6::Model::Dists::Schema::Result::BuildId'
=> { id => 'build_id' };

1;

__END__
Expand Down
7 changes: 6 additions & 1 deletion web/t/01-models/02-dists.t
Expand Up @@ -14,7 +14,7 @@ END { unlink TEST_DB_FILE }
use_ok MODEL;
my $m = MODEL->new( db_file => TEST_DB_FILE );
isa_ok $m => MODEL;
can_ok $m => qw/add deploy remove find/;
can_ok $m => qw/add deploy find remove remove_old/;

my ( $dist1, $dist2 ) = t::Helper::dist_out_data;

Expand Down Expand Up @@ -58,4 +58,9 @@ isa_ok $m->remove({ name => 'Dist1'}), MODEL, '->remove returns invocant';
is_deeply $m->find, [$dist2], 'removed a dist';
is_deeply $m->find({ name => 'Dist1'}), [], 'and it is no longer found';

isa_ok $m->remove_old('rvOZAHmQ5RGKE79B+wjaYA=='), MODEL,
'->remove_old returns invocant';
is_deeply $m->find, [],
'remove_old tossed dists that did not have correct build ID';

done_testing;
4 changes: 4 additions & 0 deletions web/t/Helper.pm
Expand Up @@ -40,6 +40,7 @@ sub dist_in_data {
issues => 12,
date_updated => 1446999664,
date_added => 1446694664,
build_id => 'rvOZAHmQ5RGKE79B+wjaYA==',
},
{
name => 'Dist2',
Expand All @@ -55,6 +56,7 @@ sub dist_in_data {
issues => 6,
date_updated => 1446490664,
date_added => 1445904664,
build_id => '+IIcE3mQ5RGZYQhR+wjaYA==',
},
);
}
Expand All @@ -73,6 +75,7 @@ sub dist_out_data {
issues => 12,
date_updated => 1446999664,
date_added => 1446694664,
build_id => 'rvOZAHmQ5RGKE79B+wjaYA==',
},
{
name => 'Dist2',
Expand All @@ -86,6 +89,7 @@ sub dist_out_data {
issues => 6,
date_updated => 1446490664,
date_added => 1445904664,
build_id => '+IIcE3mQ5RGZYQhR+wjaYA==',
},
);
}
Expand Down

0 comments on commit 1b637f1

Please sign in to comment.