Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from brummett/delete-template
Browse files Browse the repository at this point in the history
DeleteTemplate deletes the associated template database
  • Loading branch information
brummett committed Dec 1, 2014
2 parents 569edb8 + 2f44f4a commit c7702b2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 73 deletions.
33 changes: 5 additions & 28 deletions lib/TestDbServer/Command/DeleteDatabase.pm
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
package TestDbServer::Command::DeleteDatabase;

use TestDbServer::PostgresInstance;
use TestDbServer::Exceptions;
use TestDbServer::Command::DeleteTemplateOrDatabase;

use Moose;
use namespace::autoclean;

extends 'TestDbServer::Command::DeleteTemplateOrDatabase';
has database_id => ( isa => 'Str', is => 'ro', required => 1 );
has schema => (isa => 'TestDbServer::Schema', is => 'ro', required => 1 );
has superuser => ( isa => 'Str', is => 'ro', required => 1 );
has host => ( isa => 'Str', is => 'ro', required => 1 );
has port => ( isa => 'Str', is => 'ro', required => 1 );

sub execute {
my $self = shift;

my $database = $self->schema->find_database($self->database_id);
unless ($database) {
Exception::DatabaseNotFound->throw(name => $self->database_id);
}

my $pg = TestDbServer::PostgresInstance->new(
name => $database->name,
host => $self->host,
port => $self->port,
owner => $database->owner,
superuser => $self->superuser,
);

$pg->dropdb();

$database->delete();

return 1;
}
sub _entity_find_method { 'find_database' }
sub _entity_id_method { 'database_id' }
sub _not_found_exception { 'Exception::DatabaseNotFound' }

__PACKAGE__->meta->make_immutable;

Expand Down
17 changes: 5 additions & 12 deletions lib/TestDbServer/Command/DeleteTemplate.pm
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package TestDbServer::Command::DeleteTemplate;

use TestDbServer::Exceptions;
use TestDbServer::Command::DeleteTemplateOrDatabase;

use Moose;
use namespace::autoclean;

extends 'TestDbServer::Command::DeleteTemplateOrDatabase';
has template_id => ( isa => 'Str', is => 'ro', required => 1 );
has schema => (isa => 'TestDbServer::Schema', is => 'ro', required => 1 );

sub execute {
my $self = shift;

my $template = $self->schema->find_template($self->template_id);
unless ($template) {
Exception::TemplateNotFound->throw(name => $self->template_id);
}

$template->delete();
}
sub _entity_find_method { 'find_template' }
sub _entity_id_method { 'template_id' }
sub _not_found_exception { 'Exception::TemplateNotFound' }

__PACKAGE__->meta->make_immutable;

Expand Down
47 changes: 47 additions & 0 deletions lib/TestDbServer/Command/DeleteTemplateOrDatabase.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package TestDbServer::Command::DeleteTemplateOrDatabase;

use TestDbServer::Exceptions;

use Moose;
use Sub::Install;
use namespace::autoclean;

has schema => (isa => 'TestDbServer::Schema', is => 'ro', required => 1 );
has superuser => ( isa => 'Str', is => 'ro', required => 1 );
has host => ( isa => 'Str', is => 'ro', required => 1 );
has port => ( isa => 'Str', is => 'ro', required => 1 );

foreach my $subname ( qw( _entity_find_method _entity_id_method _not_found_exception )) {
no strict 'refs';
*$subname = sub { die "$_[0] didn't implement $subname"; }
}

sub execute {
my $self = shift;

my($finder, $id_getter) = ($self->_entity_find_method, $self->_entity_id_method);

my $entity = $self->schema->$finder( $self->$id_getter );
unless ($entity) {
my $not_found = $self->_not_found_exception;
$not_found->throw(name => $self->$id_getter);
}

my $pg = TestDbServer::PostgresInstance->new(
name => $entity->name,
host => $self->host,
port => $self->port,
owner => $entity->owner,
superuser => $self->superuser,
);

$pg->dropdb();

$entity->delete();

return 1;
}

__PACKAGE__->meta->make_immutable;

