|
| 1 | +-- system_platform |
| 2 | +DROP TABLE IF EXISTS system_platform; |
| 3 | +CREATE OR REPLACE VIEW system_platform AS SELECT |
| 4 | + si.id, |
| 5 | + si.inventory_id, |
| 6 | + si.rh_account_id, |
| 7 | + si.vmaas_json, |
| 8 | + si.json_checksum, |
| 9 | + si.last_updated, |
| 10 | + si.unchanged_since, |
| 11 | + sp.last_evaluation, |
| 12 | + sp.installable_advisory_count_cache, |
| 13 | + sp.installable_advisory_enh_count_cache, |
| 14 | + sp.installable_advisory_bug_count_cache, |
| 15 | + sp.installable_advisory_sec_count_cache, |
| 16 | + si.last_upload, |
| 17 | + si.stale_timestamp, |
| 18 | + si.stale_warning_timestamp, |
| 19 | + si.culled_timestamp, |
| 20 | + si.stale, |
| 21 | + si.display_name, |
| 22 | + sp.packages_installed, |
| 23 | + sp.packages_installable, |
| 24 | + si.reporter_id, |
| 25 | + sp.third_party, |
| 26 | + si.yum_updates, |
| 27 | + sp.applicable_advisory_count_cache, |
| 28 | + sp.applicable_advisory_enh_count_cache, |
| 29 | + sp.applicable_advisory_bug_count_cache, |
| 30 | + sp.applicable_advisory_sec_count_cache, |
| 31 | + si.satellite_managed, |
| 32 | + si.built_pkgcache, |
| 33 | + sp.packages_applicable, |
| 34 | + sp.template_id, |
| 35 | + si.yum_checksum, |
| 36 | + si.arch, |
| 37 | + si.bootc |
| 38 | +FROM system_inventory si JOIN system_patch sp |
| 39 | + ON si.id = sp.system_id AND si.rh_account_id = sp.rh_account_id; |
| 40 | + |
| 41 | +CREATE OR REPLACE FUNCTION system_platform_insert() |
| 42 | + RETURNS TRIGGER AS |
| 43 | +$system_platform_insert$ |
| 44 | +DECLARE |
| 45 | + new_system_id BIGINT; |
| 46 | + created TIMESTAMPTZ := CURRENT_TIMESTAMP; |
| 47 | +BEGIN |
| 48 | + INSERT INTO system_inventory ( |
| 49 | + inventory_id, |
| 50 | + rh_account_id, |
| 51 | + vmaas_json, |
| 52 | + json_checksum, |
| 53 | + last_updated, |
| 54 | + unchanged_since, |
| 55 | + last_upload, |
| 56 | + stale_timestamp, |
| 57 | + stale_warning_timestamp, |
| 58 | + culled_timestamp, |
| 59 | + stale, |
| 60 | + display_name, |
| 61 | + reporter_id, |
| 62 | + yum_updates, |
| 63 | + satellite_managed, |
| 64 | + built_pkgcache, |
| 65 | + yum_checksum, |
| 66 | + arch, |
| 67 | + bootc, |
| 68 | + tags, |
| 69 | + created |
| 70 | + ) VALUES ( |
| 71 | + NEW.inventory_id, |
| 72 | + NEW.rh_account_id, |
| 73 | + NEW.vmaas_json, |
| 74 | + NEW.json_checksum, |
| 75 | + NEW.last_updated, |
| 76 | + NEW.unchanged_since, |
| 77 | + NEW.last_upload, |
| 78 | + NEW.stale_timestamp, |
| 79 | + NEW.stale_warning_timestamp, |
| 80 | + NEW.culled_timestamp, |
| 81 | + COALESCE(NEW.stale, false), |
| 82 | + NEW.display_name, |
| 83 | + NEW.reporter_id, |
| 84 | + NEW.yum_updates, |
| 85 | + COALESCE(NEW.satellite_managed, false), |
| 86 | + COALESCE(NEW.built_pkgcache, false), |
| 87 | + NEW.yum_checksum, |
| 88 | + NEW.arch, |
| 89 | + COALESCE(NEW.bootc, false), |
| 90 | + '[]'::JSONB, |
| 91 | + created |
| 92 | + ) |
| 93 | + RETURNING id INTO new_system_id; |
| 94 | + |
| 95 | + INSERT INTO system_patch ( |
| 96 | + system_id, |
| 97 | + rh_account_id, |
| 98 | + last_evaluation, |
| 99 | + installable_advisory_count_cache, |
| 100 | + installable_advisory_enh_count_cache, |
| 101 | + installable_advisory_bug_count_cache, |
| 102 | + installable_advisory_sec_count_cache, |
| 103 | + packages_installed, |
| 104 | + packages_installable, |
| 105 | + packages_applicable, |
| 106 | + third_party, |
| 107 | + applicable_advisory_count_cache, |
| 108 | + applicable_advisory_enh_count_cache, |
| 109 | + applicable_advisory_bug_count_cache, |
| 110 | + applicable_advisory_sec_count_cache, |
| 111 | + template_id |
| 112 | + ) VALUES ( |
| 113 | + new_system_id, |
| 114 | + NEW.rh_account_id, |
| 115 | + NEW.last_evaluation, |
| 116 | + COALESCE(NEW.installable_advisory_count_cache, 0), |
| 117 | + COALESCE(NEW.installable_advisory_enh_count_cache, 0), |
| 118 | + COALESCE(NEW.installable_advisory_bug_count_cache, 0), |
| 119 | + COALESCE(NEW.installable_advisory_sec_count_cache, 0), |
| 120 | + COALESCE(NEW.packages_installed, 0), |
| 121 | + COALESCE(NEW.packages_installable, 0), |
| 122 | + COALESCE(NEW.packages_applicable, 0), |
| 123 | + COALESCE(NEW.third_party, false), |
| 124 | + COALESCE(NEW.applicable_advisory_count_cache, 0), |
| 125 | + COALESCE(NEW.applicable_advisory_enh_count_cache, 0), |
| 126 | + COALESCE(NEW.applicable_advisory_bug_count_cache, 0), |
| 127 | + COALESCE(NEW.applicable_advisory_sec_count_cache, 0), |
| 128 | + NEW.template_id |
| 129 | + ); |
| 130 | + |
| 131 | + NEW.id := new_system_id; |
| 132 | + RETURN NEW; |
| 133 | +END; |
| 134 | +$system_platform_insert$ |
| 135 | + LANGUAGE 'plpgsql'; |
| 136 | + |
| 137 | +CREATE OR REPLACE FUNCTION system_platform_update() |
| 138 | + RETURNS TRIGGER AS |
| 139 | +$system_platform_update$ |
| 140 | +BEGIN |
| 141 | + UPDATE system_inventory SET |
| 142 | + inventory_id = NEW.inventory_id, |
| 143 | + vmaas_json = NEW.vmaas_json, |
| 144 | + json_checksum = NEW.json_checksum, |
| 145 | + last_updated = NEW.last_updated, |
| 146 | + unchanged_since = NEW.unchanged_since, |
| 147 | + last_upload = NEW.last_upload, |
| 148 | + stale_timestamp = NEW.stale_timestamp, |
| 149 | + stale_warning_timestamp = NEW.stale_warning_timestamp, |
| 150 | + culled_timestamp = NEW.culled_timestamp, |
| 151 | + stale = NEW.stale, |
| 152 | + display_name = NEW.display_name, |
| 153 | + reporter_id = NEW.reporter_id, |
| 154 | + yum_updates = NEW.yum_updates, |
| 155 | + satellite_managed = NEW.satellite_managed, |
| 156 | + built_pkgcache = NEW.built_pkgcache, |
| 157 | + yum_checksum = NEW.yum_checksum, |
| 158 | + arch = NEW.arch, |
| 159 | + bootc = NEW.bootc |
| 160 | + WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id; |
| 161 | + |
| 162 | + UPDATE system_patch SET |
| 163 | + last_evaluation = NEW.last_evaluation, |
| 164 | + installable_advisory_count_cache = NEW.installable_advisory_count_cache, |
| 165 | + installable_advisory_enh_count_cache = NEW.installable_advisory_enh_count_cache, |
| 166 | + installable_advisory_bug_count_cache = NEW.installable_advisory_bug_count_cache, |
| 167 | + installable_advisory_sec_count_cache = NEW.installable_advisory_sec_count_cache, |
| 168 | + packages_installed = NEW.packages_installed, |
| 169 | + packages_installable = NEW.packages_installable, |
| 170 | + packages_applicable = NEW.packages_applicable, |
| 171 | + third_party = NEW.third_party, |
| 172 | + applicable_advisory_count_cache = NEW.applicable_advisory_count_cache, |
| 173 | + applicable_advisory_enh_count_cache = NEW.applicable_advisory_enh_count_cache, |
| 174 | + applicable_advisory_bug_count_cache = NEW.applicable_advisory_bug_count_cache, |
| 175 | + applicable_advisory_sec_count_cache = NEW.applicable_advisory_sec_count_cache, |
| 176 | + template_id = NEW.template_id |
| 177 | + WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id; |
| 178 | + |
| 179 | + RETURN NEW; |
| 180 | +END; |
| 181 | +$system_platform_update$ |
| 182 | + LANGUAGE 'plpgsql'; |
| 183 | + |
| 184 | +CREATE OR REPLACE FUNCTION system_platform_delete() |
| 185 | + RETURNS TRIGGER AS |
| 186 | +$system_platform_delete$ |
| 187 | +BEGIN |
| 188 | + DELETE FROM system_patch |
| 189 | + WHERE system_id = OLD.id AND rh_account_id = OLD.rh_account_id; |
| 190 | + |
| 191 | + DELETE FROM system_inventory |
| 192 | + WHERE id = OLD.id AND rh_account_id = OLD.rh_account_id; |
| 193 | + |
| 194 | + RETURN OLD; |
| 195 | +END; |
| 196 | +$system_platform_delete$ |
| 197 | + LANGUAGE 'plpgsql'; |
| 198 | + |
| 199 | +CREATE TRIGGER system_platform_insert_trigger |
| 200 | + INSTEAD OF INSERT ON system_platform |
| 201 | + FOR EACH ROW |
| 202 | + EXECUTE FUNCTION system_platform_insert(); |
| 203 | + |
| 204 | +CREATE TRIGGER system_platform_update_trigger |
| 205 | + INSTEAD OF UPDATE ON system_platform |
| 206 | + FOR EACH ROW |
| 207 | + EXECUTE FUNCTION system_platform_update(); |
| 208 | + |
| 209 | +CREATE TRIGGER system_platform_delete_trigger |
| 210 | + INSTEAD OF DELETE ON system_platform |
| 211 | + FOR EACH ROW |
| 212 | + EXECUTE FUNCTION system_platform_delete(); |
| 213 | + |
| 214 | +GRANT SELECT, INSERT, UPDATE, DELETE ON system_platform TO listener; |
| 215 | +-- evaluator needs to update last_evaluation |
| 216 | +GRANT UPDATE ON system_platform TO evaluator; |
| 217 | +-- manager needs to update cache and delete systems |
| 218 | +GRANT SELECT, UPDATE, DELETE ON system_platform TO manager; |
| 219 | +-- VMaaS sync needs to be able to perform system culling tasks |
| 220 | +GRANT SELECT, UPDATE, DELETE ON system_platform to vmaas_sync; |
0 commit comments