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 #27 from nnutter/no_host_port_on_template
Browse files Browse the repository at this point in the history
remove host and port from templates
  • Loading branch information
nnutter committed Nov 11, 2014
2 parents abba7f7 + 43251e9 commit f335f84
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 34 deletions.
2 changes: 0 additions & 2 deletions docs/rest-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ Returns 200 and JSON in the body
owner: String - Database role, owner of the databases created from
this template
note: String - human readable info about this template
host: String - The DB host this template database lives in
port: Integer - The port this template's Database listens on
}

Returns 404 if there's no template with that ID
Expand Down
8 changes: 4 additions & 4 deletions lib/TestDbServer/Command/CreateDatabaseFromTemplate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ sub execute {
}

my $pg = TestDbServer::PostgresInstance->new(
host => $template->host,
port => $template->port,
host => $self->host,
port => $self->port,
owner => $template->owner,
superuser => $self->superuser,
);
Expand All @@ -31,8 +31,8 @@ sub execute {

my $database = $self->schema->create_database(
name => $pg->name,
host => $pg->host,
port => $pg->port,
host => $self->host,
port => $self->port,
owner => $pg->owner,
template_id => $template->template_id,
);
Expand Down
2 changes: 0 additions & 2 deletions lib/TestDbServer/Command/CreateTemplateFromDatabase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ sub execute {
my $template = $self->schema->create_template(
name => $self->name,
note => $self->note,
host => $database->host,
port => $database->port,
owner => $database->owner,
);

Expand Down
2 changes: 1 addition & 1 deletion lib/TestDbServer/Schema/Result/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package TestDbServer::Schema::Result::Template;
use parent 'DBIx::Class::Core';

__PACKAGE__->table('database_template');
__PACKAGE__->add_columns(qw(template_id host port name owner note create_time last_used_time));
__PACKAGE__->add_columns(qw(template_id name owner note create_time last_used_time));
__PACKAGE__->set_primary_key('template_id');

1;
2 changes: 1 addition & 1 deletion lib/TestDbServer/TemplateRoutes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sub get {

if ($template) {
$self->app->log->info("found template $id");
my %template = map { $_ => $template->$_ } qw(template_id name owner note host port create_time last_used_time);
my %template = map { $_ => $template->$_ } qw(template_id name owner note create_time last_used_time);
$self->render(json => \%template);

} elsif ($error) {
Expand Down
11 changes: 11 additions & 0 deletions sqitch/deploy/database_template-drop_host_and_port.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Deploy database_template-drop_host_and_port
-- requires: database_template-table

BEGIN;

-- CASCADE for the unnamed unique constraint on host, port, and name
ALTER TABLE database_template DROP COLUMN host CASCADE;
ALTER TABLE database_template DROP COLUMN port;
ALTER TABLE database_template ADD CONSTRAINT database_template_unique_name UNIQUE (name);

COMMIT;
9 changes: 9 additions & 0 deletions sqitch/deploy/seed-template1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Deploy seed-template1
-- requires: database_template-drop_host_and_port

BEGIN;

INSERT INTO database_template (name, owner, note)
VALUES ('template1', 'postgres', 'built-in template1');

COMMIT;
9 changes: 9 additions & 0 deletions sqitch/revert/database_template-drop_host_and_port.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Revert database_template-drop_host_and_port

BEGIN;

ALTER TABLE database_template ADD COLUMN host VARCHAR NOT NULL;
ALTER TABLE database_template ADD COLUMN port VARCHAR NOT NULL;
ALTER TABLE database_template ADD CONSTRAINT database_template_unique_host_port_name UNIQUE (host, port, name);

COMMIT;
7 changes: 7 additions & 0 deletions sqitch/revert/seed-template1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert seed-template1

BEGIN;

DELETE FROM database_template WHERE name = 'template1';

COMMIT;
4 changes: 3 additions & 1 deletion sqitch/sqitch.plan
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%syntax-version=1.0.0-b2
%syntax-version=1.0.0
%project=test_db_server

empty_db 2014-09-17T21:31:58Z Anthony Brummett <[email protected]># Initial, empty database
Expand All @@ -7,3 +7,5 @@ live_database_table 2014-09-17T21:51:14Z Anthony Brummett <[email protected]
database_template-table 2014-10-10T20:16:22Z Anthony Brummett <[email protected]># Add table for new-style template databases
switch-template_id-constraint [database_template-table live_database_table] 2014-10-13T19:40:57Z Anthony Brummett <[email protected]># Change the live_database.template_id FK to point to the template_database table
remove-db_template-table [db_template_table] 2014-10-17T19:47:37Z Anthony Brummett <[email protected]># Remove entity table for old-style templates
database_template-drop_host_and_port [database_template-table] 2014-11-10T20:34:39Z Nathaniel Nutter <[email protected]># drop host and port columns
seed-template1 [database_template-drop_host_and_port] 2014-11-10T20:57:36Z Nathaniel Nutter <[email protected]># seed the template database with an entry for template1
7 changes: 7 additions & 0 deletions sqitch/verify/database_template-drop_host_and_port.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify database_template-drop_host_and_port

BEGIN;

INSERT INTO database_template (name, owner) VALUES ('xxxx', 'xxxx');

ROLLBACK;
7 changes: 7 additions & 0 deletions sqitch/verify/seed-template1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify seed-template1

BEGIN;

SELECT 1/COUNT(*) FROM database_template WHERE name = 'template1';

ROLLBACK;
16 changes: 7 additions & 9 deletions t/commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ subtest 'create template from database' => sub {

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

Expand All @@ -74,8 +75,8 @@ subtest 'create template from database' => sub {

# remove the template database
TestDbServer::PostgresInstance->new(
host => $template->host,
port => $template->port,
host => $pg->host,
port => $pg->port,
owner => $template->owner,
superuser => $config->db_user,
name => $template->name
Expand Down Expand Up @@ -111,8 +112,6 @@ subtest 'create database' => sub {
# with a template ID
my $template = $schema->create_template(
name => $uuid_gen->create_str,
host => $blank_db->host,
port => $blank_db->port,
owner => $config->test_db_owner,
);
my $create_db_cmd = TestDbServer::Command::CreateDatabase->new(
Expand Down Expand Up @@ -143,7 +142,7 @@ subtest 'create database from template' => sub {
my $pg = new_pg_instance();

note('original template named '.$pg->name);
my $template = $schema->create_template( map { $_ => $pg->$_ } qw( host port name owner ) );
my $template = $schema->create_template( map { $_ => $pg->$_ } qw( name owner ) );
# Make a table in the template
my $table_name = "test_table_$$";
{
Expand All @@ -169,7 +168,8 @@ subtest 'create database from template' => sub {

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

Expand All @@ -193,8 +193,6 @@ subtest 'delete template' => sub {

my $template = $schema->create_template(
name => $pg->name,
host => $pg->host,
port => $pg->port,
owner => $pg->owner,
);

Expand Down
2 changes: 0 additions & 2 deletions t/database.t
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ subtest 'create from template' => sub {

my $template = $db->create_template(
name => $pg->name,
host => $pg->host,
port => $pg->port,
owner => $pg->owner,
);

Expand Down
2 changes: 0 additions & 2 deletions t/root.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ subtest 'root with one template and one database' => sub {
my $template = $storage->create_template(
name => $uuid_gen->create_str,
owner => 'bubba',
host => 'localhost',
port => 123,
);
my $database = $storage->create_database(host => 'localhost',
port => 123,
Expand Down
10 changes: 5 additions & 5 deletions t/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ subtest initialize => sub {

my @templates;
subtest save_template => sub {
plan tests => 7;
plan tests => 5;

my $template_1 = $schema->create_template(name => $uuid_gen->create_str, owner => 'bubba', note => 'hi there', host => 'localhost', port => 123);
my $template_1 = $schema->create_template(name => $uuid_gen->create_str, owner => 'bubba', note => 'hi there');
ok($template_1,'Save template with a note');
push @templates, $template_1;

my $template_2 = $schema->create_template(name => $uuid_gen->create_str, owner => 'bubba', host => 'localhost', port => 123);
my $template_2 = $schema->create_template(name => $uuid_gen->create_str, owner => 'bubba');
ok($template_2, 'Save template without a note');
push @templates, $template_2;

throws_ok { $schema->create_template(name => $template_1->name, owner => 'bubba', note => 'garbage', host => 'localhost', port => 123) }
throws_ok { $schema->create_template(name => $template_1->name, owner => 'bubba', note => 'garbage') }
'DBIx::Class::Exception',
'Cannot save_template() with duplicate name';

check_required_attributes_for_save(
sub { $schema->create_template(@_) },
{ name => "template name $$", owner => 'bubba', host => 'localhost', port => 123 },
{ name => "template name $$", owner => 'bubba'},
);
};

Expand Down
8 changes: 3 additions & 5 deletions t/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ subtest 'list' => sub {

my $db = $app->db_storage;
my $owner = $uuid_gen->create_str;
@templates = ( $db->create_template(name => $uuid_gen->create_str, owner => $owner, host => 'localhost', port => 123),
$db->create_template(name => $uuid_gen->create_str, owner => $owner, host => 'localhost', port => 123),
@templates = ( $db->create_template(name => $uuid_gen->create_str, owner => $owner),
$db->create_template(name => $uuid_gen->create_str, owner => $owner),
);

$req = $t->get_ok('/templates')
Expand Down Expand Up @@ -62,14 +62,12 @@ subtest 'search' => sub {
};

subtest 'get' => sub {
plan tests => 13;
plan tests => 11;

$t->get_ok('/templates/'.$templates[0]->template_id)
->status_is(200)
->json_is('/template_id' => $templates[0]->template_id)
->json_is('/name' => $templates[0]->name)
->json_is('/host' => $templates[0]->host)
->json_is('/port' => $templates[0]->port)
->json_is('/note' => undef)
->json_has('/create_time')
->json_has('/last_used_time');
Expand Down

0 comments on commit f335f84

Please sign in to comment.