1;
4 changes: 4 additions & 0 deletions lib/TestDbServer/TemplateRoutes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ sub delete {

my $return_code;
try {
my($host, $port) = $self->app->host_and_port_for_created_database();
my $cmd = TestDbServer::Command::DeleteTemplate->new(
template_id => $id,
schema => $schema,
superuser => $self->app->configuration->db_user,
host => $host,
port => $port,
);
$schema->txn_do(sub {
$cmd->execute();
Expand Down
55 changes: 23 additions & 32 deletions t/commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ subtest 'create template from database' => sub {
# Make a table in the database
my $table_name = "test_table_$$";
{
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$pg->name, $pg->host, $pg->port),
$pg->owner,
'');
my $dbi = _connect_to_database($pg->name, $pg->owner);
ok($dbi->do("CREATE TABLE $table_name (foo integer NOT NULL PRIMARY KEY)"),
'Create table in base database');
$dbi->disconnect;
Expand All @@ -62,9 +59,7 @@ subtest 'create template from database' => sub {
ok($template, 'get created template');

# connect to the template database
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$template->name, $pg->host, $pg->port),
$pg->owner, '');
my $dbi = _connect_to_database($template->name, $pg->owner);
ok($dbi->do("SELECT foo FROM $table_name WHERE FALSE"), 'table exists in template database');
$dbi->disconnect;

Expand All @@ -91,10 +86,7 @@ subtest 'create database with owner' => sub {
# Make a table in the template
my $table_name = "test_table_$$";
{
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$pg->name, $pg->host, $pg->port),
$pg->owner,
'');
my $dbi = _connect_to_database($pg->name, $pg->owner);
ok($dbi->do("CREATE TABLE $table_name (foo integer NOT NULL PRIMARY KEY)"),
'Create table in base template');
$dbi->disconnect;
Expand All @@ -114,9 +106,7 @@ subtest 'create database with owner' => sub {
ok($database, 'execute');

# connect to the newly created database
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$database->name, $config->db_host, $config->db_port),
$pg->owner, '');
my $dbi = _connect_to_database($database->name, $pg->owner);
ok($dbi->do("SELECT foo FROM $table_name WHERE FALSE"), 'table exists in template database');
$dbi->disconnect;

Expand Down Expand Up @@ -146,10 +136,7 @@ subtest 'create database with invalid owner' => sub {
# Make a table in the template
my $table_name = "test_table_$$";
{
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$pg->name, $pg->host, $pg->port),
$pg->owner,
'');
my $dbi = _connect_to_database($pg->name, $pg->owner);
ok($dbi->do("CREATE TABLE $table_name (foo integer NOT NULL PRIMARY KEY)"),
'Create table in base template');
$dbi->disconnect;
Expand Down Expand Up @@ -183,10 +170,7 @@ subtest 'create database from template' => sub {
# Make a table in the template
my $table_name = "test_table_$$";
{
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$pg->name, $pg->host, $pg->port),
$pg->owner,
'');
my $dbi = _connect_to_database($pg->name, $pg->owner);
ok($dbi->do("CREATE TABLE $table_name (foo integer NOT NULL PRIMARY KEY)"),
'Create table in base template');
$dbi->disconnect;
Expand All @@ -205,9 +189,7 @@ subtest 'create database from template' => sub {
ok($database, 'execute');

# connect to the newly created database
my $dbi = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$database->name, $config->db_host, $config->db_port),
$database->owner, '');
my $dbi = _connect_to_database($database->name, $database->owner);
ok($dbi->do("SELECT foo FROM $table_name WHERE FALSE"), 'table exists in template database');
$dbi->disconnect;

Expand All @@ -225,7 +207,7 @@ subtest 'create database from template' => sub {
};

subtest 'delete template' => sub {
plan tests => 3;
plan tests => 4;

my $pg = new_pg_instance();

Expand All @@ -236,12 +218,17 @@ subtest 'delete template' => sub {

my $cmd = TestDbServer::Command::DeleteTemplate->new(
template_id => $template->template_id,
schema => $schema);
schema => $schema,
host => $config->db_host,
port => $config->db_port,
superuser => $config->db_user);
ok($cmd, 'new');
ok($cmd->execute(), 'execute');

ok(! $schema->find_template($template->template_id),
'template is deleted');
'template record is deleted');

ok(! _connect_to_database($pg->name, $pg->owner), 'cannot connect to deleted template database');
};

subtest 'delete database' => sub {
Expand Down Expand Up @@ -293,10 +280,7 @@ subtest 'delete with connections' => sub {
schema => $schema,
)->execute();
ok($database, 'Create database');
my $dbh = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$database->name, $config->db_host, $config->db_port),
$database->owner,
'');
my $dbh = _connect_to_database($database->name, $database->owner);
ok($dbh, 'connect to created database');
my $cmd = TestDbServer::Command::DeleteDatabase->new(
database_id => $database->id,
Expand Down Expand Up @@ -335,3 +319,10 @@ sub create_new_schema {
return TestDbServer::Schema->connect($config->db_connect_string, $config->db_user, $config->db_password);
}

sub _connect_to_database {
my($name, $owner) = @_;

return DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$name, $config->db_host, $config->db_port),
$owner, '');
}
11 changes: 10 additions & 1 deletion t/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ subtest 'get' => sub {
subtest 'delete' => sub {
plan tests => 8;

my $template_id = $templates[0]->template_id;
my $template_to_delete = $templates[0];
my $template_id = $template_to_delete->id;

# The template has to exist as a real database before we can delete it
my $dbh = DBI->connect(sprintf('dbi:Pg:dbname=%s;host=%s;port=%s',
$config->default_template_name, $config->db_host, $config->db_port),
$config->db_user, '');
$dbh->do(sprintf('CREATE DATABASE "%s"', $template_to_delete->name));
$dbh->disconnect;

$t->delete_ok("/templates/$template_id")
->status_is(204);

Expand Down

0 comments on commit c7702b2

Please sign in to comment.