/usr/bin/pg_tapgen is in libtap-parser-sourcehandler-pgtap-perl 3.31-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | #!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
use strict;
use warnings;
use DBI;
use DBD::Pg;
use Getopt::Long;
use File::Spec;
our $VERSION = '3.31';
Getopt::Long::Configure (qw(bundling));
my $opts = { psql => 'psql', directory => '.' };
Getopt::Long::GetOptions(
'dbname|d=s' => \$opts->{dbname},
'username|U=s' => \$opts->{username},
'host|h=s' => \$opts->{host},
'port|p=s' => \$opts->{port},
'exclude-schema|N=s@' => \$opts->{exclude_schema},
'directory|dir=s' => \$opts->{directory},
'verbose|v+' => \$opts->{verbose},
'help|H' => \$opts->{help},
'man|m' => \$opts->{man},
'version|V' => \$opts->{version},
) or require Pod::Usage && Pod::Usage::pod2usage(2);
if ( $opts->{help} or $opts->{man} ) {
require Pod::Usage;
Pod::Usage::pod2usage(
'-sections' => $opts->{man} ? '.+' : '(?i:(Usage|Options))',
'-verbose' => 99,
'-exitval' => 0,
)
}
if ($opts->{version}) {
print 'pg_prove ', main->VERSION, $/;
exit;
}
# Function to write a test script.
sub script(&;$) {
my ($code, $fn) = @_;
my $file = File::Spec->catfile($opts->{directory}, $fn);
open my $fh, '>:encoding(UTF-8)', $file or die "Cannot open $file: $!\n";
my $orig = select;
select $fh;
print "SET client_encoding = 'UTF-8';\n",
"SET client_min_messages = warning;\n",
"CREATE EXTENSION IF NOT EXISTS pgtap;\n",
"RESET client_min_messages;\n\n",
"BEGIN;\n",
"SELECT * FROM no_plan();\n\n";
$code->();
print "SELECT * FROM finish();\nROLLBACK;\n";
close $fh or die "Error closing $file: $!\n";
select $orig;
}
my @conn;
for (qw(host port dbname)) {
push @conn, "$_=$opts->{$_}" if defined $opts->{$_};
}
my $dsn = 'dbi:Pg';
$dsn .= ':' . join ';', @conn if @conn;
my $dbh = DBI->connect($dsn, $opts->{username}, undef, {
RaiseError => 1,
PrintError => 0,
AutoCommit => 1,
pg_enable_utf8 => 1,
});
$dbh->do(q{SET client_encoding = 'UTF-8'});
##############################################################################
script {
if (my @schemas = get_schemas($opts->{exclude_schema})) {
schemas_are(\@schemas);
for my $schema (@schemas) {
tables_are($schema);
views_are($schema);
sequences_are($schema);
functions_are($schema);
}
}
} 'schema.sql';
##############################################################################
sub get_schemas {
my @exclude = ('information_schema');
push @exclude, @{ $_[0] } if $_[0] && @{ $_[0] };
my $sth = $dbh->prepare_cached(q{
SELECT nspname
FROM pg_catalog.pg_namespace
WHERE nspname NOT LIKE 'pg_%'
AND nspname <> ALL(?)
ORDER BY nspname
});
my $schemas = $dbh->selectcol_arrayref($sth, undef, \@exclude) or return;
return @$schemas;
}
sub schemas_are {
my $schemas = shift;
print "SELECT schemas_are(ARRAY[\n '",
join("',\n '", @$schemas),
"'\n]);\n\n" if @$schemas;
}
sub get_rels {
my $sth = $dbh->prepare_cached(q{
SELECT c.relname
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON n.oid = c.relnamespace
WHERE c.relkind = ?
AND n.nspname = ?
ORDER BY c.relname
});
return $dbh->selectcol_arrayref($sth, undef, @_);
}
sub tables_are {
my $schema = shift;
my $tables = get_rels(r => $schema);
return unless $tables && @{ $tables };
print "SELECT tables_are('$schema', ARRAY[\n '",
join("',\n '", @$tables),
"'\n]);\n\n";
for my $table (@{ $tables }) {
script { has_table($schema, $table) } "table_$schema.$table.sql";
}
}
sub views_are {
my $schema = shift;
my $tables = get_rels(v => $schema);
return unless $tables && @$tables;
print "SELECT views_are('$schema', ARRAY[\n '",
join("',\n '", @$tables),
"'\n]);\n\n";
}
sub sequences_are {
my $schema = shift;
my $tables = get_rels(S => $schema);
return unless $tables && @$tables;
print "SELECT sequences_are('$schema', ARRAY[\n '",
join("',\n '", @$tables),
"'\n]);\n\n";
}
sub functions_are {
my $schema = shift;
my $sth = $dbh->prepare(q{
SELECT p.proname
FROM pg_catalog.pg_proc p
JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
WHERE n.nspname = ?
});
my $funcs = $dbh->selectcol_arrayref($sth, undef, $schema);
return unless $funcs && @$funcs;
print "SELECT functions_are('$schema', ARRAY[\n '",
join("',\n '", @$funcs),
"'\n]);\n\n";
}
sub has_table {
my ($schema, $table) = @_;
print "SELECT has_table(
'$schema', '$table',
'Should have table $schema.$table'
);\n\n";
has_pk($schema, $table);
columns_are($schema, $table);
}
sub has_pk {
my ($schema, $table) = @_;
my $fn = _hasc($schema, $table, 'p') ? 'has_pk' : 'hasnt_pk';
print "select $fn(
'$schema', '$table',
'Table $schema.$table should have a primary key'
);\n\n";
}
sub columns_are {
my ($schema, $table) = @_;
print "SET search_path = '$schema';\n";
my $cols = $dbh->selectall_arrayref(q{
SELECT a.attname AS name
, pg_catalog.format_type(a.atttypid, a.atttypmod) AS type
, a.attnotnull AS not_null
, a.atthasdef AS has_default
, pg_catalog.pg_get_expr(d.adbin, d.adrelid)
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON n.oid = c.relnamespace
JOIN pg_catalog.pg_attribute a ON c.oid = a.attrelid
LEFT JOIN pg_catalog.pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE n.nspname = ?
AND c.relname = ?
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
}, undef, $schema, $table);
return unless $cols && @{ $cols };
print "SELECT columns_are('$schema', '$table', ARRAY[\n '",
join("',\n '", map { $_->[0] } @{ $cols }),
"'\n]);\n\n";
for my $col (@{ $cols }) {
my $null_fn = $col->[2] ? 'col_not_null(' : 'col_is_null( ';
my $def_fn = $col->[3] ? 'col_has_default( ' : 'col_hasnt_default(';
print "SELECT has_column( '$table', '$col->[0]');\n",
"SELECT col_type_is( '$table', '$col->[0]', '$col->[1]');\n",
"SELECT $null_fn '$table', '$col->[0]');\n",
"SELECT $def_fn'$table', '$col->[0]');\n";
print "SELECT col_default_is( '$table', '$col->[0]', '$col->[4]');\n"
if $col->[3];
print $/;
}
}
sub _hasc {
my $sth = $dbh->prepare_cached(q{
SELECT EXISTS(
SELECT true
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
JOIN pg_catalog.pg_constraint x ON c.oid = x.conrelid
WHERE c.relhaspkey = true
AND n.nspname = ?
AND c.relname = ?
AND x.contype = ?
)
});
return $dbh->selectcol_arrayref($sth, undef, @_)->[0];
}
__END__
=encoding utf8
=head1 Name
pg_tapgen - Generate schema TAP tests from an existing database
=head1 Usage
pg_tapgen -d template1 > schema_test.sql
=head1 Description
C<pg_tapgen> is a command-line utility to generate pgTAP tests to validate a
database schema by reading an existing database and generating the tests to
match. Its use requires the installation of the L<DBI> and L<DBD::Pg> from
CPAN or via a package distribution.
B<Warning:> These prerequisites are not validated by the pgTAP C<Makefile>, so
you'll need to install them yourself. As a result, inclusion of this script in
the pgTAP distribution is experimental. It may be moved to its own
distribution in the future.
=head1 Options
-d --dbname DBNAME Database to which to connect.
-U --username USERNAME Username with which to connect.
-h --host HOST Host to which to connect.
-p --port PORT Port to which to connect.
-v --verbose Display output of test scripts while running them.
-N --exclude-schema Exclude a schema from the generated tests.
--directory DIRECTORY Directory to which to write the test files.
-H --help Print a usage statement and exit.
-m --man Print the complete documentation and exit.
-V --version Print the version number and exit.
=head1 Options Details
=over
=item C<-d>
=item C<--dbname>
pg_tapgen --dbname try
pg_tapgen -d postgres
The name of database to which to connect. Defaults to the value of the
C<$PGDATABASE> environment variable or to the system username.
=item C<-U>
=item C<--username>
pg_tapgen --username foo
pg_tapgen -U postgres
PostgreSQL user name to connect as. Defaults to the value of the C<$PGUSER>
environment variable or to the operating system name of the user running the
application.
=item C<-h>
=item C<--host>
pg_tapgen --host pg.example.com
pg_tapgen -h dev.local
Specifies the host name of the machine on which the server is running. If the
value begins with a slash, it is used as the directory for the Unix-domain
socket. Defaults to the value of the C<$PGHOST> environment variable or
localhost.
=item C<-p>
=item C<--port>
pg_tapgen --port 1234
pg_tapgen -p 666
Specifies the TCP port or the local Unix-domain socket file extension on which
the server is listening for connections. Defaults to the value of the
C<$PGPORT> environment variable or, if not set, to the port specified at
compile time, usually 5432.
=item C<--dir>
=item C<--directory>
Directory to which to write test files. Defaults to the current directory.
=item C<-v>
=item C<--verbose>
pg_tapgen --verbose
pg_tapgen -v
Display standard output of test scripts while running them. This behavior can
also be triggered by setting the C<$TEST_VERBOSE> environment variable to a
true value.
=item C<-N>
=item C<--exclude-schema>
pg_tapgen --exclude-schema contrib
pg_tapgen -N testing -N temporary
Exclude a schema from the test generation. C<pg_tapgen> always ignores
C<information_schema>, as it is also ignored by pgTAP. But if there are other
schemas in the database that you don't need or want to test for in the
database (because you run the tests on another database without those schemas,
for example), use C<--exclude-schema> to omit them. May be used more than once
to exclude more than one schema.
=item C<-H>
=item C<--help>
pg_tapgen --help
pg_tapgen -H
Outputs a brief description of the options supported by C<pg_tapgen> and exits.
=item C<-m>
=item C<--man>
pg_tapgen --man
pg_tapgen -m
Outputs this documentation and exits.
=item C<-V>
=item C<--version>
pg_tapgen --version
pg_tapgen -V
Outputs the program name and version and exits.
=back
=head1 Author
David E. Wheeler <dwheeler@cpan.org>
=head1 Copyright
Copyright (c) 2009-2015 David E. Wheeler. Some Rights Reserved.
=cut
|