File Coverage

blib/lib/App/bif/drop/project.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 6 0.0
condition n/a
subroutine 5 7 71.4
pod 1 2 50.0
total 21 51 41.1


line stmt bran cond sub pod time code
1             package App::bif::drop::project;
2 1     1   4496 use strict;
  1         2  
  1         30  
3 1     1   5 use warnings;
  1         2  
  1         29  
4 1     1   5 use Bif::Mo;
  1         1  
  1         8  
5 1     1   756 use DBIx::ThinSQL qw/sq/;
  1         27644  
  1         8  
6              
7             our $VERSION = '0.1.5_6';
8             extends 'App::bif';
9              
10             sub run {
11 1     1 1 3 my $self = shift;
12 1         5 my $opts = $self->opts;
13 1         9 my $dbw = $self->dbw;
14 0           my $info = $self->get_project( $opts->{path} );
15              
16 0 0         if ( !$opts->{force} ) {
17 0           print "Nothing dropped (missing --force, -f)\n";
18 0           return $self->ok('DropNoForce');
19             }
20              
21             $dbw->txn(
22             sub {
23 0 0   0     if ( $info->{default_hub_id} ) {
24 0           $self->start_work(
25             start => time,
26             start_comment => "drop project $info->{path} (shallow)",
27             billable => 0,
28             save => 1,
29             );
30              
31 0           $self->drop_shallow($info);
32              
33 0           print "Project dropped: $info->{path} (shallow)\n";
34             }
35             else {
36 0           $self->start_work(
37             start => time,
38             start_comment => "drop project $info->{path}",
39             billable => 0,
40             save => 1,
41             );
42              
43             $dbw->xdo(
44             delete_from => 'projects',
45             where => { id => $info->{id} },
46 0           );
47              
48 0           print "Project dropped: $info->{path})\n";
49             }
50              
51 0           $self->stop_work(
52             stop => time,
53             restore => 1,
54             );
55             }
56 0           );
57              
58 0 0         return $self->ok('DropProjectShallow') if $info->{default_hub_id};
59 0           return $self->ok('DropProject');
60             }
61              
62             sub drop_shallow {
63 0     0 0   my $self = shift;
64 0           my $info = shift;
65 0           my $dbw = $self->dbw;
66              
67             # Drop topics that are not part of any other project
68             my $res = $dbw->xdo(
69             with => 'x',
70             as => sq(
71             select => [ 't.link_topic_id', 'COUNT(t.link_topic_id) AS total' ],
72             from => 'topics t',
73             where => { 't.project_id' => $info->{id} },
74 0           group_by => 't.id',
75             having => 'total = 1',
76             ),
77             delete_from => 'link_topics',
78             where => [
79             'id IN ',
80             sq(
81             select => 'x.id',
82             from => 'x',
83             )
84             ],
85             );
86              
87             # Delete project node entities, except those which are
88             # our own identities, project entities, or are also
89             # other project node entities.
90             # $res += $dbw->xdo(
91             # delete_from => 'entities',
92             # where => [
93             # 'id IN ',
94             # sq(
95             # select => 'pte.entity_id',
96             # from => 'project_node_entities pte',
97             # where => { 'pte.project_id' => $info->{id} },
98             # except_select => 'x.entity_id',
99             # from => sq(
100             # select => 'bif.identity_id AS entity_id',
101             # from => 'bifkv bif',
102             # where => { 'bif.key' => 'self' },
103             # union_all_select => 'pe.entity_id',
104             # from => 'project_entities pe',
105             # where => { 'pe.project_id' => $info->{id} },
106             # union_all_select => 'pte.entity_id',
107             # from => 'project_node_entities pte',
108             # where => { 'pte.project_id !' => $info->{id} },
109             # )->as('x'),
110             # ),
111             # ],
112             # );
113              
114             $res += $dbw->xdo(
115             update => 'projects',
116             set => { local => 0 },
117             where => { id => $info->{id} },
118 0           );
119 0           return $res;
120             }
121              
122             1;
123             __END__