|
| 1 | +# |
| 2 | +# Tests the force option of drop database command. |
| 3 | +# |
| 4 | +use strict; |
| 5 | +use warnings; |
| 6 | + |
| 7 | +use PostgresNode; |
| 8 | +use TestLib; |
| 9 | +use Test::More tests => 9; |
| 10 | + |
| 11 | +# To avoid hanging while expecting some specific input from a psql |
| 12 | +# instance being driven by us, add a timeout high enough that it |
| 13 | +# should never trigger even on very slow machines, unless something |
| 14 | +# is really wrong. |
| 15 | +my $psql_timeout = IPC::Run::timer(60); |
| 16 | + |
| 17 | +my $node = get_new_node('master'); |
| 18 | +$node->init; |
| 19 | +$node->start; |
| 20 | + |
| 21 | +# Create a database that will be dropped. This will test that the force |
| 22 | +# option works when no other backend is connected to the database being |
| 23 | +# dropped. |
| 24 | +$node->safe_psql('postgres', 'CREATE DATABASE foobar'); |
| 25 | +$node->issues_sql_like( |
| 26 | + [ 'dropdb', '--force', 'foobar' ], |
| 27 | + qr/statement: DROP DATABASE foobar WITH \(FORCE\);/, |
| 28 | + 'SQL DROP DATABASE (FORCE) run'); |
| 29 | + |
| 30 | +# database foobar must not exist. |
| 31 | +is( $node->safe_psql( |
| 32 | + 'postgres', |
| 33 | + qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar');] |
| 34 | + ), |
| 35 | + 'f', |
| 36 | + 'database foobar was removed'); |
| 37 | + |
| 38 | +# Create a database that will be dropped. This will test that the force |
| 39 | +# option works when one other backend is connected to the database being |
| 40 | +# dropped. |
| 41 | +$node->safe_psql('postgres', 'CREATE DATABASE foobar1'); |
| 42 | + |
| 43 | +# Run psql, keeping session alive, so we have an alive backend to kill. |
| 44 | +my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', ''); |
| 45 | +my $killme = IPC::Run::start( |
| 46 | + [ |
| 47 | + 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-f', '-', '-d', |
| 48 | + $node->connstr('foobar1') |
| 49 | + ], |
| 50 | + '<', |
| 51 | + \$killme_stdin, |
| 52 | + '>', |
| 53 | + \$killme_stdout, |
| 54 | + '2>', |
| 55 | + \$killme_stderr, |
| 56 | + $psql_timeout); |
| 57 | + |
| 58 | +# Ensure killme process is active. |
| 59 | +$killme_stdin .= q[ |
| 60 | +SELECT pg_backend_pid(); |
| 61 | +]; |
| 62 | +ok( TestLib::pump_until( |
| 63 | + $killme, $psql_timeout, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m), |
| 64 | + 'acquired pid for SIGTERM'); |
| 65 | +my $pid = $killme_stdout; |
| 66 | +chomp($pid); |
| 67 | +$killme_stdout = ''; |
| 68 | +$killme_stderr = ''; |
| 69 | + |
| 70 | +# Check the connections on foobar1 database. |
| 71 | +is( $node->safe_psql( |
| 72 | + 'postgres', |
| 73 | + qq[SELECT pid FROM pg_stat_activity WHERE datname='foobar1' AND pid = $pid;] |
| 74 | + ), |
| 75 | + $pid, |
| 76 | + 'database foobar1 is used'); |
| 77 | + |
| 78 | +# Now drop database with dropdb --force command. |
| 79 | +$node->issues_sql_like( |
| 80 | + [ 'dropdb', '--force', 'foobar1' ], |
| 81 | + qr/statement: DROP DATABASE foobar1 WITH \(FORCE\);/, |
| 82 | + 'SQL DROP DATABASE (FORCE) run'); |
| 83 | + |
| 84 | +# Check that psql sees the killed backend as having been terminated. |
| 85 | +$killme_stdin .= q[ |
| 86 | +SELECT 1; |
| 87 | +]; |
| 88 | +ok( TestLib::pump_until( |
| 89 | + $killme, $psql_timeout, \$killme_stderr, |
| 90 | + qr/FATAL: terminating connection due to administrator command/m), |
| 91 | + "psql query died successfully after SIGTERM"); |
| 92 | +$killme_stderr = ''; |
| 93 | +$killme_stdout = ''; |
| 94 | +$killme->finish; |
| 95 | + |
| 96 | +# database foobar1 must not exist. |
| 97 | +is( $node->safe_psql( |
| 98 | + 'postgres', |
| 99 | + qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar1');] |
| 100 | + ), |
| 101 | + 'f', |
| 102 | + 'database foobar1 was removed'); |
| 103 | + |
| 104 | +$node->stop(); |
0 commit comments