1 INSERT INTO network_types (type) VALUES ('ipv6');
3 CREATE TABLE ip_addresses (
5 ip_address_id serial PRIMARY KEY, -- Network interface identifier
6 interface_id integer REFERENCES interfaces NOT NULL, -- Which interface
7 type text REFERENCES network_types NOT NULL, -- ipv4 or ipv6
8 ip_addr text NOT NULL, -- IP address
9 netmask text NOT NULL, -- Netmask
10 last_updated timestamp without time zone -- When the interface was last updated
12 CREATE INDEX ip_addresses_interface_id_idx ON ip_addresses (interface_id);
14 CREATE OR REPLACE VIEW interface_ip_addresses AS
16 array_accum(ip_address_id) AS ip_address_ids
18 GROUP BY interface_id;
20 CREATE OR REPLACE VIEW view_ip_addresses AS
22 ip_addresses.ip_address_id,
23 ip_addresses.interface_id,
27 CAST(date_part('epoch', ip_addresses.last_updated) AS bigint) AS last_updated
30 DROP VIEW view_interfaces;
31 DROP VIEW IF EXISTS tagvalue_view_interface_id_ifname;
32 DROP VIEW view_interface_tags;
34 INSERT INTO ip_addresses (interface_id, type, ip_addr, netmask)
35 SELECT interface_id, type, ip as ip_addr, netmask FROM interfaces;
37 ALTER TABLE interfaces DROP COLUMN type;
38 ALTER TABLE interfaces DROP COLUMN ip;
39 ALTER TABLE interfaces DROP COLUMN network;
40 ALTER TABLE interfaces DROP COLUMN broadcast;
41 ALTER TABLE interfaces DROP COLUMN netmask;
42 ALTER TABLE interfaces ADD COLUMN if_name text;
44 CREATE OR REPLACE VIEW view_interfaces AS
46 interfaces.interface_id,
48 interfaces.is_primary,
54 CAST(date_part('epoch', interfaces.last_updated) AS bigint) AS last_updated,
55 COALESCE((SELECT interface_tag_ids FROM interface_tags WHERE interface_tags.interface_id = interfaces.interface_id), '{}') AS interface_tag_ids,
56 COALESCE((SELECT ip_address_ids FROM interface_ip_addresses WHERE interface_ip_addresses.interface_id = interfaces.interface_id), '{}') AS ip_address_ids
59 CREATE OR REPLACE VIEW view_interface_tags AS
61 interface_tag.interface_tag_id,
62 interface_tag.interface_id,
63 tag_types.tag_type_id,
65 tag_types.description,
69 INNER JOIN tag_types USING (tag_type_id)
70 INNER JOIN interfaces USING (interface_id);
74 route_id serial PRIMARY KEY, -- Network interface identifier
75 node_id integer REFERENCES nodes NOT NULL, -- Which node
76 subnet text NOT NULL, -- Subnet route applies to
77 next_hop text NOT NULL, -- Next hop IP address
78 interface_id integer REFERENCES interfaces NOT NULL, -- Outgoing interface
79 last_updated timestamp without time zone -- When the interface was last updated
81 CREATE INDEX routes_node_id_idx ON routes (node_id);
83 CREATE OR REPLACE VIEW view_routes AS
90 CAST(date_part('epoch', routes.last_updated) AS bigint) AS last_updated
93 CREATE OR REPLACE VIEW node_routes AS
95 array_accum(route_id) AS route_ids
99 ALTER TABLE nodes ADD COLUMN dns text;
100 ALTER TABLE nodes ADD COLUMN ntp text;
102 -- Insert the default gateways from the primary interfaces on each node as
103 -- a route with subnet=0.0.0.0/0 and next_hop=gateway
104 INSERT INTO routes (subnet, node_id, interface_id, next_hop)
105 SELECT '0.0.0.0/0' as subnet, node_id, interface_id, gateway as next_hop FROM interfaces WHERE gateway is not NULL and is_primary is true;
107 -- Set the DNS fields in the nodes table from the DNS fields in the interfaces table
108 -- for primary interfaces with one dns server
109 UPDATE nodes SET dns = dns1 FROM interfaces
110 WHERE is_primary is True and nodes.node_id = interfaces.node_id and dns1 is not NULL and dns2 is NULL;
112 -- Set the DNS fields in the nodes table from the DNS fields in the interfaces table
113 -- for primary interfaces with two dns servers
114 UPDATE nodes SET dns = dns1 || ', ' || dns2 FROM interfaces
115 WHERE is_primary is True and nodes.node_id = interfaces.node_id and dns1 is not NULL and dns2 is not NULL;
117 -- now we can drop the gateway and dns columns from interface
118 ALTER TABLE interfaces DROP COLUMN gateway;
119 ALTER TABLE interfaces DROP COLUMN dns1;
120 ALTER TABLE interfaces DROP COLUMN dns2;
122 DROP VIEW view_nodes;
123 CREATE OR REPLACE VIEW view_nodes AS
140 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
141 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
142 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,
143 CAST(date_part('epoch', nodes.last_boot) AS bigint) AS last_boot,
144 CAST(date_part('epoch', nodes.last_download) AS bigint) AS last_download,
145 CAST(date_part('epoch', nodes.last_pcu_reboot) AS bigint) AS last_pcu_reboot,
146 CAST(date_part('epoch', nodes.last_pcu_confirmation) AS bigint) AS last_pcu_confirmation,
147 nodes.last_time_spent_online,
148 nodes.last_time_spent_offline,
150 peer_node.peer_node_id,
151 COALESCE((SELECT interface_ids FROM node_interfaces
152 WHERE node_interfaces.node_id = nodes.node_id), '{}')
154 COALESCE((SELECT nodegroup_ids FROM node_nodegroups
155 WHERE node_nodegroups.node_id = nodes.node_id), '{}')
157 COALESCE((SELECT slice_ids FROM node_slices
158 WHERE node_slices.node_id = nodes.node_id), '{}')
160 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist
161 WHERE node_slices_whitelist.node_id = nodes.node_id), '{}')
162 AS slice_ids_whitelist,
163 COALESCE((SELECT pcu_ids FROM node_pcus
164 WHERE node_pcus.node_id = nodes.node_id), '{}')
166 COALESCE((SELECT ports FROM node_pcus
167 WHERE node_pcus.node_id = nodes.node_id), '{}')
169 COALESCE((SELECT conf_file_ids FROM node_conf_files
170 WHERE node_conf_files.node_id = nodes.node_id), '{}')
172 COALESCE((SELECT node_tag_ids FROM node_tags
173 WHERE node_tags.node_id = nodes.node_id), '{}')
175 COALESCE((SELECT route_ids FROM node_routes
176 WHERE node_routes.node_id = nodes.node_id), '{}')
178 node_session.session_id AS session
180 LEFT JOIN peer_node USING (node_id)
181 LEFT JOIN node_session USING (node_id);
183 UPDATE plc_db_version SET subversion = 106;