Skip to content

Commit be01b5b

Browse files
committed
RHINENG-21214: handle inserts/updates to system_platform view
1 parent 4d1cdcc commit be01b5b

3 files changed

Lines changed: 313 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP TRIGGER IF EXISTS system_platform_update_trigger ON system_platform;
2+
DROP TRIGGER IF EXISTS system_platform_insert_trigger ON system_platform;
3+
4+
DROP FUNCTION IF EXISTS system_platform_update();
5+
DROP FUNCTION IF EXISTS system_platform_insert();
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
-- INSTEAD OF triggers for system_platform view to handle INSERT and UPDATE
2+
3+
CREATE OR REPLACE FUNCTION system_platform_insert()
4+
RETURNS TRIGGER AS
5+
$system_platform_insert$
6+
DECLARE
7+
new_system_id BIGINT;
8+
created TIMESTAMPTZ := CURRENT_TIMESTAMP;
9+
BEGIN
10+
INSERT INTO system_inventory (
11+
inventory_id,
12+
rh_account_id,
13+
vmaas_json,
14+
json_checksum,
15+
last_updated,
16+
unchanged_since,
17+
last_upload,
18+
stale_timestamp,
19+
stale_warning_timestamp,
20+
culled_timestamp,
21+
stale,
22+
display_name,
23+
reporter_id,
24+
yum_updates,
25+
satellite_managed,
26+
built_pkgcache,
27+
yum_checksum,
28+
arch,
29+
bootc,
30+
tags,
31+
created
32+
) VALUES (
33+
NEW.inventory_id,
34+
NEW.rh_account_id,
35+
NEW.vmaas_json,
36+
NEW.json_checksum,
37+
NEW.last_updated,
38+
NEW.unchanged_since,
39+
NEW.last_upload,
40+
NEW.stale_timestamp,
41+
NEW.stale_warning_timestamp,
42+
NEW.culled_timestamp,
43+
COALESCE(NEW.stale, false),
44+
NEW.display_name,
45+
NEW.reporter_id,
46+
NEW.yum_updates,
47+
COALESCE(NEW.satellite_managed, false),
48+
COALESCE(NEW.built_pkgcache, false),
49+
NEW.yum_checksum,
50+
NEW.arch,
51+
COALESCE(NEW.bootc, false),
52+
'[]'::JSONB,
53+
created
54+
)
55+
RETURNING id INTO new_system_id;
56+
57+
INSERT INTO system_patch (
58+
system_id,
59+
rh_account_id,
60+
last_evaluation,
61+
installable_advisory_count_cache,
62+
installable_advisory_enh_count_cache,
63+
installable_advisory_bug_count_cache,
64+
installable_advisory_sec_count_cache,
65+
packages_installed,
66+
packages_installable,
67+
packages_applicable,
68+
third_party,
69+
applicable_advisory_count_cache,
70+
applicable_advisory_enh_count_cache,
71+
applicable_advisory_bug_count_cache,
72+
applicable_advisory_sec_count_cache,
73+
template_id
74+
) VALUES (
75+
new_system_id,
76+
NEW.rh_account_id,
77+
NEW.last_evaluation,
78+
COALESCE(NEW.installable_advisory_count_cache, 0),
79+
COALESCE(NEW.installable_advisory_enh_count_cache, 0),
80+
COALESCE(NEW.installable_advisory_bug_count_cache, 0),
81+
COALESCE(NEW.installable_advisory_sec_count_cache, 0),
82+
COALESCE(NEW.packages_installed, 0),
83+
COALESCE(NEW.packages_installable, 0),
84+
COALESCE(NEW.packages_applicable, 0),
85+
COALESCE(NEW.third_party, false),
86+
COALESCE(NEW.applicable_advisory_count_cache, 0),
87+
COALESCE(NEW.applicable_advisory_enh_count_cache, 0),
88+
COALESCE(NEW.applicable_advisory_bug_count_cache, 0),
89+
COALESCE(NEW.applicable_advisory_sec_count_cache, 0),
90+
NEW.template_id
91+
);
92+
93+
NEW.id := new_system_id;
94+
RETURN NEW;
95+
END;
96+
$system_platform_insert$
97+
LANGUAGE 'plpgsql';
98+
99+
CREATE OR REPLACE FUNCTION system_platform_update()
100+
RETURNS TRIGGER AS
101+
$system_platform_update$
102+
BEGIN
103+
UPDATE system_inventory SET
104+
inventory_id = NEW.inventory_id,
105+
vmaas_json = NEW.vmaas_json,
106+
json_checksum = NEW.json_checksum,
107+
last_updated = NEW.last_updated,
108+
unchanged_since = NEW.unchanged_since,
109+
last_upload = NEW.last_upload,
110+
stale_timestamp = NEW.stale_timestamp,
111+
stale_warning_timestamp = NEW.stale_warning_timestamp,
112+
culled_timestamp = NEW.culled_timestamp,
113+
stale = NEW.stale,
114+
display_name = NEW.display_name,
115+
reporter_id = NEW.reporter_id,
116+
yum_updates = NEW.yum_updates,
117+
satellite_managed = NEW.satellite_managed,
118+
built_pkgcache = NEW.built_pkgcache,
119+
yum_checksum = NEW.yum_checksum,
120+
arch = NEW.arch,
121+
bootc = NEW.bootc
122+
WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id;
123+
124+
UPDATE system_patch SET
125+
last_evaluation = NEW.last_evaluation,
126+
installable_advisory_count_cache = NEW.installable_advisory_count_cache,
127+
installable_advisory_enh_count_cache = NEW.installable_advisory_enh_count_cache,
128+
installable_advisory_bug_count_cache = NEW.installable_advisory_bug_count_cache,
129+
installable_advisory_sec_count_cache = NEW.installable_advisory_sec_count_cache,
130+
packages_installed = NEW.packages_installed,
131+
packages_installable = NEW.packages_installable,
132+
packages_applicable = NEW.packages_applicable,
133+
third_party = NEW.third_party,
134+
applicable_advisory_count_cache = NEW.applicable_advisory_count_cache,
135+
applicable_advisory_enh_count_cache = NEW.applicable_advisory_enh_count_cache,
136+
applicable_advisory_bug_count_cache = NEW.applicable_advisory_bug_count_cache,
137+
applicable_advisory_sec_count_cache = NEW.applicable_advisory_sec_count_cache,
138+
template_id = NEW.template_id
139+
WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id;
140+
141+
RETURN NEW;
142+
END;
143+
$system_platform_update$
144+
LANGUAGE 'plpgsql';
145+
146+
CREATE TRIGGER system_platform_insert_trigger
147+
INSTEAD OF INSERT ON system_platform
148+
FOR EACH ROW
149+
EXECUTE FUNCTION system_platform_insert();
150+
151+
CREATE TRIGGER system_platform_update_trigger
152+
INSTEAD OF UPDATE ON system_platform
153+
FOR EACH ROW
154+
EXECUTE FUNCTION system_platform_update();

