-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathEngine.pm
2689 lines (1965 loc) · 72 KB
/
Engine.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package App::Sqitch::Engine;
use 5.010;
use Moo;
use strict;
use utf8;
use Try::Tiny;
use Locale::TextDomain qw(App-Sqitch);
use Path::Class qw(file);
use App::Sqitch::X qw(hurl);
use List::Util qw(first max);
use URI::db 0.20;
use App::Sqitch::Types qw(Str Int Num Sqitch Plan Bool HashRef URI Maybe Target);
use namespace::autoclean;
use constant registry_release => '1.1';
use constant default_lock_timeout => 60;
# VERSION
has sqitch => (
is => 'ro',
isa => Sqitch,
required => 1,
weak_ref => 1,
);
has target => (
is => 'ro',
isa => Target,
required => 1,
weak_ref => 1,
handles => {
uri => 'uri',
client => 'client',
registry => 'registry',
destination => 'name',
}
);
has username => (
is => 'ro',
isa => Maybe[Str],
lazy => 1,
default => sub {
my $self = shift;
$self->target->username || $self->_def_user
},
);
has password => (
is => 'ro',
isa => Maybe[Str],
lazy => 1,
default => sub {
my $self = shift;
$self->target->password || $self->_def_pass
},
);
sub _def_user { }
sub _def_pass { }
sub registry_destination { shift->destination }
has start_at => (
is => 'rw',
isa => Str
);
has no_prompt => (
is => 'rw',
isa => Bool,
default => 0,
);
has prompt_accept => (
is => 'rw',
isa => Bool,
default => 1,
);
has log_only => (
is => 'rw',
isa => Bool,
default => 0,
);
has with_verify => (
is => 'rw',
isa => Bool,
default => 0,
);
has max_name_length => (
is => 'rw',
isa => Int,
default => 0,
lazy => 1,
default => sub {
my $plan = shift->plan;
max map {
length $_->format_name_with_tags
} $plan->changes;
},
);
has plan => (
is => 'rw',
isa => Plan,
lazy => 1,
default => sub { shift->target->plan }
);
has _variables => (
is => 'rw',
isa => HashRef[Str],
default => sub { {} },
);
# Usually expressed as an integer, but made a number for the purposes of
# shorter test run times.
has lock_timeout => (
is => 'rw',
isa => Num,
default => default_lock_timeout,
);
has _locked => (
is => 'rw',
isa => Bool,
default => 0,
);
sub variables { %{ shift->_variables } }
sub set_variables { shift->_variables({ @_ }) }
sub clear_variables { %{ shift->_variables } = () }
sub default_registry { 'sqitch' }
sub load {
my ( $class, $p ) = @_;
# We should have an engine param.
my $target = $p->{target} or hurl 'Missing "target" parameter to load()';
# Load the engine class.
my $ekey = $target->engine_key or hurl engine => __(
'No engine specified; specify via target or core.engine'
);
my $pkg = __PACKAGE__ . '::' . $target->engine_key;
eval "require $pkg" or hurl "Unable to load $pkg";
return $pkg->new( $p );
}
sub driver { shift->key }
sub key {
my $class = ref $_[0] || shift;
hurl engine => __ 'No engine specified; specify via target or core.engine'
if $class eq __PACKAGE__;
my $pkg = quotemeta __PACKAGE__;
$class =~ s/^$pkg\:://;
return $class;
}
sub name { shift->key }
sub config_vars {
return (
target => 'any',
registry => 'any',
client => 'any'
);
}
sub use_driver {
my $self = shift;
my $driver = $self->driver;
eval "use $driver";
hurl $self->key => __x(
'{driver} required to manage {engine}',
driver => $driver,
engine => $self->name,
) if $@;
return $self;
}
sub deploy {
my ( $self, $to, $mode ) = @_;
$self->lock_destination;
my $sqitch = $self->sqitch;
my $plan = $self->_sync_plan;
my $to_index = $plan->count - 1;
my $position = $plan->position;
hurl plan => __ 'Nothing to deploy (empty plan)' if $to_index < 0;
if (defined $to) {
$to_index = $plan->index_of($to) // hurl plan => __x(
'Unknown change: "{change}"',
change => $to,
);
# Just return if there is nothing to do.
if ($to_index == $position) {
$sqitch->info(__x(
'Nothing to deploy (already at "{change}")',
change => $to
));
return $self;
}
}
if ($position == $to_index) {
# We are up-to-date.
$sqitch->info( __ 'Nothing to deploy (up-to-date)' );
return $self;
} elsif ($position == -1) {
# Initialize or upgrade the database, if necessary.
if ($self->initialized) {
$self->upgrade_registry;
} else {
$sqitch->info(__x(
'Adding registry tables to {destination}',
destination => $self->registry_destination,
));
$self->initialize;
}
$self->register_project;
} else {
# Make sure that $to_index is greater than the current point.
hurl deploy => __ 'Cannot deploy to an earlier change; use "revert" instead'
if $to_index < $position;
# Upgrade database if it needs it.
$self->upgrade_registry;
}
$sqitch->info(
defined $to ? __x(
'Deploying changes through {change} to {destination}',
change => $plan->change_at($to_index)->format_name_with_tags,
destination => $self->destination,
) : __x(
'Deploying changes to {destination}',
destination => $self->destination,
)
);
# Check that all dependencies will be satisfied.
$self->check_deploy_dependencies($plan, $to_index);
# Do it!
$mode ||= 'all';
my $meth = $mode eq 'change' ? '_deploy_by_change'
: $mode eq 'tag' ? '_deploy_by_tag'
: $mode eq 'all' ? '_deploy_all'
: hurl deploy => __x 'Unknown deployment mode: "{mode}"', mode => $mode;
;
$self->max_name_length(
max map {
length $_->format_name_with_tags
} ($plan->changes)[$position + 1..$to_index]
);
$self->$meth( $plan, $to_index );
}
sub revert {
my ( $self, $to ) = @_;
# Check the registry and, once we know it's there, lock the destination.
$self->_check_registry;
$self->lock_destination;
my $sqitch = $self->sqitch;
my $plan = $self->plan;
my @changes;
if (defined $to) {
my ($change) = $self->_load_changes(
$self->change_for_key($to)
) or do {
# Not deployed. Is it in the plan?
if ( $plan->find($to) ) {
# Known but not deployed.
hurl revert => __x(
'Change not deployed: "{change}"',
change => $to
);
}
# Never heard of it.
hurl revert => __x(
'Unknown change: "{change}"',
change => $to,
);
};
@changes = $self->deployed_changes_since(
$self->_load_changes($change)
) or do {
$sqitch->info(__x(
'No changes deployed since: "{change}"',
change => $to,
));
return $self;
};
if ($self->no_prompt) {
$sqitch->info(__x(
'Reverting changes to {change} from {destination}',
change => $change->format_name_with_tags,
destination => $self->destination,
));
} else {
hurl {
ident => 'revert:confirm',
message => __ 'Nothing reverted',
exitval => 1,
} unless $sqitch->ask_yes_no(__x(
'Revert changes to {change} from {destination}?',
change => $change->format_name_with_tags,
destination => $self->destination,
), $self->prompt_accept );
}
} else {
@changes = $self->deployed_changes or do {
$sqitch->info(__ 'Nothing to revert (nothing deployed)');
return $self;
};
if ($self->no_prompt) {
$sqitch->info(__x(
'Reverting all changes from {destination}',
destination => $self->destination,
));
} else {
hurl {
ident => 'revert',
message => __ 'Nothing reverted',
exitval => 1,
} unless $sqitch->ask_yes_no(__x(
'Revert all changes from {destination}?',
destination => $self->destination,
), $self->prompt_accept );
}
}
# Make change objects and check that all dependencies will be satisfied.
@changes = reverse $self->_load_changes( @changes );
$self->check_revert_dependencies(@changes);
# Do we want to support modes, where failures would re-deploy to previous
# tag or all the way back to the starting point? This would be very much
# like deploy() mode. I'm thinking not, as a failure on a revert is not
# something you generally want to recover from by deploying back to where
# you started. But maybe I'm wrong?
$self->max_name_length(
max map { length $_->format_name_with_tags } @changes
);
$self->revert_change($_) for @changes;
return $self;
}
sub verify {
my ( $self, $from, $to ) = @_;
$self->_check_registry;
my $sqitch = $self->sqitch;
my $plan = $self->plan;
my @changes = $self->_load_changes( $self->deployed_changes );
$sqitch->info(__x(
'Verifying {destination}',
destination => $self->destination,
));
if (!@changes) {
my $msg = $plan->count
? __ 'No changes deployed'
: __ 'Nothing to verify (no planned or deployed changes)';
$sqitch->info($msg);
return $self;
}
if ($plan->count == 0) {
# Oy, there are deployed changes, but not planned!
hurl verify => __ 'There are deployed changes, but none planned!';
}
# Figure out where to start and end relative to the plan.
my $from_idx = $self->_from_idx('verify', $from, \@changes);
my $to_idx = $self->_to_idx('verify', $to, \@changes);
# Run the verify tests.
if ( my $count = $self->_verify_changes($from_idx, $to_idx, !$to, @changes) ) {
# Emit a quick report.
# XXX Consider coloring red.
my $num_changes = 1 + $to_idx - $from_idx;
$num_changes = @changes if @changes > $num_changes;
my $msg = __ 'Verify Summary Report';
$sqitch->emit("\n", $msg);
$sqitch->emit('-' x length $msg);
$sqitch->emit(__x 'Changes: {number}', number => $num_changes );
$sqitch->emit(__x 'Errors: {number}', number => $count );
hurl verify => __ 'Verify failed';
}
# Success!
# XXX Consider coloring green.
$sqitch->emit(__ 'Verify successful');
return $self;
}
sub _from_idx {
my ( $self, $ident, $from, $changes) = @_;
return 0 unless defined $from;
return $self->_trim_to($ident, $from, $changes)
}
sub _to_idx {
my ( $self, $ident, $to, $changes) = @_;
my $plan = $self->plan;
return $self->_trim_to($ident, $to, $changes, 1) if defined $to;
if (my $id = $self->latest_change_id) {
return $plan->index_of( $id ) // $plan->count - 1;
}
return $plan->count - 1;
}
sub _trim_to {
my ( $self, $ident, $key, $changes, $pop ) = @_;
my $sqitch = $self->sqitch;
my $plan = $self->plan;
# Find the to change in the database.
my $to_id = $self->change_id_for_key( $key ) || hurl $ident => (
$plan->contains( $key ) ? __x(
'Change "{change}" has not been deployed',
change => $key,
) : __x(
'Cannot find "{change}" in the database or the plan',
change => $key,
)
);
# Find the change in the plan.
my $to_idx = $plan->index_of( $to_id ) // hurl $ident => __x(
'Change "{change}" is deployed, but not planned',
change => $key,
);
# Pop or shift changes till we find the change we want.
if ($pop) {
pop @{ $changes } while $changes->[-1]->id ne $to_id;
} else {
shift @{ $changes } while $changes->[0]->id ne $to_id;
}
# We good.
return $to_idx;
}
sub _verify_changes {
my $self = shift;
my $from_idx = shift;
my $to_idx = shift;
my $pending = shift;
my $sqitch = $self->sqitch;
my $plan = $self->plan;
my $errcount = 0;
my $i = -1;
my @seen;
my $max_name_len = max map {
length $_->format_name_with_tags
} @_, map { $plan->change_at($_) } $from_idx..$to_idx;
for my $change (@_) {
$i++;
my $errs = 0;
my $reworked = 0;
my $name = $change->format_name_with_tags;
$sqitch->emit_literal(
" * $name ..",
'.' x ($max_name_len - length $name), ' '
);
my $plan_index = $plan->index_of( $change->id );
if (defined $plan_index) {
push @seen => $plan_index;
if ( $plan_index != ($from_idx + $i) ) {
$sqitch->comment(__ 'Out of order');
$errs++;
}
# Is it reworked?
$reworked = $plan->change_at($plan_index)->is_reworked;
} else {
$sqitch->comment(__ 'Not present in the plan');
$errs++;
}
# Run the verify script.
try { $self->verify_change( $change ) } catch {
$sqitch->comment(eval { $_->message } // $_);
$errs++;
} unless $reworked;
# Emit pass/fail and add to the total error count.
$sqitch->emit( $errs ? __ 'not ok' : __ 'ok' );
$errcount += $errs;
}
# List any undeployed changes.
for my $idx ($from_idx..$to_idx) {
next if defined first { $_ == $idx } @seen;
my $change = $plan->change_at( $idx );
my $name = $change->format_name_with_tags;
$sqitch->emit_literal(
" * $name ..",
'.' x ($max_name_len - length $name), ' ',
__ 'not ok', ' '
);
$sqitch->comment(__ 'Not deployed');
$errcount++;
}
# List any pending changes.
if ($pending && $to_idx < ($plan->count - 1)) {
if (my @pending = map {
$plan->change_at($_)
} ($to_idx + 1)..($plan->count - 1) ) {
$sqitch->emit(__n(
'Undeployed change:',
'Undeployed changes:',
@pending,
));
$sqitch->emit( ' * ', $_->format_name_with_tags ) for @pending;
}
}
return $errcount;
}
sub verify_change {
my ( $self, $change ) = @_;
my $file = $change->verify_file;
if (-e $file) {
return try { $self->run_verify($file) }
catch {
hurl {
ident => 'verify',
previous_exception => $_,
message => __x(
'Verify script "{script}" failed.',
script => $file,
),
};
};
}
# The file does not exist. Complain, but don't die.
$self->sqitch->vent(__x(
'Verify script {file} does not exist',
file => $file,
));
return $self;
}
sub run_deploy { shift->run_file(@_) }
sub run_revert { shift->run_file(@_) }
sub run_verify { shift->run_file(@_) }
sub run_upgrade { shift->run_file(@_) }
sub check_deploy_dependencies {
my ( $self, $plan, $to_index ) = @_;
my $from_index = $plan->position + 1;
$to_index //= $plan->count - 1;
my @changes = map { $plan->change_at($_) } $from_index..$to_index;
my (%seen, @conflicts, @required);
for my $change (@changes) {
# Check for conflicts.
push @conflicts => grep {
$seen{ $_->id // '' } || $self->change_id_for_depend($_)
} $change->conflicts;
# Check for prerequisites.
push @required => grep { !$_->resolved_id(do {
if ( my $req = $seen{ $_->id // '' } ) {
$req->id;
} else {
$self->change_id_for_depend($_);
}
}) } $change->requires;
$seen{ $change->id } = $change;
}
if (@conflicts or @required) {
require List::MoreUtils;
my $listof = sub { List::MoreUtils::uniq(map { $_->as_string } @_) };
# Dependencies not satisfied. Put together the error messages.
my @msg;
push @msg, __nx(
'Conflicts with previously deployed change: {changes}',
'Conflicts with previously deployed changes: {changes}',
scalar @conflicts,
changes => join ' ', @conflicts,
) if @conflicts = $listof->(@conflicts);
push @msg, __nx(
'Missing required change: {changes}',
'Missing required changes: {changes}',
scalar @required,
changes => join ' ', @required,
) if @required = $listof->(@required);
hurl deploy => join "\n" => @msg;
}
# Make sure nothing isn't already deployed.
if ( my @ids = $self->are_deployed_changes(@changes) ) {
hurl deploy => __nx(
'Change "{changes}" has already been deployed',
'Changes have already been deployed: {changes}',
scalar @ids,
changes => join ', ', map { $seen{$_}->format_name_with_tags . " ($_)" } @ids
);
}
return $self;
}
sub check_revert_dependencies {
my $self = shift;
my $proj = $self->plan->project;
my (%seen, @msg);
for my $change (@_) {
$seen{ $change->id } = 1;
my @requiring = grep {
!$seen{ $_->{change_id} }
} $self->changes_requiring_change($change) or next;
# XXX Include change_id in the output?
push @msg => __nx(
'Change "{change}" required by currently deployed change: {changes}',
'Change "{change}" required by currently deployed changes: {changes}',
scalar @requiring,
change => $change->format_name_with_tags,
changes => join ' ', map {
($_->{project} eq $proj ? '' : "$_->{project}:" )
. $_->{change}
. ($_->{asof_tag} // '')
} @requiring
);
}
hurl revert => join "\n", @msg if @msg;
# XXX Should we make sure that they are all deployed before trying to
# revert them?
return $self;
}
sub change_id_for_depend {
my ( $self, $dep ) = @_;
hurl engine => __x(
'Invalid dependency: {dependency}',
dependency => $dep->as_string,
) unless defined $dep->id
|| defined $dep->change
|| defined $dep->tag;
# Return the first one.
return $self->change_id_for(
change_id => $dep->id,
change => $dep->change,
tag => $dep->tag,
project => $dep->project,
first => 1,
);
}
sub _params_for_key {
my ( $self, $key ) = @_;
my $offset = App::Sqitch::Plan::ChangeList::_offset $key;
my ( $cname, $tag ) = split /@/ => $key, 2;
my @off = ( offset => $offset );
return ( @off, change => $cname, tag => $tag ) if $tag;
return ( @off, change_id => $cname ) if $cname =~ /^[0-9a-f]{40}$/;
return ( @off, tag => $cname ) if $cname eq 'HEAD' || $cname eq 'ROOT';
return ( @off, change => $cname );
}
sub change_id_for_key {
my $self = shift;
return $self->find_change_id( $self->_params_for_key(shift) );
}
sub find_change_id {
my ( $self, %p ) = @_;
# Find the change ID or return undef.
my $change_id = $self->change_id_for(
change_id => $p{change_id},
change => $p{change},
tag => $p{tag},
project => $p{project} || $self->plan->project,
) // return;
# Return relative to the offset.
return $self->change_id_offset_from_id($change_id, $p{offset});
}
sub change_for_key {
my $self = shift;
return $self->find_change( $self->_params_for_key(shift) );
}
sub find_change {
my ( $self, %p ) = @_;
# Find the change ID or return undef.
my $change_id = $self->change_id_for(
change_id => $p{change_id},
change => $p{change},
tag => $p{tag},
project => $p{project} || $self->plan->project,
) // return;
# Return relative to the offset.
return $self->change_offset_from_id($change_id, $p{offset});
}
sub _load_changes {
my $self = shift;
my $plan = $self->plan;
my (@changes, %seen);
my %rework_tags_for;
for my $params (@_) {
next unless $params;
my $tags = $params->{tags} || [];
my $c = App::Sqitch::Plan::Change->new(%{ $params }, plan => $plan );
# Add tags.
$c->add_tag(
App::Sqitch::Plan::Tag->new(name => $_, plan => $plan, change => $c )
) for map { s/^@//; $_ } @{ $tags };
if ( defined ( my $prev_idx = $seen{ $params->{name} } ) ) {
# It's reworked; grab all subsequent tags up to but not including
# the reworking change to the reworked change.
my $ctags = $rework_tags_for{ $prev_idx } ||= [];
my $i;
for my $x ($prev_idx..$#changes) {
my $rtags = $ctags->[$i++] ||= [];
my %s = map { $_->name => 1 } @{ $rtags };
push @{ $rtags } => grep { !$s{$_->name} } $changes[$x]->tags;
}
}
if ( defined ( my $reworked_idx = eval {
$plan->first_index_of( @{ $params }{qw(name id)} )
} ) ) {
# The plan has it reworked later; grab all tags from this change
# up to but not including the reworked change.
my $ctags = $rework_tags_for{ $#changes + 1 } ||= [];
my $idx = $plan->index_of($params->{id});
my $i;
for my $x ($idx..$reworked_idx - 1) {
my $c = $plan->change_at($x);
my $rtags = $ctags->[$i++] ||= [];
push @{ $rtags } => $plan->change_at($x)->tags;
}
}
push @changes => $c;
$seen{ $params->{name} } = $#changes;
}
# Associate all rework tags in reverse order. Tags fetched from the plan
# have priority over tags fetched from the database.
while (my ($idx, $tags) = each %rework_tags_for) {
my %seen;
$changes[$idx]->add_rework_tags(
grep { !$seen{$_->name}++ }
map { @{ $_ } } reverse @{ $tags }
);
}
return @changes;
}
sub _handle_lookup_index {
my ( $self, $change, $ids ) = @_;
# Return if 0 or 1 ID.
return $ids->[0] if @{ $ids } <= 1;
# Too many found! Let the user know.
my $sqitch = $self->sqitch;
$sqitch->vent(__x(
'Change "{change}" is ambiguous. Please specify a tag-qualified change:',
change => $change,
));
# Lookup, emit reverse-chron list of tag-qualified changes, and die.
my $plan = $self->plan;
for my $id ( reverse @{ $ids } ) {
# Look in the plan, first.
if ( my $change = $plan->find($id) ) {
$self->sqitch->vent( ' * ', $change->format_tag_qualified_name )
} else {
# Look it up in the database.
$self->sqitch->vent( ' * ', $self->name_for_change_id($id) // '' )
}
}
hurl engine => __ 'Change Lookup Failed';
}
sub _deploy_by_change {
my ( $self, $plan, $to_index ) = @_;
# Just deploy each change. If any fails, we just stop.
while ($plan->position < $to_index) {
$self->deploy_change($plan->next);
}
return $self;
}
sub _rollback {
my ($self, $tagged) = (shift, shift);
my $sqitch = $self->sqitch;
if (my @run = reverse @_) {
$tagged = $tagged ? $tagged->format_name_with_tags : $self->start_at;
$sqitch->vent(
$tagged ? __x('Reverting to {change}', change => $tagged)
: __ 'Reverting all changes'
);
try {
$self->revert_change($_) for @run;
} catch {
# Sucks when this happens.
$sqitch->vent(eval { $_->message } // $_);
$sqitch->vent(__ 'The schema will need to be manually repaired');
};
}
hurl deploy => __ 'Deploy failed';
}
sub _deploy_by_tag {
my ( $self, $plan, $to_index ) = @_;
my ($last_tagged, @run);
try {
while ($plan->position < $to_index) {
my $change = $plan->next;
$self->deploy_change($change);
push @run => $change;
if ($change->tags) {
@run = ();
$last_tagged = $change;
}
}
} catch {
if (my $ident = eval { $_->ident }) {
$self->sqitch->vent($_->message) unless $ident eq 'private'
} else {
$self->sqitch->vent($_);
}
$self->_rollback($last_tagged, @run);
};
return $self;
}
sub _deploy_all {
my ( $self, $plan, $to_index ) = @_;
my @run;
try {
while ($plan->position < $to_index) {
my $change = $plan->next;
$self->deploy_change($change);
push @run => $change;
}
} catch {
if (my $ident = try { $_->ident }) {
$self->sqitch->vent($_->message) unless $ident eq 'private'
} else {
$self->sqitch->vent($_);
}
$self->_rollback(undef, @run);
};
return $self;
}
sub _sync_plan {
my $self = shift;
my $plan = $self->plan;
if (my $state = $self->current_state) {
my $idx = $plan->index_of($state->{change_id}) // hurl plan => __x(
'Cannot find change {id} ({change}) in {file}',
id => $state->{change_id},
change => join(' ', $state->{change}, @{ $state->{tags} || [] }),
file => $plan->file,
);
# Upgrade the registry if there is no script_hash column.
unless ( exists $state->{script_hash} ) {
$self->upgrade_registry;
$state->{script_hash} = $state->{change_id};
}
# Update the script hashes if they're the same as the change ID.
# DEPRECATTION: Added in v0.998 (Jan 2015, c86cba61c); consider removing
# in the future when all databases are likely to be updated already.
$self->_update_script_hashes if $state->{script_hash}
&& $state->{script_hash} eq $state->{change_id};
$plan->position($idx);
my $change = $plan->change_at($idx);
if (my @tags = $change->tags) {
$self->log_new_tags($change);
$self->start_at( $change->format_name . $tags[-1]->format_name );
} else {
$self->start_at( $change->format_name );
}
} else {
$plan->reset;
}
return $plan;
}
sub is_deployed {
my ($self, $thing) = @_;
return $thing->isa('App::Sqitch::Plan::Tag')
? $self->is_deployed_tag($thing)
: $self->is_deployed_change($thing);
}
sub deploy_change {
my ( $self, $change ) = @_;
my $sqitch = $self->sqitch;
my $name = $change->format_name_with_tags;
$sqitch->info_literal(
" + $name ..",
'.' x ($self->max_name_length - length $name), ' '
);
$self->begin_work($change);
return try {
$self->run_deploy($change->deploy_file) unless $self->log_only;
try {
$self->verify_change( $change ) if $self->with_verify;
$self->log_deploy_change($change);
$sqitch->info(__ 'ok');
} catch {
# Oy, logging or verify failed. Rollback.
$sqitch->vent(eval { $_->message } // $_);
$self->rollback_work($change);
# Begin work and run the revert.
try {
# Don't bother displaying the reverting change name.
# $self->sqitch->info(' - ', $change->format_name_with_tags);
$self->begin_work($change);
$self->run_revert($change->revert_file) unless $self->log_only;
} catch {
# Oy, the revert failed. Just emit the error.
$sqitch->vent(eval { $_->message } // $_);
};
hurl private => __ 'Deploy failed';
};
} finally {
$self->finish_work($change);
} catch {
$self->log_fail_change($change);
$sqitch->info(__ 'not ok');
die $_;
};
}