changeset 2050:a3ccef8ec304 unify_imports

Imports: Re-enabled /imports/config/{id} DELETE.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 27 Jan 2019 22:03:46 +0100
parents d1a680be7ae4
children 7d627258e045
files pkg/controllers/importconfig.go pkg/imports/config.go
diffstat 2 files changed, 78 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importconfig.go	Sun Jan 27 14:27:37 2019 +0100
+++ b/pkg/controllers/importconfig.go	Sun Jan 27 22:03:46 2019 +0100
@@ -22,6 +22,7 @@
 	"github.com/gorilla/mux"
 
 	"gemma.intevation.de/gemma/pkg/imports"
+	"gemma.intevation.de/gemma/pkg/scheduler"
 )
 
 const (
@@ -43,14 +44,6 @@
 INSERT INTO import.import_configuration_attributes
 (import_configuration_id, k, v)
 VALUES ($1, $2, $3)`
-
-	hasImportConfigurationSQL = `
-SELECT true FROM import.import_configuration
-WHERE id = $1`
-
-	deleteImportConfiguationSQL = `
-DELETE FROM import.import_configuration
-WHERE id = $1`
 )
 
 func runImportConfig(
@@ -245,57 +238,47 @@
 	conn *sql.Conn,
 ) (jr JSONResult, err error) {
 
-	/*
+	ctx := req.Context()
 
-		ctx := req.Context()
-
-		id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
+	id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
 
-		var tx *sql.Tx
-		if tx, err = conn.BeginTx(ctx, nil); err != nil {
-			return
-		}
-		defer tx.Rollback()
+	var tx *sql.Tx
+	if tx, err = conn.BeginTx(ctx, nil); err != nil {
+		return
+	}
+	defer tx.Rollback()
+
+	err = imports.DeletePersistentConfigurationContext(
+		ctx,
+		tx,
+		id,
+	)
 
-		var found bool
-		err = tx.QueryRowContext(ctx, hasImportConfigurationSQL, id).Scan(&found)
-		switch {
-		case err == sql.ErrNoRows:
-			err = JSONError{
-				Code:    http.StatusNotFound,
-				Message: fmt.Sprintf("No schedule %d found", id),
-			}
-			return
-		case err != nil:
-			return
-		case !found:
-			err = errors.New("Unexpected result")
-			return
+	switch {
+	case err == sql.ErrNoRows:
+		err = JSONError{
+			Code:    http.StatusNotFound,
+			Message: fmt.Sprintf("No configuration %d found", id),
 		}
-
-		if _, err = tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, id); err != nil {
-			return
-		}
+		return
+	case err != nil:
+		return
+	}
 
-		if _, err = tx.ExecContext(ctx, deleteImportConfiguationSQL, id); err != nil {
-			return
-		}
+	// Remove from running scheduler.
+	scheduler.UnbindByID(id)
 
-		// Remove from running scheduler.
-		scheduler.UnbindByID(id)
+	if err = tx.Commit(); err != nil {
+		return
+	}
 
-		if err = tx.Commit(); err != nil {
-			return
-		}
+	var result = struct {
+		ID int64 `json:"id"`
+	}{
+		ID: id,
+	}
 
-		var result = struct {
-			ID int64 `json:"id"`
-		}{
-			ID: id,
-		}
-
-		jr = JSONResult{Result: &result}
-	*/
+	jr = JSONResult{Result: &result}
 
 	return
 }
--- a/pkg/imports/config.go	Sun Jan 27 14:27:37 2019 +0100
+++ b/pkg/imports/config.go	Sun Jan 27 22:03:46 2019 +0100
@@ -80,10 +80,18 @@
 FROM import.import_configuration_attributes
 WHERE import_configuration_id = $1`
 
+	hasImportConfigurationSQL = `
+SELECT true FROM import.import_configuration
+WHERE id = $1`
+
 	deleteImportConfiguationAttributesSQL = `
 DELETE FROM import.import_configuration_attributes
 WHERE import_configuration_id = $1`
 
+	deleteImportConfiguationSQL = `
+DELETE FROM import.import_configuration
+WHERE id = $1`
+
 	updateImportConfigurationSQL = `
 UPDATE import.import_configuration SET
   username = $2,
@@ -110,7 +118,11 @@
 	return nil
 }
 
-func LoadPersistentConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*PersistentConfig, error) {
+func LoadPersistentConfigContext(
+	ctx context.Context,
+	conn *sql.Conn,
+	id int64,
+) (*PersistentConfig, error) {
 
 	cfg := &PersistentConfig{ID: id}
 
@@ -236,3 +248,34 @@
 	}
 	return err
 }
+
+func DeletePersistentConfigurationContext(
+	ctx context.Context,
+	tx *sql.Tx,
+	id int64,
+) error {
+	var found bool
+	if err := tx.QueryRowContext(
+		ctx,
+		hasImportConfigurationSQL,
+		id,
+	).Scan(&found); err != nil {
+		return err
+	}
+	if !found {
+		return sql.ErrNoRows
+	}
+	if _, err := tx.ExecContext(
+		ctx,
+		deleteImportConfiguationAttributesSQL,
+		id,
+	); err != nil {
+		return nil
+	}
+	_, err := tx.ExecContext(
+		ctx,
+		deleteImportConfiguationSQL,
+		id,
+	)
+	return err
+}