diff --git a/lib/NGCP/Schema/InflateColumn/DateTime/EpochString.pm b/lib/NGCP/Schema/InflateColumn/DateTime/EpochString.pm new file mode 100644 index 00000000..ba1580c9 --- /dev/null +++ b/lib/NGCP/Schema/InflateColumn/DateTime/EpochString.pm @@ -0,0 +1,49 @@ +package NGCP::Schema::InflateColumn::DateTime::EpochString; +use Sipwise::Base; +use DateTime qw(); + +extends 'DBIx::Class'; + +our $VERSION = '2.001'; + +__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_string } ) { + $info->{ inflate_datetime } = 'epoch_string'; + 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 "varchar" + || (exists $info->{ inflate_datetime } + && $info->{ inflate_datetime } eq 'epoch_string'); + return DateTime->from_epoch( epoch => int($value) ); +} + +sub _deflate_from_datetime { + my( $self, $value, $info, @rest ) = @_; + return $self->next::method( $value, $info, @rest ) + unless $info->{ data_type } eq "string" + || (exists $info->{ inflate_datetime } + && $info->{ inflate_datetime } eq 'epoch_string'); + return "".$value->epoch; +} + +1; + diff --git a/lib/NGCP/Schema/Result/voicemail_spool.pm b/lib/NGCP/Schema/Result/voicemail_spool.pm index e0262102..45dbe394 100644 --- a/lib/NGCP/Schema/Result/voicemail_spool.pm +++ b/lib/NGCP/Schema/Result/voicemail_spool.pm @@ -12,7 +12,11 @@ our $VERSION = '2.001'; extends 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime", "Helper::Row::ToJSON"); +__PACKAGE__->load_components( + "InflateColumn::DateTime", + "Helper::Row::ToJSON", + "+NGCP::Schema::InflateColumn::DateTime::EpochString", +); __PACKAGE__->table("kamailio.voicemail_spool"); @@ -192,7 +196,14 @@ Related object: L # Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-06-27 12:51:59 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pVE7vFt85S24avmUm7FIMg +for my $col (qw/origtime/) { + if(__PACKAGE__->has_column($col)) { + __PACKAGE__->remove_column($col); + __PACKAGE__->add_column($col => + { data_type => "varchar", is_nullable => 0, inflate_datetime => 'epoch_string' } + ); + } +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration __PACKAGE__->meta->make_immutable; 1;