Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add attributes to pg Engine.

  • Loading branch information...
commit 9ff6eb70ff19f3ef6de35397a51cc80517117c61 1 parent ae72f7e
@theory authored
Showing with 132 additions and 1 deletion.
  1. +77 −0 lib/App/Sqitch/Engine/pg.pm
  2. +55 −1 t/pg.t
View
77 lib/App/Sqitch/Engine/pg.pm
@@ -9,6 +9,83 @@ use Moose;
extends 'App::Sqitch::Engine';
+has client => (
+ is => 'ro',
+ isa => 'Str',
+ lazy => 1,
+ required => 1,
+ default => sub {
+ my $sqitch = shift->sqitch;
+ $sqitch->client
+ || $sqitch->config->get(key => 'core.pg.client')
+ || 'psql' . ($^O eq 'Win32' ? '.exe' : '');
+ },
+);
+
+has username => (
+ is => 'ro',
+ isa => 'Maybe[Str]',
+ lazy => 1,
+ required => 0,
+ default => sub {
+ my $sqitch = shift->sqitch;
+ $sqitch->username || $sqitch->config->get(key => 'core.pg.username');
+ },
+);
+
+has password => (
+ is => 'ro',
+ isa => 'Maybe[Str]',
+ lazy => 1,
+ required => 0,
+ default => sub {
+ shift->sqitch->config->get(key => 'core.pg.password');
+ },
+);
+
+has db_name => (
+ is => 'ro',
+ isa => 'Maybe[Str]',
+ lazy => 1,
+ required => 0,
+ default => sub {
+ my $sqitch = shift->sqitch;
+ $sqitch->db_name || $sqitch->config->get(key => 'core.pg.db_name');
+ },
+);
+
+has host => (
+ is => 'ro',
+ isa => 'Maybe[Str]',
+ lazy => 1,
+ required => 0,
+ default => sub {
+ my $sqitch = shift->sqitch;
+ $sqitch->host || $sqitch->config->get(key => 'core.pg.host');
+ },
+);
+
+has port => (
+ is => 'ro',
+ isa => 'Maybe[Int]',
+ lazy => 1,
+ required => 0,
+ default => sub {
+ my $sqitch = shift->sqitch;
+ $sqitch->port || $sqitch->config->get(key => 'core.pg.port');
+ },
+);
+
+has sqitch_schema => (
+ is => 'ro',
+ isa => 'Str',
+ lazy => 1,
+ required => 1,
+ default => sub {
+ shift->sqitch->config->get(key => 'core.pg.sqitch_schema') || 'sqitch';
+ },
+);
+
sub config_vars {
return (
client => 'any',
View
56 t/pg.t
@@ -3,7 +3,10 @@
use strict;
use warnings;
use v5.10.1;
-use Test::More tests => 2;
+use Test::More tests => 24;
+#use Test::More 'no_plan';
+use Test::MockModule;
+use App::Sqitch;
my $CLASS;
@@ -21,3 +24,54 @@ is_deeply [$CLASS->config_vars], [
port => 'int',
sqitch_schema => 'any',
], 'config_vars should return three vars';
+
+my $sqitch = App::Sqitch->new;
+isa_ok my $pg = $CLASS->new(sqitch => $sqitch), $CLASS;
+
+is $pg->client, 'psql' . ($^O eq 'Win32' ? '.exe' : ''),
+ 'client should default to psql';
+is $pg->sqitch_schema, 'sqitch', 'sqitch_schema default should be "sqitch"';
+for my $attr (qw(username password db_name host port)) {
+ is $pg->$attr, undef, "$attr default should be undef";
+}
+
+##############################################################################
+# Make sure config settings override defaults.
+my %config = (
+ 'core.pg.client' => '/path/to/psql',
+ 'core.pg.username' => 'freddy',
+ 'core.pg.password' => 's3cr3t',
+ 'core.pg.db_name' => 'widgets',
+ 'core.pg.host' => 'db.example.com',
+ 'core.pg.port' => 1234,
+ 'core.pg.sqitch_schema' => 'meta',
+);
+my $mock_config = Test::MockModule->new('App::Sqitch::Config');
+$mock_config->mock(get => sub { $config{ $_[2] } });
+ok $pg = $CLASS->new(sqitch => $sqitch), 'Create another pg';
+
+is $pg->client, '/path/to/psql', 'client should be as configured';
+is $pg->username, 'freddy', 'username should be as configured';
+is $pg->password, 's3cr3t', 'password should be as configured';
+is $pg->host, 'db.example.com', 'host should be as configured';
+is $pg->port, 1234, 'port should be as configured';
+is $pg->sqitch_schema, 'meta', 'sqitch_schema should be as configured';
+
+##############################################################################
+# Now make sure that Sqitch options override configurations.
+$sqitch = App::Sqitch->new(
+ 'client' => '/some/other/psql',
+ 'username' => 'anna',
+ 'db_name' => 'widgets_dev',
+ 'host' => 'foo.com',
+ 'port' => 98760,
+);
+
+ok $pg = $CLASS->new(sqitch => $sqitch), 'Create a pg with sqitch with options';
+
+is $pg->client, '/some/other/psql', 'client should be as optioned';
+is $pg->username, 'anna', 'username should be as optioned';
+is $pg->password, 's3cr3t', 'password should still be as configured';
+is $pg->host, 'foo.com', 'host should be as optioned';
+is $pg->port, 98760, 'port should be as optioned';
+is $pg->sqitch_schema, 'meta', 'sqitch_schema should still be as configured';
Please sign in to comment.
Something went wrong with that request. Please try again.