From d2b769d4717e2ab1e81eccb909cea4b138bff8bb Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:10:34 +0100 Subject: [PATCH 1/7] Auto inflate correctly based on database type for booleans --- .../LocalLoop/Schema/Result/Organisation.pm | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm index a178e07..aa75de7 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime"); +__PACKAGE__->load_components("InflateColumn::DateTime", "FilterColumn"); __PACKAGE__->table("organisations"); @@ -51,7 +51,7 @@ __PACKAGE__->add_columns( }, pending => { data_type => 'boolean', - default_value => \"0", + default => \"false", is_nullable => 0, }, submitted_by_id => { @@ -68,4 +68,40 @@ __PACKAGE__->belongs_to( "entity_id", ); +__PACKAGE__->filter_column( pending => { + filter_to_storage => 'to_bool', + filter_from_storage => 'from_bool', +}); + +# Only works when calling ->deploy, but atleast helps for tests +sub sqlt_deploy_hook { + my ( $source_instance, $sqlt_table ) = @_; + my $pending_field = $sqlt_table->get_field('pending'); + if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $pending_field->{default_value} = 0; + } else { + $pending_field->{default_value} = \"false"; + } +} + +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; + } +} + 1; From 8eda4f88b26627e679326f89a9553b1280661cf9 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:11:57 +0100 Subject: [PATCH 2/7] 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; From 6ed0be4801d1e66d8c2cc1ef26725e90ec4933c7 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:12:36 +0100 Subject: [PATCH 3/7] Use normal 1 instead of deferenced for proper inflation --- lib/Pear/LocalLoop/Controller/Api/Upload.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 87a3fba..7b037d5 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -150,7 +150,7 @@ sub post_upload { street_name => $validation->param('street_name'), town => $validation->param('town'), postcode => $validation->param('postcode'), - pending => \"1" + pending => 1, }); $organisation = $entity->organisation; } From 2c718e4337974f87df312a5c65848990dd572f2e Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:13:00 +0100 Subject: [PATCH 4/7] Fix layout issue in upload test and change to using pure json --- t/api/upload.t | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/t/api/upload.t b/t/api/upload.t index fb64136..74b3409 100644 --- a/t/api/upload.t +++ b/t/api/upload.t @@ -201,12 +201,11 @@ $json = { organisation_id => 1, session_key => $session_key, }; -$upload = {json => Mojo::JSON::encode_json($json)}; -$t->post_ok('/api/upload' => form => $upload ) -->status_is(200) -->or($framework->dump_error) -->json_is('/success', Mojo::JSON->true) -->json_like('/message', qr/Upload Successful/); +$t->post_ok('/api/upload' => json => $json ) + ->status_is(200) + ->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true) + ->json_like('/message', qr/Upload Successful/); is $schema->resultset('Transaction')->count, 1, "1 transaction"; print "test 13 - organisation_id missing (type 1: already validated)\n"; From 681ccf137008d6259417568ecfc0a7ece6fe7dc4 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:14:11 +0100 Subject: [PATCH 5/7] Change defaults to be pure false --- share/ddl/_source/deploy/6/001-auto.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/ddl/_source/deploy/6/001-auto.yml b/share/ddl/_source/deploy/6/001-auto.yml index 2448ed4..6d9e363 100644 --- a/share/ddl/_source/deploy/6/001-auto.yml +++ b/share/ddl/_source/deploy/6/001-auto.yml @@ -652,7 +652,7 @@ schema: pending: data_type: boolean default_value: !!perl/ref - =: 0 + =: false is_nullable: 0 is_primary_key: 0 is_unique: 0 @@ -1000,7 +1000,7 @@ schema: is_admin: data_type: boolean default_value: !!perl/ref - =: 0 + =: false is_nullable: 0 is_primary_key: 0 is_unique: 0 From 2320ec7e2391b41e5ece675fb7fa6c5dad3e6c28 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:15:07 +0100 Subject: [PATCH 6/7] Change postgres deploy to use false for boolean default value --- share/ddl/PostgreSQL/deploy/6/001-auto.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/share/ddl/PostgreSQL/deploy/6/001-auto.sql b/share/ddl/PostgreSQL/deploy/6/001-auto.sql index 0d97746..21223b0 100644 --- a/share/ddl/PostgreSQL/deploy/6/001-auto.sql +++ b/share/ddl/PostgreSQL/deploy/6/001-auto.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::PostgreSQL --- Created on Fri Sep 1 15:14:28 2017 +-- Created on Tue Sep 5 17:22:16 2017 -- ; -- @@ -76,7 +76,7 @@ CREATE TABLE "organisations" ( "postcode" character varying(16), "country" character varying(255), "sector" character varying(1), - "pending" boolean DEFAULT 0 NOT NULL, + "pending" boolean DEFAULT false NOT NULL, "submitted_by_id" integer, PRIMARY KEY ("id") ); @@ -109,7 +109,7 @@ CREATE TABLE "users" ( "email" text NOT NULL, "join_date" timestamp NOT NULL, "password" character varying(100) NOT NULL, - "is_admin" boolean DEFAULT 0 NOT NULL, + "is_admin" boolean DEFAULT false NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email" UNIQUE ("email") ); From f3335810b3a54bea6a628bd2afaa54d2e5e201dc Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 6 Sep 2017 16:16:35 +0100 Subject: [PATCH 7/7] Change postgres upgrade script to use proper boolean defaults --- share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql b/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql index c970017..f6c5187 100644 --- a/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql +++ b/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql @@ -34,7 +34,7 @@ CREATE TABLE "organisations" ( "postcode" character varying(16), "country" character varying(255), "sector" character varying(1), - "pending" boolean DEFAULT 0 NOT NULL, + "pending" boolean DEFAULT false NOT NULL, "submitted_by_id" integer, PRIMARY KEY ("id") ); @@ -59,7 +59,7 @@ CREATE TABLE "users" ( "email" text NOT NULL, "join_date" timestamp NOT NULL, "password" character varying(100) NOT NULL, - "is_admin" boolean DEFAULT 0 NOT NULL, + "is_admin" boolean DEFAULT false NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email" UNIQUE ("email") );