This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new de27021  change server primary key to only id (#4994)
de27021 is described below

commit de27021272f18f05da719d7ec356d520d31da0da
Author: mattjackson220 <[email protected]>
AuthorDate: Tue Sep 15 16:56:52 2020 -0600

    change server primary key to only id (#4994)
    
    * change server primary key to only id
    
    * updated so API doesnt accept existing server ids
---
 .../2020082700000000_server_id_primary_key.sql     | 41 ++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/server/servers.go   | 39 ++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git 
a/traffic_ops/app/db/migrations/2020082700000000_server_id_primary_key.sql 
b/traffic_ops/app/db/migrations/2020082700000000_server_id_primary_key.sql
new file mode 100644
index 0000000..3e4ed77
--- /dev/null
+++ b/traffic_ops/app/db/migrations/2020082700000000_server_id_primary_key.sql
@@ -0,0 +1,41 @@
+/*
+       Licensed under the Apache License, Version 2.0 (the "License");
+       you may not use this file except in compliance with the License.
+       You may obtain a copy of the License at
+               http://www.apache.org/licenses/LICENSE-2.0
+       Unless required by applicable law or agreed to in writing, software
+       distributed under the License is distributed on an "AS IS" BASIS,
+       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+       See the License for the specific language governing permissions and
+       limitations under the License.
+*/
+
+-- +goose Up
+-- SQL in section 'Up' is executed when this migration is applied
+-- +goose StatementBegin
+DO $$
+DECLARE r record;
+BEGIN
+  FOR r IN (SELECT indexname FROM pg_indexes WHERE tablename = 'server' AND 
indexname LIKE '%primary%')
+  LOOP
+    EXECUTE 'ALTER TABLE server DROP CONSTRAINT '|| quote_ident(r.indexname) 
|| ';';
+    EXECUTE 'ALTER TABLE ONLY server ADD CONSTRAINT '|| 
quote_ident(r.indexname) || ' PRIMARY KEY (id);';
+  END LOOP;
+END
+$$ LANGUAGE plpgsql;
+-- +goose StatementEnd
+
+-- +goose Down
+-- SQL section 'Down' is executed when this migration is rolled back
+-- +goose StatementBegin
+DO $$
+DECLARE r record;
+BEGIN
+  FOR r IN (SELECT indexname FROM pg_indexes WHERE tablename = 'server' AND 
indexname LIKE '%primary%')
+  LOOP
+    EXECUTE 'ALTER TABLE server DROP CONSTRAINT '|| quote_ident(r.indexname) 
|| ';';
+    EXECUTE 'ALTER TABLE ONLY server ADD CONSTRAINT '|| 
quote_ident(r.indexname) || ' PRIMARY KEY (id, cachegroup, type, status, 
profile);';
+  END LOOP;
+END
+$$ LANGUAGE plpgsql;
+-- +goose StatementEnd
diff --git a/traffic_ops/traffic_ops_golang/server/servers.go 
b/traffic_ops/traffic_ops_golang/server/servers.go
index 6df40ba..7d4160e 100644
--- a/traffic_ops/traffic_ops_golang/server/servers.go
+++ b/traffic_ops/traffic_ops_golang/server/servers.go
@@ -1329,6 +1329,19 @@ func createV1(inf *api.APIInfo, w http.ResponseWriter, r 
*http.Request) {
                return
        }
 
+       if server.ID != nil {
+               var prevID int
+               err := tx.QueryRow("SELECT id from server where id = $1", 
server.ID).Scan(&prevID)
+               if err != nil && err != sql.ErrNoRows {
+                       api.HandleErr(w, r, tx, http.StatusInternalServerError, 
nil, errors.New(fmt.Sprintf("checking if server with id %d exists", 
*server.ID)))
+                       return
+               }
+               if prevID != 0 {
+                       api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New(fmt.Sprintf("server with id %d already exists. Please do not provide 
an id.", *server.ID)), nil)
+                       return
+               }
+       }
+
        if err := validateV1(&server, tx); err != nil {
                api.HandleErr(w, r, tx, http.StatusBadRequest, err, nil)
                return
@@ -1385,6 +1398,19 @@ func createV2(inf *api.APIInfo, w http.ResponseWriter, r 
*http.Request) {
                return
        }
 
+       if server.ID != nil {
+               var prevID int
+               err := tx.QueryRow("SELECT id from server where id = $1", 
server.ID).Scan(&prevID)
+               if err != nil && err != sql.ErrNoRows {
+                       api.HandleErr(w, r, tx, http.StatusInternalServerError, 
nil, errors.New(fmt.Sprintf("checking if server with id %d exists", 
*server.ID)))
+                       return
+               }
+               if prevID != 0 {
+                       api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New(fmt.Sprintf("server with id %d already exists. Please do not provide 
an id.", *server.ID)), nil)
+                       return
+               }
+       }
+
        str := uuid.New().String()
        server.XMPPID = &str
 
@@ -1443,6 +1469,19 @@ func createV3(inf *api.APIInfo, w http.ResponseWriter, r 
*http.Request) {
                return
        }
 
+       if server.ID != nil {
+               var prevID int
+               err := tx.QueryRow("SELECT id from server where id = $1", 
server.ID).Scan(&prevID)
+               if err != nil && err != sql.ErrNoRows {
+                       api.HandleErr(w, r, tx, http.StatusInternalServerError, 
nil, errors.New(fmt.Sprintf("checking if server with id %d exists", 
*server.ID)))
+                       return
+               }
+               if prevID != 0 {
+                       api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New(fmt.Sprintf("server with id %d already exists. Please do not provide 
an id.", *server.ID)), nil)
+                       return
+               }
+       }
+
        str := uuid.New().String()
        server.XMPPID = &str
        _, err := validateV3(&server, tx)

Reply via email to