File Coverage

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


line stmt bran cond sub pod time code
1             package App::bif::drop::project;
2 1     1   4141 use strict;
  1         2  
  1         35  
3 1     1   4 use warnings;
  1         1  
  1         28  
4 1     1   3 use Bif::Mo;
  1         1  
  1         5  
5 1     1   739 use DBIx::ThinSQL qw/sq/;
  1         27598  
  1         8  
6              
7             our $VERSION = '0.1.5_5';
8             extends 'App::bif';
9              
10             sub run {
11 1     1 1 1 my $self = shift;
12 1         4 my $opts = $self->opts;
13 1         5 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 issues that are not part of any other project
68             my $res = $dbw->xdo(
69             with => 'x',
70             as => sq(
71             select => [ 'i.id', 'COUNT(i2.id) AS total' ],
72             from => 'issues i',
73             left_join => 'issues i2',
74             on => 'i2.src_id = i.id',
75             where => {
76             'i.project_id' => $info->{id},
77 0           'i.src_id' => undef,
78             },
79             group_by => 'i.id',
80             having => 'total = 0',
81             ),
82             delete_from => 'issues',
83             where => [
84             'id IN ',
85             sq(
86             select => 'x.id',
87             from => 'x',
88             )
89             ],
90             );
91              
92             $res += $dbw->xdo(
93             delete_from => 'tasks',
94             where => [
95             'task_status_id IN ',
96             sq(
97             select => 'ts.id',
98             from => 'task_status ts',
99             where => { 'ts.project_id' => $info->{id} },
100             )
101 0           ],
102             );
103              
104             # Delete project node entities, except those which are
105             # our own identities, project entities, or are also
106             # other project node entities.
107             # $res += $dbw->xdo(
108             # delete_from => 'entities',
109             # where => [
110             # 'id IN ',
111             # sq(
112             # select => 'pte.entity_id',
113             # from => 'project_node_entities pte',
114             # where => { 'pte.project_id' => $info->{id} },
115             # except_select => 'x.entity_id',
116             # from => sq(
117             # select => 'bif.identity_id AS entity_id',
118             # from => 'bifkv bif',
119             # where => { 'bif.key' => 'self' },
120             # union_all_select => 'pe.entity_id',
121             # from => 'project_entities pe',
122             # where => { 'pe.project_id' => $info->{id} },
123             # union_all_select => 'pte.entity_id',
124             # from => 'project_node_entities pte',
125             # where => { 'pte.project_id !' => $info->{id} },
126             # )->as('x'),
127             # ),
128             # ],
129             # );
130              
131             $res += $dbw->xdo(
132             update => 'projects',
133             set => { local => 0 },
134             where => { id => $info->{id} },
135 0           );
136 0           return $res;
137             }
138              
139             1;
140             __END__