Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package Pg::CLI::createdb; | ||
|
||
use Moose; | ||
|
||
use namespace::autoclean; | ||
|
||
use MooseX::SemiAffordanceAccessor; | ||
use MooseX::Types::Moose qw( Str ); | ||
|
||
with qw( Pg::CLI::Role::Connects Pg::CLI::Role::Executable ); | ||
|
||
__PACKAGE__->meta()->make_immutable(); | ||
|
||
1; | ||
|
||
# ABSTRACT: Wrapper for the F<createdb> utility | ||
|
||
__END__ | ||
=head1 SYNOPSIS | ||
my $createdb = Pg::CLI::createdb->new( | ||
username => 'foo', | ||
password => 'bar', | ||
host => 'pg.example.com', | ||
port => 5433, | ||
); | ||
$createdb->run( | ||
database => 'NewDB', | ||
options => [ | ||
'--encoding', 'UTF-8', | ||
'--owner', 'alice', | ||
], | ||
); | ||
=head1 DESCRIPTION | ||
This class provides a wrapper for the F<createdb> utility. | ||
=head1 METHODS | ||
This class provides the following methods: | ||
=head2 Pg::CLI::createdb->new( ... ) | ||
The constructor accepts a number of parameters: | ||
=over 4 | ||
=item * executable | ||
The path to F<createdb>. By default, this will look for F<createdb> in your | ||
path and throw an error if it cannot be found. | ||
=item * username | ||
The username to use when connecting to the database. Optional. | ||
=item * password | ||
The password to use when connecting to the database. Optional. | ||
=item * host | ||
The host to use when connecting to the database. Optional. | ||
=item * port | ||
The port to use when connecting to the database. Optional. | ||
=item * require_ssl | ||
If this is true, then the C<PGSSLMODE> environment variable will be set to | ||
"require" when connecting to the database. | ||
=back | ||
=head2 $createdb->run( database => $db, ... ) | ||
This method runs the createdb command with the given options. | ||
This method also accepts optional C<stdin>, C<stdout>, and C<stderr> | ||
parameters. These parameters can be any defined value that could be passed as | ||
the relevant parameter to L<IPC::Run3>'s C<run3> subroutine. | ||
Most notably, you can pass scalar references to pipe data in via the C<stdin> | ||
parameter or capture output sent to C<stdout> or C<stderr> | ||
This method accepts the following arguments: | ||
=over 4 | ||
=item * database | ||
The name of the database to create. Required. | ||
=item * options | ||
A list of additional options to pass to the command. Optional. | ||
=head1 BUGS | ||
See L<Pg::CLI> for bug reporting details. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use lib 't/lib'; | ||
|
||
use Pg::CLI::createdb; | ||
use Test::More 0.88; | ||
use Test::PgCLI; | ||
|
||
{ | ||
my $createdb = Pg::CLI::createdb->new( executable => 'createdb' ); | ||
|
||
test_command( | ||
'createdb', | ||
sub { | ||
$createdb->run( | ||
database => 'Foo', | ||
); | ||
}, | ||
sub { | ||
shift; | ||
my $cmd = shift; | ||
|
||
ok( | ||
!$ENV{PGPASSWORD}, | ||
'password is not set in environment when command runs' | ||
); | ||
ok( | ||
!$ENV{PGSSLMODE}, | ||
'ssl mode is not set in environment when command runs' | ||
); | ||
is_deeply( | ||
$cmd, | ||
[ | ||
'createdb', | ||
'-w', | ||
'Foo' | ||
], | ||
'command has no options except -w and database name' | ||
); | ||
}, | ||
); | ||
|
||
test_command( | ||
'createdb', | ||
sub { | ||
$createdb->run( | ||
database => 'Foo', | ||
options => [qw( -E UTF-8 -O alice )], | ||
); | ||
}, | ||
sub { | ||
shift; | ||
my $cmd = shift; | ||
|
||
is_deeply( | ||
$cmd, | ||
[ | ||
'createdb', | ||
'-w', | ||
'-E', 'UTF-8', | ||
'-O', 'alice', | ||
'Foo' | ||
], | ||
'command includes options passed to run' | ||
); | ||
}, | ||
); | ||
|
||
test_command( | ||
'createdb', | ||
sub { | ||
$createdb->run( | ||
database => 'Foo', | ||
stdin => \'in', | ||
stdout => \'out', | ||
stderr => \'err', | ||
); | ||
}, | ||
sub { | ||
shift; | ||
my $cmd = shift; | ||
my $stdin = shift; | ||
my $stdout = shift; | ||
my $stderr = shift; | ||
|
||
is_deeply( | ||
$cmd, | ||
[ | ||
'createdb', | ||
'-w', | ||
'Foo', | ||
], | ||
'command has no options except -w and database name' | ||
); | ||
|
||
is_deeply( | ||
$stdin, | ||
\'in', | ||
'got expected stdin ref' | ||
); | ||
|
||
}, | ||
); | ||
} | ||
|
||
{ | ||
my $createdb = Pg::CLI::createdb->new( | ||
executable => 'createdb', | ||
username => 'foo', | ||
password => 'bar', | ||
host => 'foo.example.com', | ||
port => 5141, | ||
require_ssl => 1, | ||
); | ||
|
||
test_command( | ||
'createdb', | ||
sub { | ||
$createdb->run( | ||
database => 'Foo', | ||
); | ||
}, | ||
sub { | ||
shift; | ||
my $cmd = shift; | ||
|
||
is( | ||
$ENV{PGPASSWORD}, 'bar', | ||
'password is set in environment when command runs' | ||
); | ||
is( | ||
$ENV{PGSSLMODE}, 'require', | ||
'ssl mode is set in environment when command runs' | ||
); | ||
is_deeply( | ||
$cmd, | ||
[ | ||
'createdb', | ||
'-U', 'foo', | ||
'-h', 'foo.example.com', | ||
'-p', 5141, | ||
'-w', | ||
'Foo' | ||
], | ||
'command includes connection info' | ||
); | ||
}, | ||
); | ||
} | ||
|
||
done_testing(); |