From 8eda4f88b26627e679326f89a9553b1280661cf9 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:11:57 +0100 Subject: [PATCH] Fixed boolean inflation for User table --- lib/Pear/LocalLoop/Schema/Result/User.pm | 38 +++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 7c57d40..b5457c5 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -11,6 +11,7 @@ __PACKAGE__->load_components( qw/ InflateColumn::DateTime PassphraseColumn TimeStamp + FilterColumn /); __PACKAGE__->table("users"); @@ -48,7 +49,7 @@ __PACKAGE__->add_columns( }, "is_admin" => { data_type => "boolean", - default_value => \"0", + default_value => \"false", is_nullable => 0, }, ); @@ -77,6 +78,41 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +sub sqlt_deploy_hook { + my ( $source_instance, $sqlt_table ) = @_; + my $pending_field = $sqlt_table->get_field('is_admin'); + if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $pending_field->{default_value} = 0; + } else { + $pending_field->{default_value} = \"false"; + } +} + +__PACKAGE__->filter_column( is_admin => { + filter_to_storage => 'to_bool', + filter_from_storage => 'from_bool', +}); + +sub to_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val ? 1 : 0; + } else { + return $val ? 'true' : 'false'; + } +} + +sub from_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val; + } else { + return lc $val eq 'true' ? 1 : 0; + } +} + sub generate_session { my $self = shift;