diff --git a/controller/store/frontend.go b/controller/store/frontend.go index 9c546712..1e9093fd 100644 --- a/controller/store/frontend.go +++ b/controller/store/frontend.go @@ -34,7 +34,7 @@ func (str *Store) GetFrontend(id int, tx *sqlx.Tx) (*Frontend, error) { func (str *Store) FindFrontendNamed(name string, tx *sqlx.Tx) (*Frontend, error) { i := &Frontend{} - if err := tx.QueryRowx("select frontends.* from frontends where name = $1", name).Scan(i); err != nil { + if err := tx.QueryRowx("select frontends.* from frontends where name = $1", name).StructScan(i); err != nil { return nil, errors.Wrap(err, "error selecting frontend by name") } return i, nil diff --git a/controller/store/frontend_test.go b/controller/store/frontend_test.go index 72440ea2..a6e9d417 100644 --- a/controller/store/frontend_test.go +++ b/controller/store/frontend_test.go @@ -1 +1,60 @@ package store + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestPublicFrontend(t *testing.T) { + str, err := Open(&Config{Path: ":memory:", Type: "sqlite3"}) + assert.Nil(t, err) + assert.NotNil(t, str) + + tx, err := str.Begin() + assert.Nil(t, err) + assert.NotNil(t, tx) + + acctId, err := str.CreateAccount(&Account{ + Email: "test@test.com", + Password: "password", + Token: "token", + }, tx) + assert.Nil(t, err) + + envId, err := str.CreateEnvironment(acctId, &Environment{ + Description: "description", + Host: "host", + Address: "address", + ZId: "zId0", + }, tx) + assert.Nil(t, err) + + feName := "public" + feId, err := str.CreateFrontend(envId, &Frontend{ + ZId: "zId0", + Name: &feName, + }, tx) + assert.Nil(t, err) + + fe, err := str.GetFrontend(feId, tx) + assert.Nil(t, err) + assert.NotNil(t, fe) + assert.Equal(t, envId, fe.EnvironmentId) + assert.Equal(t, feName, *fe.Name) + + fe0, err := str.FindFrontendNamed(feName, tx) + assert.Nil(t, err) + assert.NotNil(t, fe0) + assert.EqualValues(t, fe, fe0) + + err = str.DeleteFrontend(fe.Id, tx) + assert.Nil(t, err) + + fe0, err = str.FindFrontendNamed(feName, tx) + assert.NotNil(t, err) + assert.Nil(t, fe0) + + fe0, err = str.GetFrontend(fe.Id, tx) + assert.NotNil(t, err) + assert.Nil(t, fe0) +} diff --git a/controller/store/sql/sqlite3/002_v0_3_0_frontend_selection.sql b/controller/store/sql/sqlite3/002_v0_3_0_frontend_selection.sql index 077d6c3f..84c0802f 100644 --- a/controller/store/sql/sqlite3/002_v0_3_0_frontend_selection.sql +++ b/controller/store/sql/sqlite3/002_v0_3_0_frontend_selection.sql @@ -1,14 +1,5 @@ -- +migrate Up -create table frontends ( - id serial primary key, - environment_id integer not null references environments(id), - z_id varchar(32) not null unique, - name varchar(64) unique, - created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')), - updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')) -); - -- environments.account_id should allow NULL; environments with NULL account_id are "ephemeral" alter table environments rename to environments_old; create table environments ( @@ -24,4 +15,13 @@ create table environments ( constraint chk_z_id check (z_id <> '') ); insert into environments select * from environments_old; -drop table environments_old; \ No newline at end of file +drop table environments_old; + +create table frontends ( + id integer primary key, + environment_id integer not null references environments(id), + z_id varchar(32) not null unique, + name varchar(64) unique, + created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')), + updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')) +);