Merge pull request #347 from fnproject/fix-queries-for-mysql-pg

Split queries to make them work on Postgres and MySQL
This commit is contained in:
Reed Allman
2017-09-20 17:07:24 -07:00
committed by GitHub
3 changed files with 44 additions and 13 deletions

View File

@@ -222,19 +222,33 @@ func (ds *sqlStore) UpdateApp(ctx context.Context, newapp *models.App) (*models.
}
func (ds *sqlStore) RemoveApp(ctx context.Context, appName string) error {
res, err := ds.db.ExecContext(ctx, ds.db.Rebind(
`DELETE FROM apps WHERE name = ?;
DELETE FROM logs WHERE app_name=?;
DELETE FROM calls WHERE app_name=?;
DELETE FROM routes WHERE app_name=?;`), appName, appName, appName, appName)
if err != nil {
return err
}
_, err = res.RowsAffected()
if err == sql.ErrNoRows {
return models.ErrAppsNotFound
}
return err
return ds.Tx(func(tx *sqlx.Tx) error {
res, err := tx.ExecContext(ctx, tx.Rebind(`DELETE FROM apps WHERE name=?`), appName)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return models.ErrAppsNotFound
}
deletes := []string{
`DELETE FROM logs WHERE app_name=?`,
`DELETE FROM calls WHERE app_name=?`,
`DELETE FROM routes WHERE app_name=?`,
}
for _, stmt := range deletes {
_, err := tx.ExecContext(ctx, tx.Rebind(stmt), appName)
if err != nil {
return err
}
}
return nil
})
}
func (ds *sqlStore) GetApp(ctx context.Context, name string) (*models.App, error) {

View File

@@ -14,6 +14,9 @@ import (
func CheckAppResponseError(t *testing.T, e error) {
if e != nil {
switch err := e.(type) {
case *apps.DeleteAppsAppNotFound:
t.Errorf("Unexpected error occurred: %v Original Location: %s", err.Payload.Error.Message, MyCaller())
t.FailNow()
case *apps.DeleteAppsAppDefault:
t.Errorf("Unexpected error occurred: %v. Status code: %v Orig Location: %s", err.Payload.Error.Message, err.Code(), MyCaller())
t.FailNow()

View File

@@ -11,6 +11,20 @@ import (
func TestApps(t *testing.T) {
t.Run("delete-app-not-found-test", func(t *testing.T) {
t.Parallel()
s := SetupDefaultSuite()
cfg := &apps.DeleteAppsAppParams{
App: "missing-app",
Context: s.Context,
}
cfg.WithTimeout(time.Second * 60)
_, err := s.Client.Apps.DeleteAppsApp(cfg)
if err == nil {
t.Errorf("Error during app delete: we should get HTTP 404, but got: %s", err.Error())
}
})
t.Run("app-not-found-test", func(t *testing.T) {
t.Parallel()
s := SetupDefaultSuite()