database_admin/schema/create_schema.sql

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations
77

88

99
INSERT INTO schema_migrations
10-
VALUES (142, false);
10+
VALUES (143, false);
1111

1212
-- ---------------------------------------------------------------------------
1313
-- Functions
@@ -1103,6 +1103,159 @@ CREATE OR REPLACE VIEW system_platform AS SELECT
11031103
FROM system_inventory si JOIN system_patch sp
11041104
ON si.id = sp.system_id AND si.rh_account_id = sp.rh_account_id;
11051105

1106+
CREATE OR REPLACE FUNCTION system_platform_insert()
1107+
RETURNS TRIGGER AS
1108+
$system_platform_insert$
1109+
DECLARE
1110+
new_system_id BIGINT;
1111+
created TIMESTAMPTZ := CURRENT_TIMESTAMP;
1112+
BEGIN
1113+
INSERT INTO system_inventory (
1114+
inventory_id,
1115+
rh_account_id,
1116+
vmaas_json,
1117+
json_checksum,
1118+
last_updated,
1119+
unchanged_since,
1120+
last_upload,
1121+
stale_timestamp,
1122+
stale_warning_timestamp,
1123+
culled_timestamp,
1124+
stale,
1125+
display_name,
1126+
reporter_id,
1127+
yum_updates,
1128+
satellite_managed,
1129+
built_pkgcache,
1130+
yum_checksum,
1131+
arch,
1132+
bootc,
1133+
tags,
1134+
created
1135+
) VALUES (
1136+
NEW.inventory_id,
1137+
NEW.rh_account_id,
1138+
NEW.vmaas_json,
1139+
NEW.json_checksum,
1140+
NEW.last_updated,
1141+
NEW.unchanged_since,
1142+
NEW.last_upload,
1143+
NEW.stale_timestamp,
1144+
NEW.stale_warning_timestamp,
1145+
NEW.culled_timestamp,
1146+
COALESCE(NEW.stale, false),
1147+
NEW.display_name,
1148+
NEW.reporter_id,
1149+
NEW.yum_updates,
1150+
COALESCE(NEW.satellite_managed, false),
1151+
COALESCE(NEW.built_pkgcache, false),
1152+
NEW.yum_checksum,
1153+
NEW.arch,
1154+
COALESCE(NEW.bootc, false),
1155+
'[]'::JSONB,
1156+
created
1157+
)
1158+
RETURNING id INTO new_system_id;
1159+
1160+
INSERT INTO system_patch (
1161+
system_id,
1162+
rh_account_id,
1163+
last_evaluation,
1164+
installable_advisory_count_cache,
1165+
installable_advisory_enh_count_cache,
1166+
installable_advisory_bug_count_cache,
1167+
installable_advisory_sec_count_cache,
1168+
packages_installed,
1169+
packages_installable,
1170+
packages_applicable,
1171+
third_party,
1172+
applicable_advisory_count_cache,
1173+
applicable_advisory_enh_count_cache,
1174+
applicable_advisory_bug_count_cache,
1175+
applicable_advisory_sec_count_cache,
1176+
template_id
1177+
) VALUES (
1178+
new_system_id,
1179+
NEW.rh_account_id,
1180+
NEW.last_evaluation,
1181+
COALESCE(NEW.installable_advisory_count_cache, 0),
1182+
COALESCE(NEW.installable_advisory_enh_count_cache, 0),
1183+
COALESCE(NEW.installable_advisory_bug_count_cache, 0),
1184+
COALESCE(NEW.installable_advisory_sec_count_cache, 0),
1185+
COALESCE(NEW.packages_installed, 0),
1186+
COALESCE(NEW.packages_installable, 0),
1187+
COALESCE(NEW.packages_applicable, 0),
1188+
COALESCE(NEW.third_party, false),
1189+
COALESCE(NEW.applicable_advisory_count_cache, 0),
1190+
COALESCE(NEW.applicable_advisory_enh_count_cache, 0),
1191+
COALESCE(NEW.applicable_advisory_bug_count_cache, 0),
1192+
COALESCE(NEW.applicable_advisory_sec_count_cache, 0),
1193+
NEW.template_id
1194+
);
1195+
1196+
NEW.id := new_system_id;
1197+
RETURN NEW;
1198+
END;
1199+
$system_platform_insert$
1200+
LANGUAGE 'plpgsql';
1201+
1202+
CREATE OR REPLACE FUNCTION system_platform_update()
1203+
RETURNS TRIGGER AS
1204+
$system_platform_update$
1205+
BEGIN
1206+
UPDATE system_inventory SET
1207+
inventory_id = NEW.inventory_id,
1208+
vmaas_json = NEW.vmaas_json,
1209+
json_checksum = NEW.json_checksum,
1210+
last_updated = NEW.last_updated,
1211+
unchanged_since = NEW.unchanged_since,
1212+
last_upload = NEW.last_upload,
1213+
stale_timestamp = NEW.stale_timestamp,
1214+
stale_warning_timestamp = NEW.stale_warning_timestamp,
1215+
culled_timestamp = NEW.culled_timestamp,
1216+
stale = NEW.stale,
1217+
display_name = NEW.display_name,
1218+
reporter_id = NEW.reporter_id,
1219+
yum_updates = NEW.yum_updates,
1220+
satellite_managed = NEW.satellite_managed,
1221+
built_pkgcache = NEW.built_pkgcache,
1222+
yum_checksum = NEW.yum_checksum,
1223+
arch = NEW.arch,
1224+
bootc = NEW.bootc
1225+
WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id;
1226+
1227+
UPDATE system_patch SET
1228+
last_evaluation = NEW.last_evaluation,
1229+
installable_advisory_count_cache = NEW.installable_advisory_count_cache,
1230+
installable_advisory_enh_count_cache = NEW.installable_advisory_enh_count_cache,
1231+
installable_advisory_bug_count_cache = NEW.installable_advisory_bug_count_cache,
1232+
installable_advisory_sec_count_cache = NEW.installable_advisory_sec_count_cache,
1233+
packages_installed = NEW.packages_installed,
1234+
packages_installable = NEW.packages_installable,
1235+
packages_applicable = NEW.packages_applicable,
1236+
third_party = NEW.third_party,
1237+
applicable_advisory_count_cache = NEW.applicable_advisory_count_cache,
1238+
applicable_advisory_enh_count_cache = NEW.applicable_advisory_enh_count_cache,
1239+
applicable_advisory_bug_count_cache = NEW.applicable_advisory_bug_count_cache,
1240+
applicable_advisory_sec_count_cache = NEW.applicable_advisory_sec_count_cache,
1241+
template_id = NEW.template_id
1242+
WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id;
1243+
1244+
RETURN NEW;
1245+
END;
1246+
$system_platform_update$
1247+
LANGUAGE 'plpgsql';
1248+
1249+
CREATE TRIGGER system_platform_insert_trigger
1250+
INSTEAD OF INSERT ON system_platform
1251+
FOR EACH ROW
1252+
EXECUTE FUNCTION system_platform_insert();
1253+
1254+
CREATE TRIGGER system_platform_update_trigger
1255+
INSTEAD OF UPDATE ON system_platform
1256+
FOR EACH ROW
1257+
EXECUTE FUNCTION system_platform_update();
1258+
11061259
GRANT SELECT, INSERT, UPDATE, DELETE ON system_platform TO listener;
11071260
-- evaluator needs to update last_evaluation
11081261
GRANT UPDATE ON system_platform TO evaluator;

0 commit comments

Comments
 (0)