Skip to content

Commit dbfe6e4

Browse files
committed
Add adjust_conf method to PostgresNode
This method will modify or delete an existing line in the config file rather than simply appending to the file. This makes adjustment of files for older versions much simpler and more compact.
1 parent b33259e commit dbfe6e4

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/test/perl/PostgresNode.pm

+48-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ PostgresNode - class representing PostgreSQL server instance
1919
# Start the PostgreSQL server
2020
$node->start();
2121
22-
# Change a setting and restart
22+
# Add a setting and restart
2323
$node->append_conf('postgresql.conf', 'hot_standby = on');
2424
$node->restart();
2525
26+
# Modify or delete an existing setting
27+
$node->adjust_conf('postgresql.conf', 'max_wal_senders', '10');
28+
2629
# run a query with psql, like:
2730
# echo 'SELECT 1' | psql -qAXt postgres -v ON_ERROR_STOP=1
2831
$psql_stdout = $node->safe_psql('postgres', 'SELECT 1');
@@ -544,6 +547,50 @@ sub append_conf
544547

545548
=pod
546549
550+
=item $node->adjust_conf(filename, setting, value, skip_equals)
551+
552+
Modify the named config file setting with the value. If the value is undefined,
553+
instead delete the setting. If the setting is not present no action is taken.
554+
555+
This will write "$setting = $value\n" in place of the existing line,
556+
unless skip_equals is true, in which case it will write
557+
"$setting $value\n". If the value needs to be quoted it is the caller's
558+
responsibility to do that.
559+
560+
=cut
561+
562+
sub adjust_conf
563+
{
564+
my ($self, $filename, $setting, $value, $skip_equals) = @_;
565+
566+
my $conffile = $self->data_dir . '/' . $filename;
567+
568+
my $contents = TestLib::slurp_file($conffile);
569+
my @lines = split(/\n/, $contents);
570+
my @result;
571+
my $eq = $skip_equals ? '' : '= ';
572+
foreach my $line (@lines)
573+
{
574+
if ($line !~ /^$setting\W/)
575+
{
576+
push(@result, "$line\n");
577+
}
578+
elsif (defined $value)
579+
{
580+
push(@result, "$setting $eq$value\n");
581+
}
582+
}
583+
open my $fh, ">", $conffile
584+
or croak "could not write \"$conffile\": $!";
585+
print $fh @result;
586+
close $fh;
587+
588+
chmod($self->group_access() ? 0640 : 0600, $conffile)
589+
or die("unable to set permissions for $conffile");
590+
}
591+
592+
=pod
593+
547594
=item $node->backup(backup_name)
548595
549596
Create a hot backup with B<pg_basebackup> in subdirectory B<backup_name> of

0 commit comments

Comments
 (0)