From 0b4f8eb2937dc0ace4bce62db0029efed00254ba Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Tue, 14 Apr 2020 17:54:37 -0600 Subject: [PATCH 1/5] Added (hopefully) working goose up section --- .../migrations/20200414000000_interfaces.sql | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 traffic_ops/app/db/migrations/20200414000000_interfaces.sql diff --git a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql new file mode 100644 index 0000000000..925df528cf --- /dev/null +++ b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql @@ -0,0 +1,150 @@ +/* + 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 +CREATE TABLE interface ( + max_bandwidth bigint DEFAULT NULL CHECK (max_bandwidth IS NULL OR max_bandwidth >= 0), + monitor boolean NOT NULL, + mtu bigint DEFAULT 1500 CHECK (mtu IS NULL OR mtu > 1280), + name text NOT NULL CHECK (name != ''), + server bigint NOT NULL, + PRIMARY KEY (name, server), + FOREIGN KEY (server) REFERENCES server(id) +); +CREATE TABLE ip_address ( + address inet NOT NULL, + gateway inet CHECK (gateway IS NULL OR masklen(gateway) = 32), + interface text NOT NULL, + server bigint NOT NULL, + service_address boolean NOT NULL DEFAULT FALSE, + PRIMARY KEY (address, interface, server), + FOREIGN KEY (server) REFERENCES server(id), + FOREIGN KEY (interface, server) REFERENCES interface(name, server) +); + +INSERT INTO interface( + max_bandwidth, + monitor, + mtu, + name, + server +) +SELECT NULL, FALSE, NULL, 'mgmt', id +FROM server +WHERE server.mgmt_ip_address IS NOT NULL +AND server.mgmt_ip_address != ''; + +INSERT INTO interface( + max_bandwidth, + monitor, + mtu, + name, + server +) +SELECT NULL, TRUE, server.interface_mtu::bigint, server.interface_name, id +FROM server; + +INSERT INTO ip_address( + address, + gateway, + interface, + server, + service_address +) +SELECT + set_masklen( + server.mgmt_ip_address::inet, + CASE + WHEN server.mgmt_ip_netmask IS NULL THEN 32 + WHEN server.mgmt_ip_netmask = '' THEN 32 + ELSE + (SELECT SUM( + get_bit(digit, 0) + + get_bit(digit, 1) + + get_bit(digit, 2) + + get_bit(digit, 3) + + get_bit(digit, 4) + + get_bit(digit, 5) + + get_bit(digit, 6) + + get_bit(digit, 7) + )::int FROM ( + SELECT regexp_split_to_table::int::bit(8) AS digit + FROM regexp_split_to_table(server.mgmt_ip_netmask, '\.') + ) AS digits) + END + ), + NULLIF(server.mgmt_ip_gateway, '')::inet, + 'mgmt', + server.id, + FALSE +FROM server +WHERE server.mgmt_ip_address IS NOT NULL +AND server.mgmt_ip_address != ''; + +INSERT INTO ip_address( + address, + gateway, + interface, + server, + service_address +) +SELECT + set_masklen( + server.ip_address::inet, + CASE + WHEN server.ip_netmask IS NULL THEN 32 + WHEN server.ip_netmask = '' THEN 32 + ELSE + (SELECT SUM( + get_bit(digit, 0) + + get_bit(digit, 1) + + get_bit(digit, 2) + + get_bit(digit, 3) + + get_bit(digit, 4) + + get_bit(digit, 5) + + get_bit(digit, 6) + + get_bit(digit, 7) + )::int FROM ( + SELECT regexp_split_to_table::int::bit(8) AS digit + FROM regexp_split_to_table(server.ip_netmask, '\.') + ) AS digits) + END + ), + NULLIF(server.ip_gateway, '')::inet, + server.interface_name, + server.id, + server.ip_address_is_service +FROM server +WHERE server.ip_address IS NOT NULL +AND server.ip_address != ''; + +INSERT INTO ip_address( + address, + gateway, + interface, + server, + service_address +) +SELECT + trim(BOTH '[]' FROM server.ip6_address)::inet, + NULLIF(server.ip6_gateway, '')::inet, + server.interface_name, + server.id, + server.ip6_address_is_service +FROM server +WHERE server.ip6_address IS NOT NULL +AND server.ip6_address != ''; + +-- +goose Down +DROP TABLE IF EXISTS ip_address; From 9656124046609a71aa628db2a7e9c1ce33f4a3eb Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Tue, 14 Apr 2020 19:06:58 -0600 Subject: [PATCH 2/5] finished 'down' section, added 'ON UPDATE/DELETE' clauses --- .../app/db/migrations/20200414000000_interfaces.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql index 925df528cf..d96e191d12 100644 --- a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql +++ b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql @@ -20,7 +20,7 @@ CREATE TABLE interface ( name text NOT NULL CHECK (name != ''), server bigint NOT NULL, PRIMARY KEY (name, server), - FOREIGN KEY (server) REFERENCES server(id) + FOREIGN KEY (server) REFERENCES server(id) ON DELETE RESTRICT ON UPDATE CASCADE ); CREATE TABLE ip_address ( address inet NOT NULL, @@ -29,8 +29,8 @@ CREATE TABLE ip_address ( server bigint NOT NULL, service_address boolean NOT NULL DEFAULT FALSE, PRIMARY KEY (address, interface, server), - FOREIGN KEY (server) REFERENCES server(id), - FOREIGN KEY (interface, server) REFERENCES interface(name, server) + FOREIGN KEY (server) REFERENCES server(id) ON DELETE RESTRICT ON UPDATE CASCADE, + FOREIGN KEY (interface, server) REFERENCES interface(name, server) ON DELETE RESTRICT ON UPDATE CASCADE ); INSERT INTO interface( @@ -148,3 +148,4 @@ AND server.ip6_address != ''; -- +goose Down DROP TABLE IF EXISTS ip_address; +DROP TABLE IF EXISTS interface; From dedf0d38936c9dabd3942a3ed1a370b886f8ccdf Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Thu, 16 Apr 2020 10:44:28 -0600 Subject: [PATCH 3/5] Fixed check to handle IPv6, trim existing subnets from gateways --- .../db/migrations/20200414000000_interfaces.sql | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql index d96e191d12..800cbc2268 100644 --- a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql +++ b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql @@ -24,7 +24,15 @@ CREATE TABLE interface ( ); CREATE TABLE ip_address ( address inet NOT NULL, - gateway inet CHECK (gateway IS NULL OR masklen(gateway) = 32), + gateway inet CHECK ( + gateway IS NULL OR ( + family(gateway) = 4 AND + masklen(gateway) = 32 + ) OR ( + family(gateway) = 6 AND + masklen(gateway) = 128 + ) + ), interface text NOT NULL, server bigint NOT NULL, service_address boolean NOT NULL DEFAULT FALSE, @@ -84,7 +92,7 @@ SELECT ) AS digits) END ), - NULLIF(server.mgmt_ip_gateway, '')::inet, + NULLIF(regexp_replace(server.mgmt_ip_gateway, '/\d+$', ''), '')::inet, 'mgmt', server.id, FALSE @@ -121,7 +129,7 @@ SELECT ) AS digits) END ), - NULLIF(server.ip_gateway, '')::inet, + NULLIF(regexp_replace(server.ip_gateway, '/\d+$', '')::inet, server.interface_name, server.id, server.ip_address_is_service @@ -138,7 +146,7 @@ INSERT INTO ip_address( ) SELECT trim(BOTH '[]' FROM server.ip6_address)::inet, - NULLIF(server.ip6_gateway, '')::inet, + NULLIF(regexp_replace(server.ip6_gateway, '/\d+$', ''), '')::inet, server.interface_name, server.id, server.ip6_address_is_service From ce23f3b897ba640fc82a450dd069ef4b469d3399 Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Mon, 20 Apr 2020 09:34:49 -0600 Subject: [PATCH 4/5] Fixed typo --- traffic_ops/app/db/migrations/20200414000000_interfaces.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql index 800cbc2268..4e61c65070 100644 --- a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql +++ b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql @@ -129,7 +129,7 @@ SELECT ) AS digits) END ), - NULLIF(regexp_replace(server.ip_gateway, '/\d+$', '')::inet, + NULLIF(regexp_replace(server.ip_gateway, '/\d+$', ''), '')::inet, server.interface_name, server.id, server.ip_address_is_service From 5bf63016ccc170a120310d5238bbf392eeafd8c3 Mon Sep 17 00:00:00 2001 From: ocket8888 Date: Tue, 21 Apr 2020 16:38:22 -0600 Subject: [PATCH 5/5] Added type aliases for ease of array selection --- traffic_ops/app/db/migrations/20200414000000_interfaces.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql index 4e61c65070..85e9c39e55 100644 --- a/traffic_ops/app/db/migrations/20200414000000_interfaces.sql +++ b/traffic_ops/app/db/migrations/20200414000000_interfaces.sql @@ -41,6 +41,9 @@ CREATE TABLE ip_address ( FOREIGN KEY (interface, server) REFERENCES interface(name, server) ON DELETE RESTRICT ON UPDATE CASCADE ); +CREATE TYPE server_ip_address AS (address inet, gateway inet, service_address boolean); +CREATE TYPE server_interface AS (ip_addresses server_ip_address ARRAY, max_bandwidth bigint, monitor boolean, mtu bigint, name text); + INSERT INTO interface( max_bandwidth, monitor,