File Coverage

blib/lib/App/Sqitch/Command/revert.pm
Criterion Covered Total %
statement 55 55 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 1 1 100.0
total 77 77 100.0


line stmt bran cond sub pod time code
1              
2             use 5.010;
3 2     2   1456 use strict;
  2         8  
4 2     2   11 use warnings;
  2         4  
  2         36  
5 2     2   12 use utf8;
  2         5  
  2         52  
6 2     2   10 use Moo;
  2         4  
  2         10  
7 2     2   38 use Types::Standard qw(Int Str Bool HashRef);
  2         5  
  2         11  
8 2     2   615 use List::Util qw(first);
  2         3  
  2         27  
9 2     2   2002 use App::Sqitch::X qw(hurl);
  2         4  
  2         105  
10 2     2   11 use Locale::TextDomain qw(App-Sqitch);
  2         4  
  2         14  
11 2     2   496 use namespace::autoclean;
  2         4  
  2         14  
12 2     2   361  
  2         5  
  2         15  
13             extends 'App::Sqitch::Command';
14             with 'App::Sqitch::Role::ContextCommand';
15             with 'App::Sqitch::Role::ConnectingCommand';
16              
17             our $VERSION = 'v1.3.1'; # VERSION
18              
19             has target => (
20             is => 'ro',
21             isa => Str,
22             );
23              
24             has to_change => (
25             is => 'ro',
26             isa => Str,
27             );
28              
29             has modified => (
30             is => 'ro',
31             isa => Bool,
32             default => 0,
33             );
34              
35             has no_prompt => (
36             is => 'ro',
37             isa => Bool
38             );
39              
40             has prompt_accept => (
41             is => 'ro',
42             isa => Bool
43             );
44              
45             has log_only => (
46             is => 'ro',
47             isa => Bool,
48             default => 0,
49             );
50              
51             has lock_timeout => (
52             is => 'ro',
53             isa => Int,
54             lazy => 1,
55             default => sub { App::Sqitch::Engine::default_lock_timeout() },
56             );
57              
58             has variables => (
59             is => 'ro',
60             isa => HashRef,
61             lazy => 1,
62             default => sub { {} },
63             );
64              
65             return qw(
66             target|t=s
67             to-change|to|change=s
68             set|s=s%
69             log-only
70             lock-timeout=i
71             modified|m
72             y
73             );
74             }
75              
76             my ( $class, $config, $opt ) = @_;
77              
78             my %params = map { $_ => $opt->{$_} } grep { exists $opt->{$_} } qw(
79             to_change
80             log_only
81             lock_timeout
82             target
83             modified
84             );
85              
86             if ( my $vars = $opt->{set} ) {
87             $params{variables} = $vars
88             }
89              
90             $params{no_prompt} = delete $opt->{y} // $config->get(
91             key => 'revert.no_prompt',
92             as => 'bool',
93             ) // 0;
94              
95             $params{prompt_accept} = $config->get(
96             key => 'revert.prompt_accept',
97             as => 'bool',
98             ) // 1;
99              
100             return \%params;
101             }
102              
103             my ($self, $target) = @_;
104             my $cfg = $self->sqitch->config;
105             return (
106             %{ $cfg->get_section(section => 'core.variables') },
107 15     15   548 %{ $cfg->get_section(section => 'deploy.variables') },
108 15         261 %{ $cfg->get_section(section => 'revert.variables') },
109             %{ $target->variables }, # includes engine
110 15         84 %{ $self->variables }, # --set
111 15         45 );
112 15         40 }
113 15         257  
114 15         102 my $self = shift;
  15         570  
115             my ($targets, $changes) = $self->parse_args(
116             target => $self->target,
117             args => \@_,
118             );
119 10     10 1 29382  
120 10         78 # Warn on multiple targets.
121             my $target = shift @{ $targets };
122             $self->warn(__x(
123             'Too many targets specified; connecting to {target}',
124             target => $target->name,
125             )) if @{ $targets };
126 8         597  
  8         22  
127             # Warn on too many changes.
128             my $engine = $target->engine;
129             my $change = $self->modified
130 8 100       16 ? $engine->planned_deployed_common_ancestor_id
  8         36  
131             : $self->to_change // shift @{ $changes };
132             $self->warn(__x(
133 8         738 'Too many changes specified; reverting to "{change}"',
134             change => $change,
135             )) if @{ $changes };
136 8 100 100     1656  
  5         17  
137             # Now get to work.
138             $engine->no_prompt( $self->no_prompt );
139             $engine->prompt_accept( $self->prompt_accept );
140 8 100       19 $engine->log_only( $self->log_only );
  8         30  
141             $engine->lock_timeout( $self->lock_timeout );
142             $engine->set_variables( $self->_collect_vars($target) );
143 8         258 $engine->revert( $change );
144 8         325 return $self;
145 8         322 }
146 8         315  
147 8         510 1;
148 8         180  
149 8         137  
150             =head1 Name
151              
152             App::Sqitch::Command::revert - Revert Sqitch changes from a database
153              
154             =head1 Synopsis
155              
156             my $cmd = App::Sqitch::Command::revert->new(%params);
157             $cmd->execute;
158              
159             =head1 Description
160              
161             If you want to know how to use the C<revert> command, you probably want to be
162             reading C<sqitch-revert>. But if you really want to know how the C<revert> command
163             works, read on.
164              
165             =head1 Interface
166              
167             =head2 Class Methods
168              
169             =head3 C<options>
170              
171             my @opts = App::Sqitch::Command::revert->options;
172              
173             Returns a list of L<Getopt::Long> option specifications for the command-line
174             options for the C<revert> command.
175              
176             =head2 Attributes
177              
178             =head3 C<log_only>
179              
180             Boolean indicating whether to log the deploy without running the scripts.
181              
182             =head3 C<lock_timeout>
183              
184             The number of seconds to wait for an exclusive advisory lock on the target,
185             for engines that support the feature.
186              
187             =head3 C<modified>
188              
189             Boolean to revert to the change prior to earliest change with a revised
190             deploy script.
191              
192             =head3 C<no_prompt>
193              
194             Boolean indicating whether or not to prompt the user to really go through with
195             the revert.
196              
197             =head3 C<prompt_accept>
198              
199             Boolean value to indicate whether or not the default value for the prompt,
200             should the user hit C<return>, is to accept the prompt or deny it.
201              
202             =head3 C<target>
203              
204             The deployment target URI.
205              
206             =head3 C<to_change>
207              
208             Change to revert to.
209              
210             =head2 Instance Methods
211              
212             =head3 C<execute>
213              
214             $revert->execute;
215              
216             Executes the revert command.
217              
218             =head1 See Also
219              
220             =over
221              
222             =item L<sqitch-revert>
223              
224             Documentation for the C<revert> command to the Sqitch command-line client.
225              
226             =item L<sqitch>
227              
228             The Sqitch command-line client.
229              
230             =back
231              
232             =head1 Author
233              
234             David E. Wheeler <david@justatheory.com>
235              
236             =head1 License
237              
238             Copyright (c) 2012-2022 iovation Inc., David E. Wheeler
239              
240             Permission is hereby granted, free of charge, to any person obtaining a copy
241             of this software and associated documentation files (the "Software"), to deal
242             in the Software without restriction, including without limitation the rights
243             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
244             copies of the Software, and to permit persons to whom the Software is
245             furnished to do so, subject to the following conditions:
246              
247             The above copyright notice and this permission notice shall be included in all
248             copies or substantial portions of the Software.
249              
250             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
251             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
252             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
253             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
254             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
255             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
256             SOFTWARE.
257              
258             =cut