-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add scripts to backfill undelegated fields in db #833
Changes from 5 commits
8cc5186
eab5dfb
9c7f895
f9b56ef
d921fdf
122aa0f
aea3853
51ee8f0
0cfcbc3
4e8ea76
25625ff
0dc62fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use strict; | ||
use warnings; | ||
use utf8; | ||
use Data::Dumper; | ||
use Encode; | ||
hannaeko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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}}); | ||
hannaeko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,60 @@ sub patch_db { | |
#################################################################### | ||
# TEST RESULTS | ||
#################################################################### | ||
$dbh->do( 'ALTER TABLE test_results ADD COLUMN 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: " . $@; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be printed only if there are errors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed, fixed. |
||
|
||
$dbh->do( qq[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be done using the much simpler for-loop from the MySQL and SQLite implementations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could but it has a impact on the performance. Processing 13K rows (about 1% of the zonemaster.net database) with the sql statement takes around 1 second and doing it with the while loop takes 17 seconds. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an impressive difference. But this is a one-time task. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I can can change it to the other implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blacksponge, have you addressed the comment from @mattias-p? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has now been addressed. |
||
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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use strict; | ||
use warnings; | ||
use utf8; | ||
use Data::Dumper; | ||
use Encode; | ||
hannaeko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest that the comment is empty when all database engines are affected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done