File Coverage

blib/lib/Test/DBChanges/TableChangeSet.pm
Criterion Covered Total %
statement 11 36 30.5
branch 0 6 0.0
condition 0 2 0.0
subroutine 4 11 36.3
pod 1 1 100.0
total 16 56 28.5


line stmt bran cond sub pod time code
1             package Test::DBChanges::TableChangeSet;
2 2     2   14 use Moo;
  2         5  
  2         17  
3 2     2   651 use 5.024;
  2         8  
4 2     2   13 use Types::Standard qw(Str CodeRef ArrayRef HashRef);
  2         4  
  2         13  
5 2     2   1746 use namespace::autoclean;
  2         4  
  2         14  
6              
7             our $VERSION = '1.0.2'; # VERSION
8             # ABSTRACT: set of changes to one DB table
9              
10              
11             has table_name => ( is => 'ro', required => 1, isa => Str );
12             has source_name => ( is => 'ro', required => 1, isa => Str );
13             # how to turn the raw changes into the proper objects that the caller expects
14             has factory_sub => ( is => 'ro', required => 1, isa => CodeRef );
15             has _raw_changes => ( is => 'ro', required => 1, isa => ArrayRef[HashRef],
16             init_arg => 'raw_changes' );
17              
18             has _raw_changes_by_operation => (
19             is => 'lazy',
20             builder => sub {
21 0     0     my ($self) = @_;
22              
23 0           my %changes_by_operation;
24 0           for my $change ($self->_raw_changes->@*) {
25             push $changes_by_operation{$change->{operation}}->@*,
26 0           $change;
27             }
28              
29 0           return \%changes_by_operation;
30             },
31             );
32              
33             sub _make_row_objects {
34 0     0     my ($self,$operation) = @_;
35              
36             return [ map {
37             $self->factory_sub->($_->{data})
38 0           } $self->_raw_changes_by_operation->{$operation}->@* ];
  0            
39             }
40              
41              
42             has inserted_rows => (
43             is => 'lazy',
44 0     0     builder => sub { shift->_make_row_objects('INSERT') },
45             );
46              
47              
48             has updated_rows => (
49             is => 'lazy',
50 0     0     builder => sub { shift->_make_row_objects('UPDATE') },
51             );
52              
53              
54             has deleted_rows => (
55             is => 'lazy',
56 0     0     builder => sub { shift->_make_row_objects('DELETE') },
57             );
58              
59             sub _raw_combined_changes_data {
60 0     0     my ($self,$key_column) = @_;
61 0   0       $key_column //= 'id';
62              
63 0           my %data_by_key;
64 0           for my $change ($self->_raw_changes->@*) {
65 0           my $key = $change->{data}{$key_column};
66 0 0         if ($change->{operation} eq 'INSERT') {
    0          
    0          
67 0           $data_by_key{$key} = $change->{data};
68             }
69             elsif ($change->{operation} eq 'UPDATE') {
70 0           my %data = $change->{data}->%*;
71             $data_by_key{$key}
72 0           ->@{ keys %data } = values %data;
73             }
74             elsif ($change->{operation} eq 'DELETE') {
75 0           delete $data_by_key{$key};
76             }
77             }
78 0           return [ values %data_by_key ];
79             }
80              
81              
82             sub combined_rows {
83 0     0 1   my ($self,$key_column) = @_;
84              
85             return [ map {
86 0           $self->factory_sub->($_)
  0            
87             } $self->_raw_combined_changes_data($key_column)->@* ];
88             }
89              
90             1;
91              
92             __END__