diff --git a/Build.PL b/Build.PL index a4131e3b..d607a9cb 100644 --- a/Build.PL +++ b/Build.PL @@ -6,6 +6,7 @@ my $builder = Module::Build->new( dist_version_from => 'lib/NGCP/Schema.pm', requires => { 'aliased' => 0, + 'DateTime' => 0, 'DateTime::Format::MySQL' => 0, #required by DBIx::Class 'DBD::mysql' => 0, 'DBIx::Class::EncodedColumn' => 0, diff --git a/lib/NGCP/Schema/InflateColumn/DateTime/EpochMilli.pm b/lib/NGCP/Schema/InflateColumn/DateTime/EpochMilli.pm new file mode 100644 index 00000000..72231104 --- /dev/null +++ b/lib/NGCP/Schema/InflateColumn/DateTime/EpochMilli.pm @@ -0,0 +1,44 @@ +package NGCP::Schema::InflateColumn::DateTime::EpochMilli; +use Sipwise::Base; +use DateTime qw(); + +extends 'DBIx::Class'; + +__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_milli } ) { + $info->{ inflate_datetime } = 'epoch_milli'; + 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_milli'); + 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_milli'); + return $value->epoch; +} diff --git a/lib/NGCP/Schema/Result/cdr.pm b/lib/NGCP/Schema/Result/cdr.pm index e249b483..c4754fc4 100644 --- a/lib/NGCP/Schema/Result/cdr.pm +++ b/lib/NGCP/Schema/Result/cdr.pm @@ -12,7 +12,11 @@ our $VERSION = '2.000'; extends 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime", "Helper::Row::ToJSON"); +__PACKAGE__->load_components( + "InflateColumn::DateTime", + "Helper::Row::ToJSON", + "+NGCP::Schema::InflateColumn::DateTime::EpochMilli", +); __PACKAGE__->table("accounting.cdr"); @@ -635,7 +639,13 @@ NGCP::Schema::Result::cdr # Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-06-27 12:51:39 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rNMfsyIh9LAGJGoe/or06w +for my $col (qw/init_time start_time/) { + if(__PACKAGE__->has_column($col)) { + __PACKAGE__->remove_column($col); + __PACKAGE__->add_column($col => + { data_type => "decimal", is_nullable => 0, size => [13, 3], inflate_datetime => 'epoch_milli' } + ); + } +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration __PACKAGE__->meta->make_immutable; -1;