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 #66 from brummett/delete-template
Browse files Browse the repository at this point in the history
Allow deleting a template which has databases made from it
  • Loading branch information
brummett committed Mar 17, 2015
2 parents 8fd2ece + 3a449e7 commit 44ac1aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/TestDbServer/Command/DeleteTemplate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ sub _entity_find_method { 'find_template' }
sub _entity_id_method { 'template_id' }
sub _not_found_exception { 'Exception::TemplateNotFound' }

sub execute {
my $self = shift;

my $databases_with_this_template = $self->schema->search_database(template_id => $self->template_id);
$databases_with_this_template->update({template_id => undef});

$self->SUPER::execute();
}

__PACKAGE__->meta->make_immutable;

1;
41 changes: 40 additions & 1 deletion t/commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ my $config = TestDbServer::Configuration->new_from_path();
my $schema = create_new_schema($config);
my $uuid_gen = Data::UUID->new();

plan tests => 7;
plan tests => 8;

subtest 'create template from database' => sub {
plan tests => 5;
Expand Down Expand Up @@ -231,6 +231,45 @@ subtest 'delete template' => sub {
ok(! _connect_to_database($pg->name, $pg->owner), 'cannot connect to deleted template database');
};

subtest 'delete template with databases' => sub {
plan tests => 7;

my $pg = new_pg_instance();

my $template = $schema->create_template(
name => $pg->name,
owner => $pg->owner,
);
ok($template, 'create template');

my $create_db_command = TestDbServer::Command::CreateDatabaseFromTemplate->new(
owner => undef,
host => $config->db_host,
port => $config->db_port,
template_name => $template->name,
schema => $schema,
superuser => $config->db_user,
);
my $database = $create_db_command->execute();
ok($database, 'create db from template');

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

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

my $template_record = $schema->find_template($template->template_id);
ok(! $template_record, 'template record was deleted');
my $database_record = $schema->find_database($database->database_id);
is($database_record->template_id, undef, q(database record's template_id is now null));
};

subtest 'delete database' => sub {
plan tests => 5;

Expand Down

0 comments on commit 44ac1aa

Please sign in to comment.