annotate pkg/imports/dst.go @ 5670:b75d0b303328

Various fixes and improvements of gauges import: - Allow update of erased data (and thereby set erased to false) - Fix source_organization to work with ERDMS2 - Give ISRS of new and updated gauges in summary - Fixed reference of null pointers if revlevels are missing - Fixed reference of null pointer on update errors - Added ISRS to reference_code warning
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 08 Dec 2023 17:29:56 +0100
parents 59a99655f34d
children 6270951dda28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4392
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
3 //
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
6 //
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
10 //
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
11 // Author(s):
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
13 // * Sascha Wilde <wilde@intevation.de>
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
14
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
15 package imports
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
16
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
17 import (
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
18 "context"
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
19 "database/sql"
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
20 "fmt"
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
21 )
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
22
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
23 // DeleteStretch is a Job to delete a stretch from the database.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
24 type DeleteStretch struct {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
25 ID int64 `json:"id"`
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
26 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
27
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
28 // DSTJobKind is the import queue type identifier.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
29 const DSTJobKind JobKind = "dst"
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
30
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
31 type dstJobCreator struct{}
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
32
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
33 func init() { RegisterJobCreator(DSTJobKind, dstJobCreator{}) }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
34
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
35 func (dstJobCreator) Description() string { return "delete stretch" }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
36
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
37 func (dstJobCreator) AutoAccept() bool { return false }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
38
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
39 func (dstJobCreator) Create() Job { return new(DeleteStretch) }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
40
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
41 func (dstJobCreator) Depends() [2][]string {
5020
e4ab338e7ba9 Removed unnecessary creatings of empty slices in expressing read dependencies in imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4392
diff changeset
42 return [2][]string{{"stretches"}}
4392
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
43 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
44
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
45 const (
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
46 dstExistsSQL = `
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
47 SELECT EXISTS (
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
48 SELECT 1 FROM users.stretches
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
49 WHERE id = $1 AND staging_done)
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
50 `
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
51 dstStageDoneSQL = `
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
52 DELETE FROM users.stretches
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
53 WHERE id IN (
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
54 SELECT key from import.track_imports
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
55 WHERE import_id = $1 AND
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
56 deletion AND
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
57 relation = 'users.stretches'::regclass)`
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
58 )
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
59
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
60 // StageDone finally removes the stretch from the database.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
61 func (dstJobCreator) StageDone(
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
62 ctx context.Context,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
63 tx *sql.Tx,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
64 id int64,
5034
59a99655f34d Added feedback support for StageDone.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5020
diff changeset
65 _ Feedback,
4392
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
66 ) error {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
67 _, err := tx.ExecContext(ctx, dstStageDoneSQL, id)
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
68 return err
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
69 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
70
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
71 // CleanUp of a stretch delete import is a NOP.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
72 func (*DeleteStretch) CleanUp() error { return nil }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
73
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
74 // Do prepares the deletion of the stretch.
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
75 func (dst *DeleteStretch) Do(
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
76 ctx context.Context,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
77 importID int64,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
78 conn *sql.Conn,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
79 feedback Feedback,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
80 ) (interface{}, error) {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
81
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
82 tx, err := conn.BeginTx(ctx, nil)
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
83 if err != nil {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
84 return nil, err
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
85 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
86 defer tx.Rollback()
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
87
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
88 var found bool
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
89 if err := tx.QueryRowContext(ctx, dstExistsSQL, dst.ID).Scan(&found); err != nil {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
90 return nil, err
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
91 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
92
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
93 if !found {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
94 return nil, fmt.Errorf("no stretch with id %d found", dst.ID)
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
95 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
96
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
97 feedback.Info("Prepare deletion of stretch with id %d", dst.ID)
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
98
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
99 if _, err := tx.ExecContext(
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
100 ctx,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
101 trackImportDeletionSQL,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
102 importID,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
103 "users.stretches",
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
104 dst.ID,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
105 true,
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
106 ); err != nil {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
107 return nil, err
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
108 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
109
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
110 if err := tx.Commit(); err != nil {
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
111 return nil, err
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
112 }
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
113
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
114 return dst, nil
024b16a1c253 Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
115 }