[Bast-commits] r5126 - in DBIx-Class-OptimisticLocking/1.000/trunk:
lib/DBIx/Class t t/lib/OLTest t/lib/OLTest/Schema t/var
bpphillips at dev.catalyst.perl.org
bpphillips at dev.catalyst.perl.org
Fri Nov 14 04:54:27 GMT 2008
Author: bpphillips
Date: 2008-11-14 04:54:27 +0000 (Fri, 14 Nov 2008)
New Revision: 5126
Added:
DBIx-Class-OptimisticLocking/1.000/trunk/t/02-dirty-ignored.t
DBIx-Class-OptimisticLocking/1.000/trunk/t/03-all.t
DBIx-Class-OptimisticLocking/1.000/trunk/t/04-all-ignored.t
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAllIgnored.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestDirtyIgnored.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionIgnored.pm
Modified:
DBIx-Class-OptimisticLocking/1.000/trunk/lib/DBIx/Class/OptimisticLocking.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/00-load.t
DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAll.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersion.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionAlt.pm
DBIx-Class-OptimisticLocking/1.000/trunk/t/var/oltest.sql
Log:
variety of changes
* changed terminology around a bit
* added "ignored" functionality to version strategy
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/lib/DBIx/Class/OptimisticLocking.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/lib/DBIx/Class/OptimisticLocking.pm 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/lib/DBIx/Class/OptimisticLocking.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -34,7 +34,7 @@
=head1 CONFIGURATION
-=head2 optimistic_locking_mode
+=head2 optimistic_locking_strategy
This configuration controls the main functionality of this component.
The current recognized optimistic locking modes supported are:
@@ -67,7 +67,7 @@
=back
-=head2 optimistic_locking_insignificant_dirty_columns
+=head2 optimistic_locking_ignore_columns
Occassionally you may elect to ignore certain columns that are not
significant enough to detect colisions and cause the update to fail.
@@ -77,44 +77,28 @@
=head2 optimistic_locking_version_column
-If you are using 'version' as your L<optimistic_locking_mode>, you can
+If you are using 'version' as your L<optimistic_locking_strategy>, you can
optionally specify a different name for the column used for version
tracking. If an alternate name is not passed, the component will look
for a column named C<version>.
=cut
-__PACKAGE__->mk_classdata(optimistic_locking_mode => 'dirty');
-__PACKAGE__->mk_classdata('optimistic_locking_insignificant_dirty_columns');
+__PACKAGE__->mk_classdata(optimistic_locking_strategy => 'dirty');
+__PACKAGE__->mk_classdata('optimistic_locking_ignore_columns');
__PACKAGE__->mk_classdata(optimistic_locking_version_column => 'version');
-=head1 METHODS
-
-=head2 get_original_columns
-
-Corresponds to L<DBIx::Class::Row/get_columns> except that the values
-returned reflect the original state of the object.
-
-=cut
-
-
-sub get_original_columns {
+sub _get_original_columns {
my $self = shift;
my %columns = ( $self->get_columns, %{ $self->{_opt_locking_orig_values} || {} } );
return %columns;
}
-=head2 get_original_column
-Corresponds to L<DBIx::Class::Row/get_column> except that the value
-returned reflects the original state of the object.
-
-=cut
-
-sub get_original_column {
+sub _get_original_column {
my $self = shift;
my $column = shift;
- my %columns = $self->get_original_columns;
+ my %columns = $self->_get_original_columns;
return exists $columns{$column} ? $columns{$column} : ();
}
@@ -138,8 +122,8 @@
my $track_original_values = (
(
- $self->optimistic_locking_mode eq 'dirty'
- || $self->optimistic_locking_mode eq 'all'
+ $self->optimistic_locking_strategy eq 'dirty'
+ || $self->optimistic_locking_strategy eq 'all'
)
&& !$self->is_column_changed($column)
);
@@ -174,11 +158,21 @@
# short-circuit if we're not changed
return $self if !$self->is_changed;
- if ( $self->optimistic_locking_mode eq 'version' ) {
- my $v_col = $self->optimistic_locking_version_column;
+ if ( $self->optimistic_locking_strategy eq 'version' ) {
+ # increment the version number but only if there are dirty
+ # columns that are not being ignored by the optimistic
+ # locking
- # increment the version
- $self->set_column( $v_col, $self->get_original_column($v_col) + 1 );
+ my %dirty_columns = $self->get_dirty_columns;
+
+ delete(@dirty_columns{ @{ $self->optimistic_locking_ignore_columns || [] } });
+
+ if(%dirty_columns){
+ my $v_col = $self->optimistic_locking_version_column;
+
+ # increment the version
+ $self->set_column( $v_col, $self->_get_original_column($v_col) + 1 );
+ }
}
# DBIx::Class::Row::update looks at this value, we'll precompute it
@@ -196,14 +190,14 @@
sub _optimistic_locking_ident_condition {
my $self = shift;
my $ident_condition = $self->{_orig_ident} || $self->ident_condition;
- my $mode = $self->optimistic_locking_mode;
+ my $mode = $self->optimistic_locking_strategy;
- my $insignificant = $self->optimistic_locking_insignificant_dirty_columns || [];
+ my $ignore_columns = $self->optimistic_locking_ignore_columns || [];
if ( $mode eq 'dirty' ) {
my %orig = %{$self->{_opt_locking_orig_values} || {}};
- delete($orig{$_}) foreach(@$insignificant);
+ delete($orig{$_}) foreach(@$ignore_columns);
$ident_condition = {%orig, %$ident_condition };
} elsif ( $mode eq 'version' ) {
@@ -213,8 +207,8 @@
} elsif ( $mode eq 'all' ) {
- my %orig = $self->get_original_columns;
- delete($orig{$_}) foreach(@$insignificant);
+ my %orig = $self->_get_original_columns;
+ delete($orig{$_}) foreach(@$ignore_columns);
$ident_condition = { %orig, %$ident_condition };
}
@@ -222,6 +216,7 @@
return $ident_condition;
}
+
=head1 AUTHOR
Brian Phillips, C<< <bphillips at cpan.org> >>
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/00-load.t
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/00-load.t 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/00-load.t 2008-11-14 04:54:27 UTC (rev 5126)
@@ -5,5 +5,3 @@
BEGIN {
use_ok( 'DBIx::Class::OptimisticLocking' );
}
-
-diag( "Testing DBIx::Class::OptimisticLocking $DBIx::Class::OptimisticLocking::VERSION, Perl $], $^X" );
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t 2008-11-14 04:54:27 UTC (rev 5126)
@@ -8,6 +8,7 @@
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
: ( tests => 7 );
+ $DBD::SQLite::sqlite_version; # get rid of warnings
}
use lib 't/lib';
Copied: DBIx-Class-OptimisticLocking/1.000/trunk/t/02-dirty-ignored.t (from rev 5125, DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t)
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/02-dirty-ignored.t (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/02-dirty-ignored.t 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+BEGIN {
+ eval "use DBD::SQLite";
+ plan $@
+ ? ( skip_all => 'needs DBD::SQLite for testing' )
+ : ( tests => 7 );
+ $DBD::SQLite::sqlite_version; # get rid of warnings
+}
+
+use lib 't/lib';
+
+use_ok('DBIx::Class::OptimisticLocking');
+
+use_ok( 'OLTest' );
+
+use_ok( 'OLTest::Schema' );
+
+my $s = OLTest->init_schema();
+
+my $r1 = $s->resultset('TestDirtyIgnored')->new({
+ col1 => 'a',
+ col2 => 'a',
+ col3 => 'a',
+});
+$r1->insert;
+
+my $r2 = $s->resultset('TestDirtyIgnored')->find($r1->id);
+is($r1->id, $r2->id, 'retrieved identical object');
+$r1->col3('b');
+$r2->col3('c');
+$r1->update;
+
+# won't fail because col3 is marked "ignored"
+eval {$r2->update};
+ok(!$@,'no error expected');
+
+$r2 = $s->resultset('TestDirtyIgnored')->find($r1->id);
+$r2->col1('c');
+$r2->update;
+is($r2->col1,'c', 'update succeeded');
+
+$r1->col1('d');
+eval { $r1->update; };
+ok($@, 'error expected on critical dirty column update');
Copied: DBIx-Class-OptimisticLocking/1.000/trunk/t/03-all.t (from rev 5125, DBIx-Class-OptimisticLocking/1.000/trunk/t/01-dirty.t)
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/03-all.t (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/03-all.t 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+BEGIN {
+ eval "use DBD::SQLite";
+ plan $@
+ ? ( skip_all => 'needs DBD::SQLite for testing' )
+ : ( tests => 7 );
+ $DBD::SQLite::sqlite_version; # get rid of warnings
+}
+
+use lib 't/lib';
+
+use_ok('DBIx::Class::OptimisticLocking');
+
+use_ok( 'OLTest' );
+
+use_ok( 'OLTest::Schema' );
+
+my $s = OLTest->init_schema();
+
+my $r1 = $s->resultset('TestAll')->new({
+ col1 => 'a',
+ col2 => 'a',
+ col3 => 'a',
+});
+$r1->insert;
+
+my $r2 = $s->resultset('TestAll')->find($r1->id);
+is($r1->id, $r2->id, 'retrieved identical object');
+$r1->col2('b');
+$r2->col3('c');
+$r1->update;
+
+# fails because $r2 doesn't have $r1's new col2 value
+eval {$r2->update};
+ok($@,'error expected');
+
+$r2 = $s->resultset('TestAll')->find($r1->id);
+$r2->col2('d');
+$r2->update;
+is($r2->col2,'d', 'update succeeded');
+
+$r1->col2('d');
+eval { $r1->update; };
+ok($@, 'error expected even on identical update ');
Added: DBIx-Class-OptimisticLocking/1.000/trunk/t/04-all-ignored.t
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/04-all-ignored.t (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/04-all-ignored.t 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,48 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+BEGIN {
+ eval "use DBD::SQLite";
+ plan $@
+ ? ( skip_all => 'needs DBD::SQLite for testing' )
+ : ( tests => 7 );
+ $DBD::SQLite::sqlite_version; # get rid of warnings
+}
+
+use lib 't/lib';
+
+use_ok('DBIx::Class::OptimisticLocking');
+
+use_ok( 'OLTest' );
+
+use_ok( 'OLTest::Schema' );
+
+my $s = OLTest->init_schema();
+
+my $r1 = $s->resultset('TestAllIgnored')->new({
+ col1 => 'a',
+ col2 => 'a',
+ col3 => 'a',
+});
+$r1->insert;
+
+my $r2 = $s->resultset('TestAllIgnored')->find($r1->id);
+is($r1->id, $r2->id, 'retrieved identical object');
+$r1->col3('b');
+$r2->col2('c');
+$r1->update;
+
+# passes because $r2's col3 is an ignored column when it comes to optimistic locking
+eval {$r2->update};
+ok(!$@,'error expected');
+
+$r2 = $s->resultset('TestAllIgnored')->find($r1->id);
+$r2->col2('d');
+$r2->update;
+is($r2->col2,'d', 'update succeeded');
+
+$r1->col2('d');
+eval { $r1->update; };
+ok($@, 'error expected even on identical update of non-ignored column');
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAll.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAll.pm 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAll.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -9,6 +9,7 @@
qw/ id col1 col2 col3 /
);
-__PACKAGE__->optimistic_locking_mode('all');
+__PACKAGE__->set_primary_key(qw/id/);
+__PACKAGE__->optimistic_locking_strategy('all');
1;
Added: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAllIgnored.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAllIgnored.pm (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestAllIgnored.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,16 @@
+package OLTest::Schema::TestAllIgnored;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ OptimisticLocking PK::Auto Core /);
+__PACKAGE__->table('test_all_ignored');
+__PACKAGE__->add_columns(
+ qw/ id col1 col2 col3 /
+);
+
+__PACKAGE__->set_primary_key(qw/id/);
+__PACKAGE__->optimistic_locking_strategy('all');
+__PACKAGE__->optimistic_locking_ignore_columns(['col3']);
+
+1;
Added: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestDirtyIgnored.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestDirtyIgnored.pm (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestDirtyIgnored.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,15 @@
+package OLTest::Schema::TestDirtyIgnored;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ OptimisticLocking PK::Auto Core /);
+__PACKAGE__->table('test_dirty_ignored');
+__PACKAGE__->add_columns(
+ qw/ id col1 col2 col3 /
+);
+
+__PACKAGE__->set_primary_key(qw/id/);
+__PACKAGE__->optimistic_locking_ignore_columns([qw(col3)]);
+
+1;
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersion.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersion.pm 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersion.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -7,6 +7,7 @@
__PACKAGE__->table('test_version');
__PACKAGE__->add_columns( qw/ id col1 version / );
-__PACKAGE__->optimistic_locking_mode('version');
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->optimistic_locking_strategy('version');
1;
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionAlt.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionAlt.pm 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionAlt.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -7,7 +7,8 @@
__PACKAGE__->table('test_version_alt');
__PACKAGE__->add_columns( qw/ id col1 myversion / );
-__PACKAGE__->optimistic_locking_mode('version');
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->optimistic_locking_strategy('version');
__PACKAGE__->optimistic_locking_version_column('myversion');
1;
Added: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionIgnored.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionIgnored.pm (rev 0)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema/TestVersionIgnored.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -0,0 +1,14 @@
+package OLTest::Schema::TestVersionIgnored;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ OptimisticLocking PK::Auto Core /);
+__PACKAGE__->table('test_version_ignored');
+__PACKAGE__->add_columns( qw/ id col1 col2 version / );
+
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->optimistic_locking_strategy('version');
+__PACKAGE__->optimistic_locking_ignore_columns(['col2']);
+
+1;
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema.pm
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema.pm 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/lib/OLTest/Schema.pm 2008-11-14 04:54:27 UTC (rev 5126)
@@ -3,6 +3,6 @@
use base qw/DBIx::Class::Schema/;
-__PACKAGE__->load_classes(qw/TestDirty TestDirtyInsignificant TestAll TestVersion TestVersionAlt/);
+__PACKAGE__->load_classes(qw/TestDirty TestDirtyIgnored TestAll TestAllIgnored TestVersion TestVersionAlt TestVersionIgnored/);
1;
Modified: DBIx-Class-OptimisticLocking/1.000/trunk/t/var/oltest.sql
===================================================================
--- DBIx-Class-OptimisticLocking/1.000/trunk/t/var/oltest.sql 2008-11-13 22:01:34 UTC (rev 5125)
+++ DBIx-Class-OptimisticLocking/1.000/trunk/t/var/oltest.sql 2008-11-14 04:54:27 UTC (rev 5126)
@@ -1,8 +1,10 @@
BEGIN TRANSACTION;
DROP TABLE IF EXISTS test_dirty;
-DROP TABLE IF EXISTS test_dirty_insignificant;
+DROP TABLE IF EXISTS test_dirty_ignored;
DROP TABLE IF EXISTS test_all;
+DROP TABLE IF EXISTS test_all_ignored;
DROP TABLE IF EXISTS test_version;
+DROP TABLE IF EXISTS test_version_ignored;
DROP TABLE IF EXISTS test_version_alt;
CREATE TABLE test_dirty (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -10,7 +12,7 @@
col2 TEXT NOT NULL,
col3 TEXT NOT NULL
);
-CREATE TABLE test_dirty_insignificant (
+CREATE TABLE test_dirty_ignored (
id INTEGER PRIMARY KEY AUTOINCREMENT,
col1 TEXT NOT NULL,
col2 TEXT NOT NULL,
@@ -22,11 +24,23 @@
col2 TEXT NOT NULL,
col3 TEXT NOT NULL
);
+CREATE TABLE test_all_ignored (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ col1 TEXT NOT NULL,
+ col2 TEXT NOT NULL,
+ col3 TEXT NOT NULL
+);
CREATE TABLE test_version (
id INTEGER PRIMARY KEY AUTOINCREMENT,
col1 TEXT NOT NULL,
version INTEGER NOT NULL
);
+CREATE TABLE test_version_ignored (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ col1 TEXT NOT NULL,
+ col2 TEXT NOT NULL,
+ version INTEGER NOT NULL
+);
CREATE TABLE test_version_alt (
id INTEGER PRIMARY KEY AUTOINCREMENT,
col1 TEXT NOT NULL,
More information about the Bast-commits
mailing list