diff --git a/lib/NGCP/Schema/InflateColumn/DateTime/EpochMicro.pm b/lib/NGCP/Schema/InflateColumn/DateTime/EpochMicro.pm new file mode 100644 index 00000000..f30f6d0c --- /dev/null +++ b/lib/NGCP/Schema/InflateColumn/DateTime/EpochMicro.pm @@ -0,0 +1,46 @@ +package NGCP::Schema::InflateColumn::DateTime::EpochMicro; +use Sipwise::Base; +use DateTime qw(); + +extends 'DBIx::Class'; + +our $VERSION = '2.003'; + +__PACKAGE__->load_components(qw(InflateColumn::DateTime)); + +# back compat +sub add_columns { + my ($class, @cols) = @_; + my @columns; + while (my $col = shift @cols) { + my $info = ref $cols[0] ? shift @cols : {}; + if( my $type = delete $info->{ epoch_micro } ) { + $info->{ inflate_datetime } = 'epoch_micro'; + if( $type =~ m{^[cm]time$} ) { + __PACKAGE__->load_components( 'TimeStamp' ); + $info->{ set_on_create } = 1; + $info->{ set_on_update } = 1 if $type eq 'mtime'; + } + } + push @columns, $col => $info; + } + $class->next::method(@columns); +} + +sub _inflate_to_datetime { + my ($self, $value, $info, @rest) = @_; + return $self->next::method($value, $info, @rest) + unless $info->{data_type} eq "decimal" + || (exists $info->{inflate_datetime} + && $info->{inflate_datetime} eq 'epoch_micro'); + return DateTime->from_epoch(epoch => $value); +} + +sub _deflate_from_datetime { + my ($self, $value, $info, @rest) = @_; + return $self->next::method($value, $info, @rest) + unless $info->{data_type} eq "decimal" + || (exists $info->{inflate_datetime} + && $info->{inflate_datetime} eq 'epoch_micro'); + return $value->epoch; +} diff --git a/lib/NGCP/Schema/Result/messages.pm b/lib/NGCP/Schema/Result/messages.pm index 497a817a..5694c230 100644 --- a/lib/NGCP/Schema/Result/messages.pm +++ b/lib/NGCP/Schema/Result/messages.pm @@ -15,6 +15,7 @@ extends 'DBIx::Class::Core'; __PACKAGE__->load_components( "InflateColumn::DateTime", "Helper::Row::ToJSON", + "+NGCP::Schema::InflateColumn::DateTime::EpochMicro", ); @@ -219,5 +220,14 @@ NGCP::Schema::Result::messages # Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-06-27 12:52:12 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bYbdYKBz9w7LgX1fXS2wzQ +for my $col (qw/timestamp/) { + if(__PACKAGE__->has_column($col)) { + __PACKAGE__->remove_column($col); + __PACKAGE__->add_column($col => + { data_type => "decimal", is_nullable => 0, size => [17, 6], inflate_datetime => 'epoch_micro' } + ); + } +} + __PACKAGE__->meta->make_immutable; 1;