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 ) = @_;
--- /dev/null
+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' );
+ }
+}
--- /dev/null
+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;