annotate pkg/imports/dsec.go @ 4678:e1cf039ea25a

Section deletion: return error if nothing deleted If nothing is deleted on acceptance of such an import, it's probably because row level security does not allow deletion of the entry.
author Tom Gottfried <tom@intevation.de>
date Tue, 15 Oct 2019 17:25:23 +0200
parents 3dbe05de54ea
children 4847ac70103a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package imports
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "context"
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
4678
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
19 "errors"
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "fmt"
4678
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
21 "log"
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 )
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 // DeleteSection is a Job to delete a section from the database.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 type DeleteSection struct {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 ID int64 `json:"id"`
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 // DSECJobKind is the import queue type identifier.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 const DSECJobKind JobKind = "dsec"
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 type dsecJobCreator struct{}
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 func init() { RegisterJobCreator(DSECJobKind, dsecJobCreator{}) }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 func (dsecJobCreator) Description() string { return "delete section" }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 func (dsecJobCreator) AutoAccept() bool { return false }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 func (dsecJobCreator) Create() Job { return new(DeleteSection) }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 func (dsecJobCreator) Depends() [2][]string {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 return [2][]string{
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 {"sections"},
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 {},
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 const (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 dsecExistsSQL = `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 SELECT EXISTS (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 SELECT 1 FROM waterway.sections
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 WHERE id = $1 AND staging_done)
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 dsecStageDoneSQL = `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 DELETE FROM waterway.sections
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 WHERE id IN (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 SELECT key from import.track_imports
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 WHERE import_id = $1 AND
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 deletion AND
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 relation = 'waterway.sections'::regclass)`
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 )
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 // StageDone finally removes the section from the database.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 func (dsecJobCreator) StageDone(
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 ctx context.Context,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 tx *sql.Tx,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 id int64,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 ) error {
4678
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
70 res, err := tx.ExecContext(ctx, dsecStageDoneSQL, id)
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
71 if err != nil {
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
72 return err
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
73 }
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
74 n, err := res.RowsAffected()
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
75 if err != nil {
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
76 log.Println(
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
77 "error: RowsAffected returned an error. Missing driver support?")
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
78 return nil
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
79 }
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
80 if n == 0 {
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
81 return errors.New(
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
82 "Nothing deleted. Probably due to missing access rights.")
e1cf039ea25a Section deletion: return error if nothing deleted
Tom Gottfried <tom@intevation.de>
parents: 4276
diff changeset
83 }
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 return err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 // CleanUp of a section delete import is a NOP.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 func (*DeleteSection) CleanUp() error { return nil }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 // Do prepares the deletion of the section.
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 func (dsec *DeleteSection) Do(
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 ctx context.Context,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 importID int64,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 conn *sql.Conn,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 feedback Feedback,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 ) (interface{}, error) {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 tx, err := conn.BeginTx(ctx, nil)
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 if err != nil {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 return nil, err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 defer tx.Rollback()
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 var found bool
4191
a1cc14c9ce69 Delete section import: Fixed SQL usage.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4188
diff changeset
105 if err := tx.QueryRowContext(ctx, dsecExistsSQL, dsec.ID).Scan(&found); err != nil {
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 return nil, err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 if !found {
4276
3dbe05de54ea Fixed typo in import logging message.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4192
diff changeset
110 return nil, fmt.Errorf("no section with id %d found", dsec.ID)
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 feedback.Info("Prepare deletion of section with id %d", dsec.ID)
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 if _, err := tx.ExecContext(
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 ctx,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 trackImportDeletionSQL,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 importID,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 "waterway.sections",
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 dsec.ID,
4192
095731c880b4 Delete section import: Fixed swapped arguments for tracking.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4191
diff changeset
121 true,
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 ); err != nil {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 return nil, err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 if err := tx.Commit(); err != nil {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 return nil, err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 return dsec, nil
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 }