Mercurial > gemma
changeset 4400:3b36c4d810b0
Added back end for deletion of sounding results.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Mon, 16 Sep 2019 16:41:56 +0200 |
parents | 349e409fbbb1 |
children | 9842812b92e0 |
files | pkg/controllers/routes.go pkg/imports/dsr.go pkg/imports/modelconvert.go pkg/models/imports.go |
diffstat | 4 files changed, 135 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/routes.go Fri Sep 13 16:16:59 2019 +0200 +++ b/pkg/controllers/routes.go Mon Sep 16 16:41:56 2019 +0200 @@ -241,7 +241,7 @@ kinds := strings.Join([]string{ "bn", "gm", "fa", "wx", "wa", "wg", "dmv", "fd", "dma", - "sec", "dsec", "dst", + "sec", "dsec", "dst", "dsr", }, "|") api.Handle("/imports/{kind:"+kinds+"}", waterwayAdmin(&mw.JSONHandler{
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/imports/dsr.go Mon Sep 16 16:41:56 2019 +0200 @@ -0,0 +1,120 @@ +// This is Free Software under GNU Affero General Public License v >= 3.0 +// without warranty, see README.md and license for details. +// +// SPDX-License-Identifier: AGPL-3.0-or-later +// License-Filename: LICENSES/AGPL-3.0.txt +// +// Copyright (C) 2019 by via donau +// – Österreichische Wasserstraßen-Gesellschaft mbH +// Software engineering by Intevation GmbH +// +// Author(s): +// * Sascha L. Teichmann <sascha.teichmann@intevation.de> +// * Sascha Wilde <wilde@intevation.de> + +package imports + +import ( + "context" + "database/sql" + + "gemma.intevation.de/gemma/pkg/models" +) + +// DeleteSoundingResult is a Job to delete a sounding result from the database. +type DeleteSoundingResult struct { + BottleneckId string `json:"bottleneck-id"` + Date models.Date `json:"date-info"` +} + +// DSRJobKind is the import queue type identifier. +const DSRJobKind JobKind = "dsr" + +type dsrJobCreator struct{} + +func init() { RegisterJobCreator(DSRJobKind, dsrJobCreator{}) } + +func (dsrJobCreator) Description() string { return "delete sounding result" } + +func (dsrJobCreator) AutoAccept() bool { return false } + +func (dsrJobCreator) Create() Job { return new(DeleteSoundingResult) } + +func (dsrJobCreator) Depends() [2][]string { + return [2][]string{ + {"sounding resultes"}, + {}, + } +} + +const ( + dsrFindSQL = ` +SELECT id FROM waterway.sounding_results + WHERE bottleneck_id = $1 + AND date_info = $2 + AND staging_done +` + dsrStageDoneSQL = ` +DELETE FROM waterway.sounding_results +WHERE id IN ( + SELECT key from import.track_imports + WHERE import_id = $1 AND + deletion AND + relation = 'waterway.sounding_results'::regclass)` +) + +// StageDone finally removes the sounding result from the database. +func (dsrJobCreator) StageDone( + ctx context.Context, + tx *sql.Tx, + id int64, +) error { + _, err := tx.ExecContext(ctx, dsrStageDoneSQL, id) + return err +} + +// CleanUp of a sounding result delete import is a NOP. +func (*DeleteSoundingResult) CleanUp() error { return nil } + +// Do prepares the deletion of the sounding result. +func (dsr *DeleteSoundingResult) Do( + ctx context.Context, + importID int64, + conn *sql.Conn, + feedback Feedback, +) (interface{}, error) { + + tx, err := conn.BeginTx(ctx, nil) + if err != nil { + return nil, err + } + defer tx.Rollback() + + feedback.Info("SR: bottleneck-id: %v, date-info: %v", + dsr.BottleneckId, dsr.Date.Time) + + var id int64 + if err := tx.QueryRowContext(ctx, dsrFindSQL, + dsr.BottleneckId, dsr.Date.Time).Scan(&id); err != nil { + return nil, err + } + + feedback.Info("Prepare deletion of sounding result with id %d", id) + + if _, err := tx.ExecContext( + ctx, + trackImportDeletionSQL, + importID, + "waterway.sounding_results", + id, + true, + ); err != nil { + return nil, err + } + + if err := tx.Commit(); err != nil { + return nil, err + } + + return dsr, nil +}
--- a/pkg/imports/modelconvert.go Fri Sep 13 16:16:59 2019 +0200 +++ b/pkg/imports/modelconvert.go Mon Sep 16 16:41:56 2019 +0200 @@ -31,6 +31,7 @@ SECJobKind: func() interface{} { return new(models.SectionImport) }, DSECJobKind: func() interface{} { return new(models.SectionDelete) }, DSTJobKind: func() interface{} { return new(models.StretchDelete) }, + DSRJobKind: func() interface{} { return new(models.SoundingResultDelete) }, } // ImportModelForJobKind returns the constructor function to @@ -173,6 +174,14 @@ dst := input.(*models.StretchDelete) return &DeleteStretch{ID: dst.ID} }, + + DSRJobKind: func(input interface{}) interface{} { + dsr := input.(*models.SoundingResultDelete) + return &DeleteSoundingResult{ + BottleneckId: dsr.BottleneckId, + Date: dsr.Date, + } + }, } func nilString(s *string) string {
--- a/pkg/models/imports.go Fri Sep 13 16:16:59 2019 +0200 +++ b/pkg/models/imports.go Mon Sep 16 16:41:56 2019 +0200 @@ -128,6 +128,11 @@ StretchDelete struct { ID int64 `json:"id"` } + + SoundingResultDelete struct { + BottleneckId string `json:"bottleneck-id"` + Date Date `json:"date-info"` + } ) func (cui *ConfigurableURLImport) MarshalAttributes(attrs common.Attributes) error {