Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix problem with not null constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Oct 11, 2018
1 parent ca2b9aa commit 5e51d23
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/storage/executor/internal/postgres/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (scope logScope) Write(data query.WriteLog) (types.Log, error) {
entity.Context, entity.InputID)
}
q := `INSERT INTO "log" ("account_id", "schema_id", "input_id", "template_id", "identifier", "code", "context")
VALUES ((SELECT "account_id" FROM "schema" WHERE "id" = $1), $1, $2, $3, $4, $5, $6)
RETURNING "id", "created_at"`
VALUES ((SELECT "account_id" FROM "schema" WHERE "id" = $1), $1, $2, $3, $4, $5, $6)
RETURNING "id", "created_at"`
row := scope.conn.QueryRowContext(scope.ctx, q,
entity.SchemaID, entity.InputID, entity.TemplateID,
entity.Identifier, entity.Code, encoded)
Expand Down
21 changes: 14 additions & 7 deletions pkg/storage/executor/internal/postgres/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (scope schemaScope) Create(token *types.Token, data query.CreateSchema) (ty
"user %q of account %q tried to marshal a schema definition `%#v` into XML",
token.UserID, token.User.AccountID, entity.Definition)
}
q := `INSERT INTO "schema" ("id", "account_id", "title", "definition") VALUES ($1, $2, $3, $4)
RETURNING "id", "created_at"`
q := `INSERT INTO "schema" ("id", "account_id", "title", "definition")
VALUES (coalesce($1, uuid_generate_v4()), $2, $3, $4)
RETURNING "id", "created_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, data.ID, entity.AccountID, entity.Title, encoded)
if err := row.Scan(&entity.ID, &entity.CreatedAt); err != nil {
return entity, errors.Database(errors.ServerErrorMessage, err,
Expand All @@ -44,7 +45,8 @@ func (scope schemaScope) Create(token *types.Token, data query.CreateSchema) (ty
// Read TODO issue#173
func (scope schemaScope) Read(token *types.Token, data query.ReadSchema) (types.Schema, error) {
entity, encoded := types.Schema{ID: data.ID, AccountID: token.User.AccountID}, []byte(nil)
q := `SELECT "title", "definition", "created_at", "updated_at", "deleted_at" FROM "schema"
q := `SELECT "title", "definition", "created_at", "updated_at", "deleted_at"
FROM "schema"
WHERE "id" = $1 AND "account_id" = $2`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
if err := row.Scan(&entity.Title, &encoded, &entity.CreatedAt, &entity.UpdatedAt, &entity.DeletedAt); err != nil {
Expand All @@ -63,7 +65,8 @@ func (scope schemaScope) Read(token *types.Token, data query.ReadSchema) (types.
// ReadByID TODO issue#173
func (scope schemaScope) ReadByID(id domain.ID) (types.Schema, error) {
entity, encoded := types.Schema{ID: id}, []byte(nil)
q := `SELECT "title", "definition", "created_at", "updated_at" FROM "schema"
q := `SELECT "title", "definition", "created_at", "updated_at"
FROM "schema"
WHERE "id" = $1 AND "deleted_at" IS NULL`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID)
if err := row.Scan(&entity.Title, &encoded, &entity.CreatedAt, &entity.UpdatedAt); err != nil {
Expand Down Expand Up @@ -98,7 +101,8 @@ func (scope schemaScope) Update(token *types.Token, data query.UpdateSchema) (ty
"user %q of account %q tried to marshal definition `%#v` of the schema %q into XML",
token.UserID, token.User.AccountID, entity.Definition, entity.ID)
}
q := `UPDATE "schema" SET "title" = $1, "definition" = $2
q := `UPDATE "schema"
SET "title" = $1, "definition" = $2
WHERE "id" = $3 AND "account_id" = $4
RETURNING "updated_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.Title, encoded, entity.ID, entity.AccountID)
Expand All @@ -117,7 +121,9 @@ func (scope schemaScope) Delete(token *types.Token, data query.DeleteSchema) (ty
return entity, readErr
}
if data.Permanently {
q := `DELETE FROM "schema" WHERE "id" = $1 AND "account_id" = $2 RETURNING now()`
q := `DELETE FROM "schema"
WHERE "id" = $1 AND "account_id" = $2
RETURNING now()`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
if scanErr := row.Scan(&entity.DeletedAt); scanErr != nil {
return entity, errors.Database(errors.ServerErrorMessage, scanErr,
Expand All @@ -126,7 +132,8 @@ func (scope schemaScope) Delete(token *types.Token, data query.DeleteSchema) (ty
}
return entity, nil
}
q := `UPDATE "schema" SET "deleted_at" = now()
q := `UPDATE "schema"
SET "deleted_at" = now()
WHERE "id" = $1 AND "account_id" = $2
RETURNING "deleted_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
Expand Down
21 changes: 14 additions & 7 deletions pkg/storage/executor/internal/postgres/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type templateScope struct {
func (scope templateScope) Create(token *types.Token, data query.CreateTemplate) (types.Template, error) {
entity := types.Template{AccountID: token.User.AccountID, Title: data.Title, Definition: data.Definition}
encoded := string(entity.Definition)
q := `INSERT INTO "template" ("id", "account_id", "title", "definition") VALUES ($1, $2, $3, $4)
RETURNING "id", "created_at"`
q := `INSERT INTO "template" ("id", "account_id", "title", "definition")
VALUES (coalesce($1, uuid_generate_v4()), $2, $3, $4)
RETURNING "id", "created_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, data.ID, entity.AccountID, entity.Title, encoded)
if err := row.Scan(&entity.ID, &entity.CreatedAt); err != nil {
return entity, errors.Database(errors.ServerErrorMessage, err,
Expand All @@ -38,7 +39,8 @@ func (scope templateScope) Create(token *types.Token, data query.CreateTemplate)
// Read TODO issue#173
func (scope templateScope) Read(token *types.Token, data query.ReadTemplate) (types.Template, error) {
entity, encoded := types.Template{ID: data.ID, AccountID: token.User.AccountID}, ""
q := `SELECT "title", "definition", "created_at", "updated_at", "deleted_at" FROM "template"
q := `SELECT "title", "definition", "created_at", "updated_at", "deleted_at"
FROM "template"
WHERE "id" = $1 AND "account_id" = $2`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
if err := row.Scan(&entity.Title, &encoded, &entity.CreatedAt, &entity.UpdatedAt, &entity.DeletedAt); err != nil {
Expand All @@ -53,7 +55,8 @@ func (scope templateScope) Read(token *types.Token, data query.ReadTemplate) (ty
// ReadByID TODO issue#173
func (scope templateScope) ReadByID(id domain.ID) (types.Template, error) {
entity, encoded := types.Template{ID: id}, ""
q := `SELECT "title", "definition", "created_at", "updated_at" FROM "template"
q := `SELECT "title", "definition", "created_at", "updated_at"
FROM "template"
WHERE "id" = $1 AND "deleted_at" IS NULL`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID)
if err := row.Scan(&entity.Title, &encoded, &entity.CreatedAt, &entity.UpdatedAt); err != nil {
Expand All @@ -79,7 +82,8 @@ func (scope templateScope) Update(token *types.Token, data query.UpdateTemplate)
entity.Definition = data.Definition
}
encoded := string(entity.Definition)
q := `UPDATE "template" SET "title" = $1, "definition" = $2
q := `UPDATE "template"
SET "title" = $1, "definition" = $2
WHERE "id" = $3 AND "account_id" = $4
RETURNING "updated_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.Title, encoded, entity.ID, entity.AccountID)
Expand All @@ -98,7 +102,9 @@ func (scope templateScope) Delete(token *types.Token, data query.DeleteTemplate)
return entity, readErr
}
if data.Permanently {
q := `DELETE FROM "template" WHERE "id" = $1 AND "account_id" = $2 RETURNING now()`
q := `DELETE FROM "template"
WHERE "id" = $1 AND "account_id" = $2
RETURNING now()`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
if scanErr := row.Scan(&entity.DeletedAt); scanErr != nil {
return entity, errors.Database(errors.ServerErrorMessage, scanErr,
Expand All @@ -107,7 +113,8 @@ func (scope templateScope) Delete(token *types.Token, data query.DeleteTemplate)
}
return entity, nil
}
q := `UPDATE "template" SET "deleted_at" = now()
q := `UPDATE "template"
SET "deleted_at" = now()
WHERE "id" = $1 AND "account_id" = $2
RETURNING "deleted_at"`
row := scope.conn.QueryRowContext(scope.ctx, q, entity.ID, entity.AccountID)
Expand Down
7 changes: 4 additions & 3 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"testing"

"github.com/kamilsk/form-api/pkg/errors"
"github.com/kamilsk/form-api/pkg/storage"
"github.com/stretchr/testify/assert"

. "github.com/kamilsk/form-api/pkg/storage"
)

func TestMust(t *testing.T) {
t.Run("success", func(t *testing.T) {
assert.NotPanics(t, func() { storage.Must(func(*storage.Storage) error { return nil }) })
assert.NotPanics(t, func() { Must(func(*Storage) error { return nil }) })
})
t.Run("panic", func(t *testing.T) {
assert.Panics(t, func() { storage.Must(func(*storage.Storage) error { return errors.Simple("test") }) })
assert.Panics(t, func() { Must(func(*Storage) error { return errors.Simple("test") }) })
})
}

0 comments on commit 5e51d23

Please sign in to comment.