From: Brian Cassidy Date: Mon, 1 Jun 2009 14:25:32 +0000 (-0300) Subject: added back-compat for the "epoch => 1", etc syntax X-Git-Tag: 0.05~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=29f37a29ee0497500d375de134ae39798f75ead8;p=dbsrgits%2FDBIx-Class-DateTime-Epoch.git added back-compat for the "epoch => 1", etc syntax --- diff --git a/Changes b/Changes index a8c03b0..98e3d89 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Perl extension DBIx::Class::DateTime::Epoch. +0.05 Mon Jun 01 2009 + [ THING THAT WILL NO LONGER BREAK YOUR CODE ] + - added back-compat for the "epoch => 1", etc syntax + 0.04 Tue May 26 2009 [ THINGS THAT WILL BREAK YOUR CODE ] - refactor module to itegrate with InflateColumn::DateTime and TimeStamp. diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm index afb1414..f392c91 100644 --- a/lib/DBIx/Class/DateTime/Epoch.pm +++ b/lib/DBIx/Class/DateTime/Epoch.pm @@ -3,13 +3,30 @@ package DBIx::Class::DateTime::Epoch; use strict; use warnings; -our $VERSION = '0.04'; +our $VERSION = '0.05'; use base qw( DBIx::Class ); use DateTime; -__PACKAGE__->load_components( qw/InflateColumn::DateTime/ ); +__PACKAGE__->load_components( qw( DynamicDefault InflateColumn::DateTime ) ); + +# back compat +sub register_column { + my( $class, $col, $info ) = @_; + + if( my $type = delete $info->{ epoch } ) { + $info->{ inflate_datetime } = 'epoch'; + + if( $type =~ m{^[cm]time$} ) { + __PACKAGE__->load_components( 'TimeStamp' ); + $info->{ dynamic_default_on_create } = 'get_timestamp'; + $info->{ dynamic_default_on_update } = 'get_timestamp' if $type eq 'mtime'; + } + } + + $class->next::method( $col, $info ); +} sub _inflate_to_datetime { my( $self, $value, $info, @rest ) = @_; diff --git a/t/03-compat.t b/t/03-compat.t new file mode 100644 index 0000000..68fe75c --- /dev/null +++ b/t/03-compat.t @@ -0,0 +1,53 @@ +use strict; +use warnings; + +use lib 't/lib'; + +use Test::More tests => 15; + +use DBICx::TestDatabase; +use DateTime; +my $schema = DBICx::TestDatabase->new( 'MySchema' ); + +my $rs = $schema->resultset( 'FooCompat' ); + +{ + my $now = DateTime->now; + my $epoch = $now->epoch; + + $schema->populate( 'FooCompat', [ [ qw( id bar baz creation_time modification_time ) ], [ 1, ( $epoch ) x 4 ] ] ); + + { + my $row = $rs->find( 1 ); + + isa_ok( $row->bar, 'DateTime' ); + isa_ok( $row->baz, 'DateTime' ); + ok( $row->bar == $now, 'inflate: epoch as int' ); + ok( $row->baz == $now, 'inflate: epoch as varchar' ); + } + + { + $rs->create( { bar => $now, baz => $now } ); + my $row = $rs->find( 2 ); + + isa_ok( $row->bar, 'DateTime' ); + isa_ok( $row->baz, 'DateTime' ); + is( $row->get_column( 'bar' ), $epoch, 'deflate: epoch as int' ); + is( $row->get_column( 'baz' ), $epoch, 'deflate: epoch as varchar' ); + + # courtesy of TimeStamp + isa_ok( $row->creation_time, 'DateTime' ); # courtesy of TimeStamp + isa_ok( $row->modification_time, 'DateTime' ); + like( $row->get_column( 'creation_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + + my $mtime = $row->modification_time; + sleep( 1 ); + $row->update( { name => 'test' } ); + + $row = $rs->find( 2 ); + isa_ok( $row->modification_time, 'DateTime' ); + like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + ok( $row->modification_time > $mtime, 'mtime column was updated' ); + } +} diff --git a/t/lib/MySchema/FooCompat.pm b/t/lib/MySchema/FooCompat.pm new file mode 100644 index 0000000..a64494c --- /dev/null +++ b/t/lib/MySchema/FooCompat.pm @@ -0,0 +1,43 @@ +package MySchema::FooCompat; + +use strict; +use warnings; + +use base qw( DBIx::Class ); + +__PACKAGE__->load_components( qw( DateTime::Epoch Core ) ); +__PACKAGE__->table( 'foo_compat' ); +__PACKAGE__->add_columns( + id => { + data_type => 'bigint', + is_auto_increment => 1, + is_nullable => 0, + }, + name => { + data_type => 'varchar', + size => 10, + is_nullable => 1, + }, + bar => { # epoch stored as an int + data_type => 'bigint', + epoch => 1, + }, + baz => { # epoch stored as a string + data_type => 'varchar', + size => 50, + epoch => 1, + }, + # working in conjunction with DBIx::Class::TimeStamp + creation_time => { + data_type => 'bigint', + epoch => 'ctime', + }, + modification_time => { + data_type => 'bigint', + epoch => 'mtime', + } +); + +__PACKAGE__->set_primary_key( 'id' ); + +1;