From 4be5a07e8657a1b72106afa6766b5fd55f044f02 Mon Sep 17 00:00:00 2001 From: Al Beano Date: Sat, 9 Sep 2017 23:21:04 +0100 Subject: [PATCH] Don't keep updating the SOA every minute, it's not nice --- bin/writezone.pl | 21 +++++++++++++++------ dbupdate/6.sql | 3 +++ lib/cyberman/Helper.pm | 18 +++++++++++++++++- lib/cyberman/Records.pm | 4 ++++ schema.sql | 6 ++++-- 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 dbupdate/6.sql diff --git a/bin/writezone.pl b/bin/writezone.pl index 82db75c..7f3a2f8 100755 --- a/bin/writezone.pl +++ b/bin/writezone.pl @@ -15,6 +15,16 @@ my $yml = YAML::Tiny->read("$Bin/../config.yml"); my $tld = $yml->[0]->{"tld"}; my $conf = $yml->[0]->{"zonewriter"}; +die "Unsupported database!" + unless $yml->[0]->{"plugins"}->{"Database"}->{"driver"} eq "SQLite"; +my $dbfile = "$Bin/../$yml->[0]->{plugins}->{Database}->{dbname}"; +my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", ""); + +my $sth = $dbh->prepare("SELECT * from cyberman"); +$sth->execute; +my $cyberman = $sth->fetchrow_hashref; +exit unless $cyberman->{"intserial"} > $cyberman->{"lastserial"}; + open my $out, ">", $conf->{"file"} or die $!; # Introduction @@ -41,12 +51,7 @@ if ($conf->{"include"}->{"enabled"}) { } # Time to get the records -die "Unsupported database!" - unless $yml->[0]->{"plugins"}->{"Database"}->{"driver"} eq "SQLite"; -my $dbfile = "$Bin/../$yml->[0]->{plugins}->{Database}->{dbname}"; -my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", ""); - -my $sth = $dbh->prepare("SELECT * FROM record"); +$sth = $dbh->prepare("SELECT * FROM record"); $sth->execute; while (my $r = $sth->fetchrow_hashref) { @@ -71,3 +76,7 @@ while (my $r = $sth->fetchrow_hashref) { } close $out; + +$sth = $dbh->prepare("UPDATE cyberman SET lastserial=?"); +$sth->bind_param(1, $cyberman->{"intserial"}); +$sth->execute; diff --git a/dbupdate/6.sql b/dbupdate/6.sql new file mode 100644 index 0000000..efd57f0 --- /dev/null +++ b/dbupdate/6.sql @@ -0,0 +1,3 @@ +alter table cyberman add column intserial integer not null default 1; +alter table cyberman add column lastserial integer not null default 0; +update cyberman set dbrev=7; diff --git a/lib/cyberman/Helper.pm b/lib/cyberman/Helper.pm index f814243..9af3bd2 100644 --- a/lib/cyberman/Helper.pm +++ b/lib/cyberman/Helper.pm @@ -1,6 +1,7 @@ package cyberman::Helper; use base qw(Exporter); use Dancer2 appname => "cyberman"; +use Dancer2::Plugin::Database; use Math::Random::Secure qw(irand); use Digest::Bcrypt; @@ -10,7 +11,7 @@ use Email::Simple::Creator; use Exporter qw(import); -our @EXPORT = qw(auth_test randstring hash_password check_name send_email); +our @EXPORT = qw(auth_test randstring hash_password check_name send_email incr_serial); # Helper functions @@ -97,4 +98,19 @@ sub send_email { sendmail($email) if config->{"mail"}->{"enabled"}; } +sub incr_serial { + my $cyberman = database->quick_select( + "cyberman", + {}, + ); + database->quick_update( + "cyberman", + {}, + { + "intserial" => $cyberman->{"intserial"} + 1, + }, + ); + return 1; +} + 1; diff --git a/lib/cyberman/Records.pm b/lib/cyberman/Records.pm index 05f0cf2..f46a476 100644 --- a/lib/cyberman/Records.pm +++ b/lib/cyberman/Records.pm @@ -126,6 +126,8 @@ post '/domains/:name/records/add' => sub { }, ); + incr_serial(); + template 'redir' => { "redir" => "../records?added=1", }; @@ -165,6 +167,8 @@ post '/domains/:name/records/:sid/remove' => sub { }, ); + incr_serial(); + template 'redir' => { "redir" => "../../records?removed=1", }; diff --git a/schema.sql b/schema.sql index 87c9860..acc5961 100644 --- a/schema.sql +++ b/schema.sql @@ -1,9 +1,11 @@ drop table if exists cyberman; create table cyberman ( id integer primary key, - dbrev integer not null + dbrev integer not null, + intserial integer not null default 1, + lastserial integer not null default 0 ); -insert into cyberman (dbrev) values (6); +insert into cyberman (dbrev) values (7); drop table if exists user; create table user (