Mercurial > gemma
comparison pkg/imports/dst.go @ 4392:024b16a1c253
Implemented deletion of stretches.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Thu, 12 Sep 2019 20:08:29 +0200 |
parents | |
children | e4ab338e7ba9 |
comparison
equal
deleted
inserted
replaced
4391:d9088bb96ce1 | 4392:024b16a1c253 |
---|---|
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 "fmt" | |
21 ) | |
22 | |
23 // DeleteStretch is a Job to delete a stretch from the database. | |
24 type DeleteStretch struct { | |
25 ID int64 `json:"id"` | |
26 } | |
27 | |
28 // DSTJobKind is the import queue type identifier. | |
29 const DSTJobKind JobKind = "dst" | |
30 | |
31 type dstJobCreator struct{} | |
32 | |
33 func init() { RegisterJobCreator(DSTJobKind, dstJobCreator{}) } | |
34 | |
35 func (dstJobCreator) Description() string { return "delete stretch" } | |
36 | |
37 func (dstJobCreator) AutoAccept() bool { return false } | |
38 | |
39 func (dstJobCreator) Create() Job { return new(DeleteStretch) } | |
40 | |
41 func (dstJobCreator) Depends() [2][]string { | |
42 return [2][]string{ | |
43 {"stretches"}, | |
44 {}, | |
45 } | |
46 } | |
47 | |
48 const ( | |
49 dstExistsSQL = ` | |
50 SELECT EXISTS ( | |
51 SELECT 1 FROM users.stretches | |
52 WHERE id = $1 AND staging_done) | |
53 ` | |
54 dstStageDoneSQL = ` | |
55 DELETE FROM users.stretches | |
56 WHERE id IN ( | |
57 SELECT key from import.track_imports | |
58 WHERE import_id = $1 AND | |
59 deletion AND | |
60 relation = 'users.stretches'::regclass)` | |
61 ) | |
62 | |
63 // StageDone finally removes the stretch from the database. | |
64 func (dstJobCreator) StageDone( | |
65 ctx context.Context, | |
66 tx *sql.Tx, | |
67 id int64, | |
68 ) error { | |
69 _, err := tx.ExecContext(ctx, dstStageDoneSQL, id) | |
70 return err | |
71 } | |
72 | |
73 // CleanUp of a stretch delete import is a NOP. | |
74 func (*DeleteStretch) CleanUp() error { return nil } | |
75 | |
76 // Do prepares the deletion of the stretch. | |
77 func (dst *DeleteStretch) Do( | |
78 ctx context.Context, | |
79 importID int64, | |
80 conn *sql.Conn, | |
81 feedback Feedback, | |
82 ) (interface{}, error) { | |
83 | |
84 tx, err := conn.BeginTx(ctx, nil) | |
85 if err != nil { | |
86 return nil, err | |
87 } | |
88 defer tx.Rollback() | |
89 | |
90 var found bool | |
91 if err := tx.QueryRowContext(ctx, dstExistsSQL, dst.ID).Scan(&found); err != nil { | |
92 return nil, err | |
93 } | |
94 | |
95 if !found { | |
96 return nil, fmt.Errorf("no stretch with id %d found", dst.ID) | |
97 } | |
98 | |
99 feedback.Info("Prepare deletion of stretch with id %d", dst.ID) | |
100 | |
101 if _, err := tx.ExecContext( | |
102 ctx, | |
103 trackImportDeletionSQL, | |
104 importID, | |
105 "users.stretches", | |
106 dst.ID, | |
107 true, | |
108 ); err != nil { | |
109 return nil, err | |
110 } | |
111 | |
112 if err := tx.Commit(); err != nil { | |
113 return nil, err | |
114 } | |
115 | |
116 return dst, nil | |
117 } |