annotate pkg/imports/dsec.go @ 4562:5cc4042cf07c iso-areas

Started with integrating iso area generation into gemma server. WIP
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Oct 2019 18:35:09 +0200
parents 3dbe05de54ea
children e1cf039ea25a
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"
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "fmt"
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 )
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 // 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
23 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
24 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
25 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 // 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
28 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
29
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 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
31
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 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
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 (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
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) 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
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) 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
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) 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
41 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
42 {"sections"},
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 {},
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
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 const (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 dsecExistsSQL = `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 SELECT EXISTS (
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 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
51 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
52 `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 dsecStageDoneSQL = `
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 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
55 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
56 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
57 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
58 deletion AND
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 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
60 )
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 // 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
63 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
64 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
65 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
66 id int64,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 ) error {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 _, err := tx.ExecContext(ctx, dsecStageDoneSQL, id)
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return err
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 // 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
73 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
74
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 // 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
76 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
77 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
78 importID int64,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 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
80 feedback Feedback,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 ) (interface{}, error) {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 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
84 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
85 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
86 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 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
88
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 var found bool
4191
a1cc14c9ce69 Delete section import: Fixed SQL usage.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4188
diff changeset
90 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
91 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
92 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 if !found {
4276
3dbe05de54ea Fixed typo in import logging message.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4192
diff changeset
95 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
96 }
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 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
99
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 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
101 ctx,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 trackImportDeletionSQL,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 importID,
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 "waterway.sections",
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 dsec.ID,
4192
095731c880b4 Delete section import: Fixed swapped arguments for tracking.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4191
diff changeset
106 true,
4188
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 ); err != nil {
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 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
109 }
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110
f8b7db7e392a Added an "import" job to delete a section from the database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 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
112 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
113 }
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 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
116 }