Mercurial > gemma
annotate pkg/imports/dsr.go @ 4484:819eafe60f41
Merged
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Thu, 26 Sep 2019 12:16:23 +0200 |
parents | 911b1349a9bd |
children | ca6a5f722471 |
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" |
4408
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
20 "errors" |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
21 |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
22 "gemma.intevation.de/gemma/pkg/models" |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 ) |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
25 // DeleteSoundingResult is a Job to delete a sounding result from the database. |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
26 type DeleteSoundingResult struct { |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
27 BottleneckId string `json:"bottleneck-id"` |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
28 Date models.Date `json:"date-info"` |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
29 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
30 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
31 // DSRJobKind is the import queue type identifier. |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
32 const DSRJobKind JobKind = "dsr" |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
33 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
34 type dsrJobCreator struct{} |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
35 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
36 func init() { RegisterJobCreator(DSRJobKind, dsrJobCreator{}) } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
37 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
38 func (dsrJobCreator) Description() string { return "delete sounding result" } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
39 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
40 func (dsrJobCreator) AutoAccept() bool { return false } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
41 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
42 func (dsrJobCreator) Create() Job { return new(DeleteSoundingResult) } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
43 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
44 func (dsrJobCreator) Depends() [2][]string { |
4403
9c6c65a628a3
Delete sounding result imports: Fixed dependencies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4400
diff
changeset
|
45 // Same as import. |
9c6c65a628a3
Delete sounding result imports: Fixed dependencies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4400
diff
changeset
|
46 return srJobCreator{}.Depends() |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 const ( |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
50 dsrFindSQL = ` |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
51 SELECT id FROM waterway.sounding_results |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
52 WHERE bottleneck_id = $1 |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
53 AND date_info = $2 |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
54 AND staging_done |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 ` |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
56 dsrStageDoneSQL = ` |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
57 DELETE FROM waterway.sounding_results |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 WHERE id IN ( |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 SELECT key from import.track_imports |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 WHERE import_id = $1 AND |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 deletion AND |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
62 relation = 'waterway.sounding_results'::regclass)` |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 ) |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
65 // StageDone finally removes the sounding result from the database. |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
66 func (dsrJobCreator) StageDone( |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 ctx context.Context, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 tx *sql.Tx, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
69 id int64, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 ) error { |
4408
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
71 result, err := tx.ExecContext(ctx, dsrStageDoneSQL, id) |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
72 if err != nil { |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
73 return err |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
74 } |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
75 rows, err := result.RowsAffected() |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
76 if err != nil { |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
77 return err |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
78 } |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
79 if rows == 0 { |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
80 return errors.New("Deletion failed. " + |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
81 "Propably Data outside the area of responsibility." + |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
82 "Or the data was already deleted by another user.") |
911b1349a9bd
Added minimal error handling to deletion of SR.
Sascha Wilde <wilde@intevation.de>
parents:
4403
diff
changeset
|
83 } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
84 return 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 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
87 // CleanUp of a sounding result delete import is a NOP. |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
88 func (*DeleteSoundingResult) CleanUp() error { return nil } |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
89 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
90 // Do prepares the deletion of the sounding result. |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
91 func (dsr *DeleteSoundingResult) Do( |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
92 ctx context.Context, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
93 importID int64, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
94 conn *sql.Conn, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
95 feedback Feedback, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
96 ) (interface{}, error) { |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
97 |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
98 tx, err := conn.BeginTx(ctx, nil) |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
99 if err != nil { |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
100 return nil, err |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
101 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
102 defer tx.Rollback() |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
103 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
104 feedback.Info("SR: bottleneck-id: %v, date-info: %v", |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
105 dsr.BottleneckId, dsr.Date.Time) |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
106 |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
107 var id int64 |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
108 if err := tx.QueryRowContext(ctx, dsrFindSQL, |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
109 dsr.BottleneckId, dsr.Date.Time).Scan(&id); err != nil { |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
110 return nil, err |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
111 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
112 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
113 feedback.Info("Prepare deletion of sounding result with id %d", id) |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
114 |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
115 if _, err := tx.ExecContext( |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
116 ctx, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
117 trackImportDeletionSQL, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
118 importID, |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
119 "waterway.sounding_results", |
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
120 id, |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
121 true, |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
122 ); err != nil { |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
123 return nil, err |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
124 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
125 |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
126 if err := tx.Commit(); err != nil { |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
127 return nil, err |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
128 } |
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
129 |
4400
3b36c4d810b0
Added back end for deletion of sounding results.
Sascha Wilde <wilde@intevation.de>
parents:
4392
diff
changeset
|
130 return dsr, nil |
4392
024b16a1c253
Implemented deletion of stretches.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
131 } |