/usr/share/perl5/Catmandu/Plugin/Versioning.pm is in libcatmandu-perl 0.9505-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Catmandu::Plugin::Versioning;
use Catmandu::Sane;
our $VERSION = '0.9505';
use Catmandu::Util qw(is_value is_array_ref check_value check_positive);
use Data::Compare;
use Moo::Role;
use namespace::clean;
has version_bag => (
is => 'lazy',
);
has version_compare_ignore => (
is => 'lazy',
coerce => sub {
my $keys = $_[0];
$keys = [@$keys] if is_array_ref $keys;
$keys = [split /,/, $keys] if is_value $keys;
push @$keys, '_version' unless grep /^_version$/, @$keys;
$keys;
},
);
has version_transfer => (
is => 'lazy',
coerce => sub {
my $keys = $_[0];
$keys = [@$keys] if is_array_ref $keys;
$keys = [split /,/, $keys] if is_value $keys;
$keys;
},
);
sub _build_version_bag {
$_[0]->store->bag($_[0]->name . '_version');
}
sub _build_version_compare_ignore {
[qw(_version)];
}
sub _build_version_transfer {
[];
}
around add => sub {
my ($sub, $self, $data) = @_;
if (defined $data->{_id} and my $d = $self->get($data->{_id})) {
$data->{_version} = $d->{_version} ||= 1;
for my $key (@{$self->version_transfer}) {
next if exists $data->{$key} || !exists $d->{$key};
$data->{$key} = $d->{$key};
}
return $data
if Compare($data, $d, {ignore_hash_keys => $self->version_compare_ignore});
$self->version_bag->add({_id => "$data->{_id}.$data->{_version}", data => $d});
$data->{_version}++;
} else {
$data->{_version} ||= 1;
}
$sub->($self, $data);
};
sub get_history {
my ($self, $id, %opts) = @_;
if (my $data = $self->get($id)) {
my $history = [$data];
my $version = $data->{_version} || 1;
while (--$version) {
push @$history, $self->get_version($id, $version);
}
return $history;
}
return;
}
sub get_version {
my ($self, $id, $version) = @_;
check_value($id);
check_positive($version);
my $data = $self->version_bag->get("$id.$version") || return;
$data->{data};
}
sub restore_version {
my ($self, $id, $version) = @_;
if (my $data = $self->get_version($id, $version)) {
return $self->add($data);
}
return;
}
sub get_previous_version {
my ($self, $id) = @_;
if (my $data = $self->get($id)) {
my $version = $data->{_version} || 1;
if ($version > 1) {
return $self->get_version($id, $version - 1);
}
}
return;
}
sub restore_previous_version {
my ($self, $id) = @_;
if (my $data = $self->get_previous_version($id)) {
return $self->add($data);
}
return;
}
1;
__END__
=pod
=head1 NAME
Catmandu::Plugin::Versioning - Automatically adds versioning to Catmandu::Store records
=head1 SYNOPSIS
# Using configuration files
$ cat catmandu.yml
---
store:
test:
package: MongoDB
options:
database_name: test
bags:
data:
plugins:
- Versioning
# Add two version of record 001 to the store
$ echo '{"_id":"001",hello":"world"}' | catmandu import JSON to test
$ echo '{"_id":"001",hello":"world2"}' | catmandu import JSON to test
# In the store we see only the latest version
$ catmandu export test to YAML
---
_id: '001'
_version: 2
hello: world2
# In the '_version' store we'll find all the previous versions
$ catmandu export test --bag data_version to YAML
---
_id: '001.1'
data:
_id: '001'
_version: 1
hello: world
# Or in your Perl program
my $store = Catmandu::Store::MongoDB->new(
database_name => 'test' ,
bags => {
data => {
plugins => [qw(Versioning)]
}
});
$store->bag->add({ _id => '001' , hello => 'world'});
$store->bag->add({ _id => '001' , hello => 'world2'});
print "Versions:\n";
for (@{$store->bag->get_history('001')}) {
print Dumper($_);
}
=head1 DESCRIPTION
The Catmandu::Plugin::Versioning plugin automatically adds a new 'version' bag to your Catmandu::Store
containing previous versions of newly created records. The name of the version is created by appending
'_version' to your original bag name. E.g. when add the Versioning plugin to a 'test' bag then 'test_version'
will contain the previous version of all your records.
When using Catmandu::Store-s that don't have dynamic schema's (e.g. Solr , DBI) these new bags need to be
predefined (e.g. create new Solr cores or database tables).
=head1 METHODS
Every bag that is configured with the Catmandu::Plugin::Versioning plugin can use the following methods:
=head2 get_version(ID,VERSION)
Retrieve a record with identifier ID and version identifier VERSION. E.g.
my $obj = $store->bag('test')->get_version('001',1);
=head2 get_previous_version(ID)
Retrieve the previous version of a record with identifier ID. E.g.
=head2 get_history(ID)
Returns an ARRAY reference with all the versions of the record with identifier ID.
=head2 restore_version(ID,VERSION)
Overwrites the current version of the stored record with identifier ID with a version with identifier VERSION.
=head2 restore_previous_version(ID)
Overwrites the current version of the stored record with identifier ID with its previous version.
=head1 OPTIONS
=head2 version_compare_ignore
By default every change to a record with trigger the creation of a new version. Use the version_compare_ignore option
to specify fields that should be ignored when testing for new updates. E.g. in the example below we configured the
MongoDB store to add versioning to the default 'data' bag. We want to ignore changes to the 'date_updated' field
when creating new version records
# catmandu.yml
---
store:
test:
package: MongoDB
options:
database_name: test
bags:
data:
plugins:
- Versioning
version_compare_ignore:
- date_updated
# In your perl
# First version
$store->bag->add({ _id => '001' , name => 'test' , date_updated => '10:00' });
# Second version (name has changed)
$store->bag->add({ _id => '001' , name => 'test123' , date_updated => '10:00' });
# Second version (date_updated has changed but we ignored that in our configuration)
$store->bag->add({ _id => '001' , name => 'test123' , date_updated => '10:15' });
=head2 version_transfer
This option autmatically copies the configured fields from the previous version of a record to the new version of the
record. E.g. in the example below we will create a versioning on the default bag and add a rights statement that can
not be deleted.
# catmandu.yml
---
store:
test:
package: MongoDB
options:
database_name: test
bags:
data:
plugins:
- Versioning
version_transfer:
- rights:
# In your perl
# First version
$store->bag->add({ _id => '001' , name => 'test' , rights => 'Acme Corp.' });
# Second version we will try you delete rights but this is copied to the new version
$store->bag->add({ _id => '001' , name => 'test'});
print "Rights: %s\n" , $store->bag->get('001')->{rights}; # Rights: Acme Corp.
=head1 SEE ALSO
L<Catmandu::Store>, L<Catmandu::Bag>
=cut
|