You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ra...@apache.org on 2020/11/02 22:05:59 UTC

[trafficcontrol] branch master updated: Migration fixes (#5228)

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

rawlin 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 2da6408  Migration fixes (#5228)
2da6408 is described below

commit 2da6408444144f301f3bc955bd25ebc11c29fd84
Author: ocket8888 <oc...@apache.org>
AuthorDate: Mon Nov 2 15:05:48 2020 -0700

    Migration fixes (#5228)
    
    * Fix bad migration file name
    
    * Add COALESCEs for newly-nullable columns downgrading to non-nullable
    
    * Added todb tests action
    
    * Added database tests workflow
    
    * Remove now-unnecessary PR checklist item
    
    * Add CHANGELOG entry
    
    * Fix bad tests
---
 .github/actions/todb-tests/Dockerfile              | 24 ++++++++
 .github/actions/todb-tests/README.md               | 38 +++++++++++++
 .github/actions/todb-tests/action.yml              | 22 ++++++++
 .github/actions/todb-tests/entrypoint.sh           | 66 ++++++++++++++++++++++
 .github/workflows/traffic.ops.database.yml         | 37 ++++++++++++
 CHANGELOG.md                                       |  4 +-
 PULL_REQUEST_TEMPLATE.md                           |  1 -
 .../2020072700000000_remove_redundancy.sql         |  6 +-
 ...081108261100_add_server_ip_profile_trigger.sql} |  0
 9 files changed, 193 insertions(+), 5 deletions(-)

diff --git a/.github/actions/todb-tests/Dockerfile b/.github/actions/todb-tests/Dockerfile
new file mode 100644
index 0000000..132a4ae
--- /dev/null
+++ b/.github/actions/todb-tests/Dockerfile
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+FROM alpine:3.12
+
+RUN apk add --no-cache bash
+
+COPY entrypoint.sh /
+
+ENTRYPOINT /entrypoint.sh
diff --git a/.github/actions/todb-tests/README.md b/.github/actions/todb-tests/README.md
new file mode 100644
index 0000000..fd3ce70
--- /dev/null
+++ b/.github/actions/todb-tests/README.md
@@ -0,0 +1,38 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+-->
+
+# todb-tests Docker action
+This action runs Traffic Ops database tests.
+
+## Outputs
+
+### `exit-code`
+0 if the tests passed, 1 otherwise.
+
+## Example usage
+```yaml
+jobs:
+  tests:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout
+        uses: actions/checkout@master
+      - name: ./.github/actions/todb-tests
+```
diff --git a/.github/actions/todb-tests/action.yml b/.github/actions/todb-tests/action.yml
new file mode 100644
index 0000000..aeb78e9
--- /dev/null
+++ b/.github/actions/todb-tests/action.yml
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+name: 'todb-tests'
+description: 'Runs any and all tests for the Traffic Ops database'
+runs:
+  using: 'docker'
+  image: 'Dockerfile'
diff --git a/.github/actions/todb-tests/entrypoint.sh b/.github/actions/todb-tests/entrypoint.sh
new file mode 100755
index 0000000..08c83e6
--- /dev/null
+++ b/.github/actions/todb-tests/entrypoint.sh
@@ -0,0 +1,66 @@
+#!/bin/bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+set -e
+
+cd traffic_ops/app/db/migrations;
+
+# Ensure proper order
+SORTED="$(mktemp)";
+SORTEDDASHN="$(mktemp)";
+
+ls | sort > "$SORTED";
+ls | sort -n > "$SORTEDDASHN";
+
+CODE=0;
+
+if [ ! -z "$(diff $SORTED $SORTEDDASHN)" ]; then
+	echo "ERROR: expected sort -n and sort to give the same migration order:" >&2;
+	diff "$SORTED" "$SORTEDDASHN" >&2;
+	CODE=1;
+fi
+
+rm "$SORTED" "$SORTEDDASHN";
+
+# No two migrations may share a timestamp
+ls | cut -d _ -f 1 | uniq -d | while read -r file; do
+	echo "ERROR: more than one file uses timestamp $file - timestamps must be unique" >&2;
+	CODE=1;
+done
+
+# Files must be named like {{timestamp}}_{{migration name}}.sql
+for file in "$(ls)"; do
+	if ! [[ "$file" =~ [0-9]+_[^\.]+\.sql ]]; then
+		echo "ERROR: traffic_ops/app/db/migrations/$file: wrong filename, must match \d+_[^\\.]+\\.sql" >&2;
+		CODE=1;
+	fi
+done
+
+set +e;
+# All new migrations must use 16-digit timestamps.
+VIOLATING_FILES="$(ls | sort | cut -d _ -f 1 | sed -n -e '/2020061622101648/,$p' | tr '[:space:]' '\n' | grep -vE '^[0-9]{16}$')";
+set -e;
+
+if [[ ! -z "$VIOLATING_FILES" ]]; then
+	for file in "$VIOLATING_FILES"; do
+		echo "ERROR: traffic_ops/app/db/migrations/$file(name).sql: wrong filename, all migrations after 2020-06-16 must use 16-digit timestamps in their filenames" >&2;
+	done
+	CODE=1;
+fi
+
+exit "$CODE";
diff --git a/.github/workflows/traffic.ops.database.yml b/.github/workflows/traffic.ops.database.yml
new file mode 100644
index 0000000..26987a3
--- /dev/null
+++ b/.github/workflows/traffic.ops.database.yml
@@ -0,0 +1,37 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+name: Traffic Ops Database Tests
+
+on:
+  create:
+  push:
+    paths:
+      - traffic_ops/app/db/**
+  pull_request:
+    types: [opened, reopened, edited, synchronize]
+    paths:
+      - traffic_ops/app/db/**
+
+jobs:
+  documentation:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@master
+      - name: Rut Traffic Ops Database Tests
+        uses: ./.github/actions/todb-tests
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 086d8c0..c96b613 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,10 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 
 ## [unreleased]
 ### Fixed
-- Fixed #5216 - Removed duplicate button to link delivery service to server [Related Github issue](https://github.com/apache/trafficcontrol/issues/5216) 
+- Fixed #5216 - Removed duplicate button to link delivery service to server [Related Github issue](https://github.com/apache/trafficcontrol/issues/5216)
 - Fixed an issue where the jobs and servers table in Traffic Portal would not clear a column's filter when it's hidden
 - Fixed an issue with Traffic Router failing to authenticate if secrets are changed
 - Fixed validation error message for Traffic Ops `POST /api/x/profileparameters` route
+- Fixed an issue where downgrading the database would fail while having server interfaces with null gateways, MTU, and/or netmasks.
+- Fixed an issue where partial upgrades of the database would occasionally fail to apply 2020081108261100_add_server_ip_profile_trigger.
 - Fixed an issue where Traffic Router would erroneously return 503s or NXDOMAINs if the caches in a cachegroup were all unavailable for a client's requested IP version, rather than selecting caches from the next closest available cachegroup.
 
 ### Added
diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md
index cb6578d..db0fd0e 100644
--- a/PULL_REQUEST_TEMPLATE.md
+++ b/PULL_REQUEST_TEMPLATE.md
@@ -83,7 +83,6 @@ e.g.
 - [ ] This PR includes documentation OR I have explained why documentation is unnecessary
 - [ ] This PR includes an update to CHANGELOG.md OR such an update is not necessary
 - [ ] This PR includes any and all required license headers
-- [ ] This PR ensures that database migration sequence is correct OR this PR does not include a database migration
 - [ ] This PR **DOES NOT FIX A SERIOUS SECURITY VULNERABILITY** (see [the Apache Software Foundation's security guidelines](https://www.apache.org/security/) for details)
 
 
diff --git a/traffic_ops/app/db/migrations/2020072700000000_remove_redundancy.sql b/traffic_ops/app/db/migrations/2020072700000000_remove_redundancy.sql
index 66b078d..bc39d63 100644
--- a/traffic_ops/app/db/migrations/2020072700000000_remove_redundancy.sql
+++ b/traffic_ops/app/db/migrations/2020072700000000_remove_redundancy.sql
@@ -44,11 +44,11 @@ ALTER TABLE server ADD CONSTRAINT need_gateway_if_ip CHECK (ip_address IS NULL O
 ALTER TABLE server ADD CONSTRAINT need_netmask_if_ip CHECK (ip_address IS NULL OR ip_address = '' OR ip_netmask IS NOT NULL);
 
 UPDATE server SET ip_address = host(ip_address.address),
-  ip_netmask = host(netmask(ip_address.address)),
-  ip_gateway = host(ip_address.gateway),
+  ip_netmask = COALESCE(host(netmask(ip_address.address)), ''),
+  ip_gateway = COALESCE(host(ip_address.gateway), ''),
   ip_address_is_service = ip_address.service_address,
   interface_name = ip_address.interface,
-  interface_mtu = interface.mtu
+  interface_mtu = COALESCE(interface.mtu, 0)
   FROM ip_address
   JOIN interface ON ip_address.interface = interface.name
   WHERE server.id = ip_address.server
diff --git a/traffic_ops/app/db/migrations/20200811082611_add_server_ip_profile_trigger.sql b/traffic_ops/app/db/migrations/2020081108261100_add_server_ip_profile_trigger.sql
similarity index 100%
rename from traffic_ops/app/db/migrations/20200811082611_add_server_ip_profile_trigger.sql
rename to traffic_ops/app/db/migrations/2020081108261100_add_server_ip_profile_trigger.sql