This file is indexed.

/usr/bin/pg_tapgen is in libtap-parser-sourcehandler-pgtap-perl 3.25-1.

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
#!/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;
our $VERSION = '3.25';

Getopt::Long::Configure (qw(bundling));

my $opts = { psql => 'psql', color => 1 };

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},
    '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;
}

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,
});

print "SELECT * FROM no_plan();\n\n";
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);
    }
}

print "SELECT * FROM finish();\n";

##############################################################################

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";
}

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";
}

__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.
  -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<-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 <david@kineticode.com>

=head1 Copyright

Copyright (c) 2009-2010 Kineticode, Inc. Some Rights Reserved.

=cut