| 1 | package DBIO::ChangeLog::Schema; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Schema-level change tracking component | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 12 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use base 'DBIO::Schema'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use DBIO::ChangeLog::Set; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use DBIO::ChangeLog::Entry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use DBIO::ResultSource::Table; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | __PACKAGE__->mk_classdata('changelog_sources'); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 12 | __PACKAGE__->mk_group_accessors(simple => qw/ | |||||||||||||||||||||||||||
| 19 | _changelog_user | ||||||||||||||||||||||||||||
| 20 | _changelog_session | ||||||||||||||||||||||||||||
| 21 | _changelog_disabled_flag | ||||||||||||||||||||||||||||
| 22 | _changelog_current_changeset_id | ||||||||||||||||||||||||||||
| 23 | _changelog_sources_registered | ||||||||||||||||||||||||||||
| 24 | /); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 14 | ▶ | sub changelog_user { | ||||||||||||||||||||||||||
|
Subroutine
changelog_user: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 29 | 14 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 3 | $self->_changelog_user($_[0]); | |||||||||||||||||||||||||||
| 31 | 3 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 11 | return $self->_changelog_user; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 14 | ▶ | sub changelog_session { | ||||||||||||||||||||||||||
|
Subroutine
changelog_session: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 14 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | $self->_changelog_session($_[0]); | |||||||||||||||||||||||||||
| 41 | 3 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 11 | return $self->_changelog_session; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 114 | ▶ | sub changelog_disabled { | ||||||||||||||||||||||||||
|
Subroutine
changelog_disabled: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 114 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 114 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 8 | $self->_changelog_disabled_flag($_[0]); | |||||||||||||||||||||||||||
| 51 | 8 | return $self; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 106 | return $self->_changelog_disabled_flag; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 22 | ▶ | sub connection { | ||||||||||||||||||||||||||
|
Subroutine
connection: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 22 | my $ret = $self->next::method(@_); | |||||||||||||||||||||||||||
| 60 | 22 | ▶ | $ret->_register_changelog_sources | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | unless $ret->_changelog_sources_registered; | ||||||||||||||||||||||||||||
| 62 | 22 | return $ret; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub txn_do { | ||||||||||||||||||||||||||
|
Subroutine
txn_do: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 16 | my ($self, $coderef, @args) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # If changelog is disabled or we already have a changeset (nested), | ||||||||||||||||||||||||||||
| 70 | # just delegate | ||||||||||||||||||||||||||||
| 71 | 16 | ▶ | if ($self->changelog_disabled || $self->_changelog_current_changeset_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->changelog_disabled or $self->_changelog_current_changeset_id
MC/DC100%2/2
$self->changelog_disabled or $self->_changelog_current_changeset_id
$self->changelog_disabled
$self->_changelog_current_changeset_id
Truth table100%3/3
A or B
A= $self->changelog_disabledB= $self->_changelog_current_changeset_id
| |||||||||||||||||||||||||||||
| 72 | 5 | return $self->next::method($coderef, @args); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Wrap: create changeset, set as current, run coderef | ||||||||||||||||||||||||||||
| 76 | 11 | my $outer = $self; | |||||||||||||||||||||||||||
| 77 | 11 | ▶ | return $self->next::method(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | # Create the changeset row | ||||||||||||||||||||||||||||
| 79 | 11 | my $cs = $outer->resultset('ChangeLog_Set')->create({ | |||||||||||||||||||||||||||
| 80 | user_id => $outer->changelog_user, | ||||||||||||||||||||||||||||
| 81 | session_id => $outer->changelog_session, | ||||||||||||||||||||||||||||
| 82 | created_at => _now(), | ||||||||||||||||||||||||||||
| 83 | }); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Use local so rollback naturally clears this | ||||||||||||||||||||||||||||
| 86 | 11 | local $outer->{_changelog_current_changeset_id} = $cs->id; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 11 | return $coderef->(@args); | |||||||||||||||||||||||||||
| 89 | 11 | }); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub deploy_changelog { | ||||||||||||||||||||||||||
|
Subroutine
deploy_changelog: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | my @tables; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | if (my $set_source = $self->source('ChangeLog_Set')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | push @tables, $set_source; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | for my $source_name ($self->sources) { | |||||||||||||||||||||||||||
| 103 | 21 | ▶ | next if $source_name =~ /_ChangeLog$/ || $source_name eq 'ChangeLog_Set'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$source_name =~ /_ChangeLog$/ or $source_name eq "ChangeLog_Set"
MC/DC100%2/2
$source_name =~ /_ChangeLog$/ or $source_name eq "ChangeLog_Set"
$source_name =~ /_ChangeLog$/
$source_name eq "ChangeLog_Set"
Truth table100%3/3
A or B
A= $source_name =~ /_ChangeLog$/B= $source_name eq "ChangeLog_Set"
| |||||||||||||||||||||||||||||
| 104 | 10 | ▶ | if (my $cl = $self->source($source_name . '_ChangeLog')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 10 | push @tables, $cl; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | for my $table (@tables) { | |||||||||||||||||||||||||||
| 110 | 11 | $self->storage->deploy($table); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | return $self; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # ---- Internal ---- | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 44 | ▶ | sub _register_changelog_sources { | ||||||||||||||||||||||||||
|
Subroutine
_register_changelog_sources: called CC=9 SCAR=23.9
| |||||||||||||||||||||||||||||
| 119 | 44 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Register changelog_set source | ||||||||||||||||||||||||||||
| 122 | 44 | my $set_def = DBIO::ChangeLog::Set->source_definition; | |||||||||||||||||||||||||||
| 123 | 44 | my $set_source = $self->_build_changelog_source( | |||||||||||||||||||||||||||
| 124 | 'ChangeLog_Set', $set_def, | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | 44 | $self->register_extra_source('ChangeLog_Set', $set_source); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Register per-source changelog tables | ||||||||||||||||||||||||||||
| 129 | 44 | my @source_names = $self->sources; | |||||||||||||||||||||||||||
| 130 | 44 | my $tracked = $self->changelog_sources; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 44 | for my $source_name (@source_names) { | |||||||||||||||||||||||||||
| 133 | # Skip changelog sources themselves | ||||||||||||||||||||||||||||
| 134 | 123 | ▶ | next if $source_name =~ /_ChangeLog$/ || $source_name eq 'ChangeLog_Set'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$source_name =~ /_ChangeLog$/ or $source_name eq "ChangeLog_Set"
MC/DC100%2/2
$source_name =~ /_ChangeLog$/ or $source_name eq "ChangeLog_Set"
$source_name =~ /_ChangeLog$/
$source_name eq "ChangeLog_Set"
Truth table100%3/3
A or B
A= $source_name =~ /_ChangeLog$/B= $source_name eq "ChangeLog_Set"
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # If tracked sources are specified, skip untracked ones | ||||||||||||||||||||||||||||
| 137 | 56 | ▶ | if ($tracked && @$tracked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tracked and @$tracked
MC/DC0%0/2
$tracked and @$tracked
$tracked
@$tracked
Truth table33%1/3
A and B
A= $trackedB= @$tracked
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | next unless grep { $_ eq $source_name } @$tracked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 56 | my $orig_source = $self->source($source_name); | |||||||||||||||||||||||||||
| 142 | 56 | my $table_name = $orig_source->name; | |||||||||||||||||||||||||||
| 143 | 56 | ▶ | next unless defined $table_name && length $table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $table_name and length $table_name
MC/DC0%0/2
defined $table_name and length $table_name
defined $table_name
length $table_name
Truth table33%1/3
A and B
A= defined $table_nameB= length $table_name
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 56 | my $entry_def = DBIO::ChangeLog::Entry->source_definition( | |||||||||||||||||||||||||||
| 146 | table => $table_name, | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | 56 | my $entry_source = $self->_build_changelog_source( | |||||||||||||||||||||||||||
| 149 | $source_name . '_ChangeLog', $entry_def, | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | 56 | $self->register_extra_source( | |||||||||||||||||||||||||||
| 152 | $source_name . '_ChangeLog', $entry_source, | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 44 | $self->_changelog_sources_registered(1); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 100 | ▶ | sub _build_changelog_source { | ||||||||||||||||||||||||||
|
Subroutine
_build_changelog_source: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 160 | 100 | my ($self, $source_name, $def) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 100 | my $result_class = "DBIO::ChangeLog::Result::$source_name"; | |||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||
| 164 | 12 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | 100 | @{"${result_class}::ISA"} = ('DBIO::Core'); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 100 | my $source = DBIO::ResultSource::Table->new({ | |||||||||||||||||||||||||||
| 169 | source_name => $source_name, | ||||||||||||||||||||||||||||
| 170 | result_class => $result_class, | ||||||||||||||||||||||||||||
| 171 | }); | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 100 | $source->name($def->{table}); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # Add columns in order | ||||||||||||||||||||||||||||
| 176 | 100 | for my $col (@{ $def->{column_order} }) { | |||||||||||||||||||||||||||
| 177 | 512 | $source->add_columns($col => $def->{columns}{$col}); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 100 | $source->set_primary_key(@{ $def->{primary_key} }); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 100 | $result_class->mk_classdata('result_source_instance'); | |||||||||||||||||||||||||||
| 183 | 100 | $result_class->result_source_instance($source); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 100 | return $source; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 11 | ▶ | sub _now { | ||||||||||||||||||||||||||
|
Subroutine
_now: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 189 | 11 | require POSIX; | |||||||||||||||||||||||||||
| 190 | 11 | return POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 12 | 1; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||