comparison pkg/imports/dsr.go @ 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 pkg/imports/dst.go@024b16a1c253
children 9c6c65a628a3
comparison
equal deleted inserted replaced
4399:349e409fbbb1 4400:3b36c4d810b0
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2019 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13 // * Sascha Wilde <wilde@intevation.de>
14
15 package imports
16
17 import (
18 "context"
19 "database/sql"
20
21 "gemma.intevation.de/gemma/pkg/models"
22 )
23
24 // DeleteSoundingResult is a Job to delete a sounding result from the database.
25 type DeleteSoundingResult struct {
26 BottleneckId string `json:"bottleneck-id"`
27 Date models.Date `json:"date-info"`
28 }
29
30 // DSRJobKind is the import queue type identifier.
31 const DSRJobKind JobKind = "dsr"
32
33 type dsrJobCreator struct{}
34
35 func init() { RegisterJobCreator(DSRJobKind, dsrJobCreator{}) }
36
37 func (dsrJobCreator) Description() string { return "delete sounding result" }
38
39 func (dsrJobCreator) AutoAccept() bool { return false }
40
41 func (dsrJobCreator) Create() Job { return new(DeleteSoundingResult) }
42
43 func (dsrJobCreator) Depends() [2][]string {
44 return [2][]string{
45 {"sounding resultes"},
46 {},
47 }
48 }
49
50 const (
51 dsrFindSQL = `
52 SELECT id FROM waterway.sounding_results
53 WHERE bottleneck_id = $1
54 AND date_info = $2
55 AND staging_done
56 `
57 dsrStageDoneSQL = `
58 DELETE FROM waterway.sounding_results
59 WHERE id IN (
60 SELECT key from import.track_imports
61 WHERE import_id = $1 AND
62 deletion AND
63 relation = 'waterway.sounding_results'::regclass)`
64 )
65
66 // StageDone finally removes the sounding result from the database.
67 func (dsrJobCreator) StageDone(
68 ctx context.Context,
69 tx *sql.Tx,
70 id int64,
71 ) error {
72 _, err := tx.ExecContext(ctx, dsrStageDoneSQL, id)
73 return err
74 }
75
76 // CleanUp of a sounding result delete import is a NOP.
77 func (*DeleteSoundingResult) CleanUp() error { return nil }
78
79 // Do prepares the deletion of the sounding result.
80 func (dsr *DeleteSoundingResult) Do(
81 ctx context.Context,
82 importID int64,
83 conn *sql.Conn,
84 feedback Feedback,
85 ) (interface{}, error) {
86
87 tx, err := conn.BeginTx(ctx, nil)
88 if err != nil {
89 return nil, err
90 }
91 defer tx.Rollback()
92
93 feedback.Info("SR: bottleneck-id: %v, date-info: %v",
94 dsr.BottleneckId, dsr.Date.Time)
95
96 var id int64
97 if err := tx.QueryRowContext(ctx, dsrFindSQL,
98 dsr.BottleneckId, dsr.Date.Time).Scan(&id); err != nil {
99 return nil, err
100 }
101
102 feedback.Info("Prepare deletion of sounding result with id %d", id)
103
104 if _, err := tx.ExecContext(
105 ctx,
106 trackImportDeletionSQL,
107 importID,
108 "waterway.sounding_results",
109 id,
110 true,
111 ); err != nil {
112 return nil, err
113 }
114
115 if err := tx.Commit(); err != nil {
116 return nil, err
117 }
118
119 return dsr, nil
120 }