From 8cc5186dab68adb94929a676b951e280b72caab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Wed, 28 Jul 2021 13:08:55 +0200 Subject: [PATCH 01/11] add scripts to backfill undelegated fields in db --- ...tgresql_db_zonemaster_backend_ver_7.0.0.pl | 52 ++++++++++++++++++- ..._sqlite_db_zonemaster_backend_ver_7.0.0.pl | 34 ++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl diff --git a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl index d08e2919b..d4d3ad250 100644 --- a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl @@ -20,8 +20,56 @@ sub patch_db { #################################################################### # TEST RESULTS #################################################################### - $dbh->do( 'ALTER TABLE test_results ADD COLUMN undelegated integer NOT NULL DEFAULT 0' ); + $dbh->do( 'ALTER TABLE test_results ADD COLUMN IF NOT EXISTS undelegated integer NOT NULL DEFAULT 0' ); + $dbh->do( qq[ +update test_results set undelegated = test_results_undelegated.undelegated_bool::int +from ( + select + test_results.id, + ( + case when ds_filled.ds_filled is null then false else ds_filled.ds_filled end + or + case when ns_filled.ns_filled is null then false else ns_filled.ns_filled end + ) as undelegated_bool + from test_results + left join ( + select + count(*) > 0 as ds_filled, + id + from ( + select + jd.value, + id + from + ( + select json_array_elements(params->'ds_info') as ja, id + from test_results + ) as s1, + json_each_text(ja) as jd + ) as s2 + where value is not null and value::text != ''::text + group by id + ) as ds_filled on ds_filled.id = test_results.id + left join ( + select + count(*) > 0 as ns_filled, + id + from ( + select + jd.value, + id + from + ( + select json_array_elements(params->'nameservers') as ja, id + from test_results + ) as s1, + json_each_text(ja) as jd + ) as s2 + where value is not null and value::text != ''::text + group by id + ) as ns_filled on ns_filled.id = test_results.id +) as test_results_undelegated where test_results.id = test_results_undelegated.id; + ] ); } patch_db(); - diff --git a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl new file mode 100644 index 000000000..2ed85c122 --- /dev/null +++ b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use utf8; +use Data::Dumper; +use Encode; +use JSON::PP; + +use DBI qw(:utils); + +use Zonemaster::Backend::Config; +use Zonemaster::Backend::DB::SQLite; + +my $config = Zonemaster::Backend::Config->load_config(); +if ( $config->DB_engine ne 'SQLite' ) { + die "The configuration file does not contain the SQLite backend"; +} +my $db = Zonemaster::Backend::DB::SQLite->from_config( $config ); +my $dbh = $db->dbh; + + +sub patch_db { + my @arefs = $dbh->selectall_array('SELECT id, params from test_results', undef); + foreach my $row (@arefs) { + my $id = @$row[0]; + my $raw_params = decode_json(@$row[1]); + my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); + my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); + my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; + + $dbh->do('UPDATE test_results SET undelegated = ? where id = ?', undef, $undelegated, $id); + } +} + +patch_db(); From eab5dfb6d5f2aaf6e1f6a8924e006ea38c6af1cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Wed, 28 Jul 2021 13:49:45 +0200 Subject: [PATCH 02/11] make the script compatible with PG 9.3 --- share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl index d4d3ad250..73a46eecd 100644 --- a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl @@ -20,7 +20,11 @@ sub patch_db { #################################################################### # TEST RESULTS #################################################################### - $dbh->do( 'ALTER TABLE test_results ADD COLUMN IF NOT EXISTS undelegated integer NOT NULL DEFAULT 0' ); + eval { + $dbh->do( 'ALTER TABLE test_results ADD COLUMN undelegated integer NOT NULL DEFAULT 0' ); + }; + print "Error while changing DB schema: " . $@; + $dbh->do( qq[ update test_results set undelegated = test_results_undelegated.undelegated_bool::int from ( From 9c7f8950614e080e0722cde0213892e14cef8636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Wed, 28 Jul 2021 15:19:53 +0200 Subject: [PATCH 03/11] add mysql patch script --- ...h_mysql_db_zonemaster_backend_ver_7.0.0.pl | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl diff --git a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl new file mode 100644 index 000000000..1cf5b79db --- /dev/null +++ b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use utf8; +use Data::Dumper; +use Encode; +use JSON::PP; + +use DBI qw(:utils); + +use Zonemaster::Backend::Config; +use Zonemaster::Backend::DB::MySQL; + +my $config = Zonemaster::Backend::Config->load_config(); +if ( $config->DB_engine ne 'MySQL' ) { + die "The configuration file does not contain the MySQL backend"; +} +my $db = Zonemaster::Backend::DB::MySQL->from_config( $config ); +my $dbh = $db->dbh; + + +sub patch_db { + my @arefs = $dbh->selectall_array('SELECT id, params from test_results', undef); + foreach my $row (@arefs) { + my $id = @$row[0]; + my $raw_params = decode_json(@$row[1]); + my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); + my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); + my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; + + $dbh->do('UPDATE test_results SET undelegated = ? where id = ?', undef, $undelegated, $id); + } +} + +patch_db(); From f9b56ef33d7c84e2238f7b6c776c843edd149bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Thu, 29 Jul 2021 10:36:12 +0200 Subject: [PATCH 04/11] update manifest --- MANIFEST | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MANIFEST b/MANIFEST index 28b38eb1a..fdeca7b03 100644 --- a/MANIFEST +++ b/MANIFEST @@ -56,9 +56,11 @@ share/patch_db_README.txt share/patch_mysql_db_zonemaster_backend_ver_1.0.3.pl share/patch_mysql_db_zonemaster_backend_ver_5.0.0.pl share/patch_mysql_db_zonemaster_backend_ver_5.0.2.pl +share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl share/patch_postgresql_db_zonemaster_backend_ver_1.0.3.pl share/patch_postgresql_db_zonemaster_backend_ver_5.0.0.pl share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl share/tmpfiles.conf share/travis_mysql_backend_config.ini share/travis_postgresql_backend_config.ini From d921fdf95f338a54ac3fdcfc3a6ee1591d941aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Thu, 29 Jul 2021 15:26:29 +0200 Subject: [PATCH 05/11] update docs --- README.md | 4 ++-- docs/upgrade_db_zonemaster_backend_ver_7.0.0.md | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f0c8b7411..f00db1726 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Zonemaster Engine installed. Please see the instructions](https://github.com/zonemaster/zonemaster-engine/blob/master/docs/Installation.md). -### Upgrade +### Upgrade If you upgrade Zonemaster-Backend and want to keep the content of the database (SQLite, MySQL/MariaDB or PostgreSQL) then you should not reset the database when @@ -40,7 +40,7 @@ Older than 1.0.3 | [Upgrade to 1.0.3] | At least 1.0.3 but older than 1.1.0 | [Upgrade to 1.1.0] | At least 1.1.0 but older than 5.0.0 | [Upgrade to 5.0.0] | At least 5.0.0 but older than 5.0.2 | [Upgrade to 5.0.2] | For MySQL/MariaDB only -At least 5.0.2 but older than 7.0.0 | [Upgrade to 7.0.0] | For PostgreSQL only +At least 5.0.2 but older than 7.0.0 | [Upgrade to 7.0.0] | For all database types If the database was created before Zonemaster-Backend version 5.0.2, then you have to upgrade it in several steps. diff --git a/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md b/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md index 7773e9642..4270e3761 100644 --- a/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md +++ b/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md @@ -12,14 +12,20 @@ export ZONEMASTER_BACKEND_CONFIG_FILE="/usr/local/etc/zonemaster/backend_config. ### SQLite -No patching (upgrading) is needed on zonemaster database on SQLite for this -version of Zonemaster-Backend. +Run +```sh +cd $(perl -MFile::ShareDir -le 'print File::ShareDir::dist_dir("Zonemaster-Backend")') +perl patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl +``` ### MySQL (or MariaDB) -No patching (upgrading) is needed on zonemaster database on MySQL (or MariaDB) -for this version of Zonemaster-Backend. +Run +```sh +cd $(perl -MFile::ShareDir -le 'print File::ShareDir::dist_dir("Zonemaster-Backend")') +perl patch_mysql_db_zonemaster_backend_ver_7.0.0.pl +``` ### PostgreSQL @@ -29,4 +35,3 @@ Run cd $(perl -MFile::ShareDir -le 'print File::ShareDir::dist_dir("Zonemaster-Backend")') perl patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl ``` - From 122aa0f326035eaa49ff106180dc7feb9a44e385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Tue, 3 Aug 2021 16:47:39 +0200 Subject: [PATCH 06/11] remove unused import --- README.md | 2 +- share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl | 3 --- share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl | 3 --- share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl | 3 --- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/README.md b/README.md index f00db1726..c7d3e908f 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Older than 1.0.3 | [Upgrade to 1.0.3] | At least 1.0.3 but older than 1.1.0 | [Upgrade to 1.1.0] | At least 1.1.0 but older than 5.0.0 | [Upgrade to 5.0.0] | At least 5.0.0 but older than 5.0.2 | [Upgrade to 5.0.2] | For MySQL/MariaDB only -At least 5.0.2 but older than 7.0.0 | [Upgrade to 7.0.0] | For all database types +At least 5.0.2 but older than 7.0.0 | [Upgrade to 7.0.0] | If the database was created before Zonemaster-Backend version 5.0.2, then you have to upgrade it in several steps. diff --git a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl index 1cf5b79db..822645005 100644 --- a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl @@ -1,8 +1,5 @@ use strict; use warnings; -use utf8; -use Data::Dumper; -use Encode; use JSON::PP; use DBI qw(:utils); diff --git a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl index 73a46eecd..41291b8b2 100644 --- a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl @@ -1,8 +1,5 @@ use strict; use warnings; -use utf8; -use Data::Dumper; -use Encode; use DBI qw(:utils); diff --git a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl index 2ed85c122..5647f4e12 100644 --- a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl @@ -1,8 +1,5 @@ use strict; use warnings; -use utf8; -use Data::Dumper; -use Encode; use JSON::PP; use DBI qw(:utils); From aea3853903200ef53c5c92989c120904d6afda2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Thu, 5 Aug 2021 11:46:19 +0200 Subject: [PATCH 07/11] only print error if exists --- share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl index 41291b8b2..67571864c 100644 --- a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl @@ -20,7 +20,9 @@ sub patch_db { eval { $dbh->do( 'ALTER TABLE test_results ADD COLUMN undelegated integer NOT NULL DEFAULT 0' ); }; - print "Error while changing DB schema: " . $@; + if ($@) { + print "Error while changing DB schema: " . $@; + } $dbh->do( qq[ update test_results set undelegated = test_results_undelegated.undelegated_bool::int From 51ee8f046247318d6b4385698d4cc8cc28e80ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Thu, 12 Aug 2021 09:49:39 +0200 Subject: [PATCH 08/11] lasy load rows --- share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl | 9 +++++---- share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl index 822645005..59615b82b 100644 --- a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl @@ -16,10 +16,11 @@ sub patch_db { - my @arefs = $dbh->selectall_array('SELECT id, params from test_results', undef); - foreach my $row (@arefs) { - my $id = @$row[0]; - my $raw_params = decode_json(@$row[1]); + my $sth1 = $dbh->prepare('SELECT id, params from test_results', undef); + $sth1->execute; + while ( my $row = $sth1->fetchrow_hashref ) { + my $id = $row->{id}; + my $raw_params = decode_json($row->{params}); my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; diff --git a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl index 5647f4e12..828d5b481 100644 --- a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl @@ -16,10 +16,11 @@ sub patch_db { - my @arefs = $dbh->selectall_array('SELECT id, params from test_results', undef); - foreach my $row (@arefs) { - my $id = @$row[0]; - my $raw_params = decode_json(@$row[1]); + my $sth1 = $dbh->prepare('SELECT id, params from test_results', undef); + $sth1->execute; + while ( my $row = $sth1->fetchrow_hashref ) { + my $id = $row->{id}; + my $raw_params = decode_json($row->{params}); my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; From 4e8ea763f41051383a817d53e2bbf3445ae70134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Mon, 16 Aug 2021 09:52:04 +0200 Subject: [PATCH 09/11] remove special instructions --- docs/upgrade_db_zonemaster_backend_ver_7.0.0.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md b/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md index 4270e3761..fd4b9d592 100644 --- a/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md +++ b/docs/upgrade_db_zonemaster_backend_ver_7.0.0.md @@ -1,15 +1,6 @@ If your zonemaster database was created by a Zonemaster-Backend version smaller than v7.0.0, and not upgraded, use the instructions in this file. -### FreeBSD - -If the installation is on FreeBSD, then set the environment before running any -of the commands below: - -```sh -export ZONEMASTER_BACKEND_CONFIG_FILE="/usr/local/etc/zonemaster/backend_config.ini" -``` - ### SQLite Run From 25625ffc5c8fa88ee089929f7852170447ede7f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Tue, 24 Aug 2021 13:20:29 +0200 Subject: [PATCH 10/11] improve code readability --- share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl | 4 ++-- share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl index 59615b82b..2f347d67e 100644 --- a/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_mysql_db_zonemaster_backend_ver_7.0.0.pl @@ -21,8 +21,8 @@ sub patch_db { while ( my $row = $sth1->fetchrow_hashref ) { my $id = $row->{id}; my $raw_params = decode_json($row->{params}); - my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); - my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); + my $ds_info_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{ds_info}}; + my $nameservers_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{nameservers}}; my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; $dbh->do('UPDATE test_results SET undelegated = ? where id = ?', undef, $undelegated, $id); diff --git a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl index 828d5b481..a8311fc08 100644 --- a/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_sqlite_db_zonemaster_backend_ver_7.0.0.pl @@ -21,8 +21,8 @@ sub patch_db { while ( my $row = $sth1->fetchrow_hashref ) { my $id = $row->{id}; my $raw_params = decode_json($row->{params}); - my $ds_info_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{ds_info}}); - my $nameservers_values = scalar( map { grep (!/^$/, values( %$_ ) ) } @{$raw_params->{nameservers}}); + my $ds_info_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{ds_info}}; + my $nameservers_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{nameservers}}; my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; $dbh->do('UPDATE test_results SET undelegated = ? where id = ?', undef, $undelegated, $id); From 0dc62fe8ab82a79010b77d7c953d6d2d7d9ba0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Wed, 1 Sep 2021 17:30:05 +0200 Subject: [PATCH 11/11] change postgres implementation --- ...tgresql_db_zonemaster_backend_ver_7.0.0.pl | 80 +++++-------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl index 67571864c..147e1394f 100644 --- a/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl +++ b/share/patch_postgresql_db_zonemaster_backend_ver_7.0.0.pl @@ -1,5 +1,7 @@ use strict; use warnings; +use JSON::PP; +use Encode; use DBI qw(:utils); @@ -10,69 +12,29 @@ if ( $config->DB_engine ne 'PostgreSQL' ) { die "The configuration file does not contain the PostgreSQL backend"; } -my $dbh = Zonemaster::Backend::DB::PostgreSQL->from_config( $config )->dbh; +my $db = Zonemaster::Backend::DB::PostgreSQL->from_config( $config ); +my $dbh = $db->dbh; + sub patch_db { + my $sth1 = $dbh->prepare('SELECT id, params from test_results', undef); + $sth1->execute; + while ( my $row = $sth1->fetchrow_hashref ) { + my $id = $row->{id}; + my $raw_params; - #################################################################### - # TEST RESULTS - #################################################################### - eval { - $dbh->do( 'ALTER TABLE test_results ADD COLUMN undelegated integer NOT NULL DEFAULT 0' ); - }; - if ($@) { - print "Error while changing DB schema: " . $@; - } + if (utf8::is_utf8($row->{params}) ) { + $raw_params = decode_json( encode_utf8 ( $row->{params} ) ); + } else { + $raw_params = decode_json( $row->{params} ); + } - $dbh->do( qq[ -update test_results set undelegated = test_results_undelegated.undelegated_bool::int -from ( - select - test_results.id, - ( - case when ds_filled.ds_filled is null then false else ds_filled.ds_filled end - or - case when ns_filled.ns_filled is null then false else ns_filled.ns_filled end - ) as undelegated_bool - from test_results - left join ( - select - count(*) > 0 as ds_filled, - id - from ( - select - jd.value, - id - from - ( - select json_array_elements(params->'ds_info') as ja, id - from test_results - ) as s1, - json_each_text(ja) as jd - ) as s2 - where value is not null and value::text != ''::text - group by id - ) as ds_filled on ds_filled.id = test_results.id - left join ( - select - count(*) > 0 as ns_filled, - id - from ( - select - jd.value, - id - from - ( - select json_array_elements(params->'nameservers') as ja, id - from test_results - ) as s1, - json_each_text(ja) as jd - ) as s2 - where value is not null and value::text != ''::text - group by id - ) as ns_filled on ns_filled.id = test_results.id -) as test_results_undelegated where test_results.id = test_results_undelegated.id; - ] ); + my $ds_info_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{ds_info}}; + my $nameservers_values = scalar grep !/^$/, map { values %$_ } @{$raw_params->{nameservers}}; + my $undelegated = $ds_info_values > 0 || $nameservers_values > 0 || 0; + + $dbh->do('UPDATE test_results SET undelegated = ? where id = ?', undef, $undelegated, $id); + } } patch_db();