Skip to content

Commit c81ac01

Browse files
committed
RHINENG-25740: schema migrations 150
1 parent 6a2c48f commit c81ac01

2 files changed

Lines changed: 210 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
DROP FUNCTION IF EXISTS delete_system(inventory_id_in uuid);
2+
CREATE OR REPLACE FUNCTION delete_system(inventory_id_in uuid)
3+
RETURNS TABLE
4+
(
5+
deleted_inventory_id uuid
6+
)
7+
AS
8+
$delete_system$
9+
DECLARE
10+
v_system_id INT;
11+
v_account_id INT;
12+
BEGIN
13+
-- opt out to refresh cache and then delete
14+
SELECT id, rh_account_id
15+
FROM system_inventory
16+
WHERE inventory_id = inventory_id_in
17+
LIMIT 1
18+
FOR UPDATE OF system_inventory
19+
INTO v_system_id, v_account_id;
20+
21+
IF v_system_id IS NULL OR v_account_id IS NULL THEN
22+
RAISE NOTICE 'Not found';
23+
RETURN;
24+
END IF;
25+
26+
UPDATE system_inventory
27+
SET stale = true
28+
WHERE rh_account_id = v_account_id
29+
AND id = v_system_id;
30+
31+
DELETE
32+
FROM system_advisories
33+
WHERE rh_account_id = v_account_id
34+
AND system_id = v_system_id;
35+
36+
DELETE
37+
FROM system_repo
38+
WHERE rh_account_id = v_account_id
39+
AND system_id = v_system_id;
40+
41+
DELETE
42+
FROM system_package2
43+
WHERE rh_account_id = v_account_id
44+
AND system_id = v_system_id;
45+
46+
DELETE
47+
FROM system_patch
48+
WHERE rh_account_id = v_account_id
49+
AND system_id = v_system_id;
50+
51+
RETURN QUERY DELETE FROM system_inventory
52+
WHERE rh_account_id = v_account_id AND
53+
id = v_system_id
54+
RETURNING inventory_id;
55+
END;
56+
$delete_system$ LANGUAGE 'plpgsql';
57+
58+
CREATE OR REPLACE FUNCTION delete_systems(inventory_ids UUID[])
59+
RETURNS INTEGER
60+
AS
61+
$$
62+
DECLARE
63+
tmp_cnt INTEGER;
64+
BEGIN
65+
66+
WITH systems as (
67+
SELECT rh_account_id, id
68+
FROM system_inventory
69+
WHERE inventory_id = ANY (inventory_ids)
70+
ORDER BY rh_account_id, id FOR UPDATE OF system_inventory),
71+
marked as (
72+
UPDATE system_inventory sp
73+
SET stale = true
74+
WHERE (rh_account_id, id) in (select rh_account_id, id from systems)
75+
),
76+
advisories as (
77+
DELETE
78+
FROM system_advisories
79+
WHERE (rh_account_id, system_id) in (select rh_account_id, id from systems)
80+
),
81+
repos as (
82+
DELETE
83+
FROM system_repo
84+
WHERE (rh_account_id, system_id) in (select rh_account_id, id from systems)
85+
),
86+
packages2 as (
87+
DELETE
88+
FROM system_package2
89+
WHERE (rh_account_id, system_id) in (select rh_account_id, id from systems)
90+
),
91+
patch_systems as (
92+
DELETE
93+
FROM system_patch
94+
WHERE (rh_account_id, system_id) in (select rh_account_id, id from systems)
95+
),
96+
deleted as (
97+
DELETE
98+
FROM system_inventory
99+
WHERE (rh_account_id, id) in (select rh_account_id, id from systems)
100+
RETURNING id
101+
)
102+
SELECT count(*)
103+
FROM deleted
104+
INTO tmp_cnt;
105+
106+
RETURN tmp_cnt;
107+
END
108+
$$ LANGUAGE plpgsql;
109+
110+
CREATE OR REPLACE FUNCTION delete_culled_systems(delete_limit INTEGER)
111+
RETURNS INTEGER
112+
AS
113+
$fun$
114+
DECLARE
115+
ids UUID[];
116+
BEGIN
117+
ids := ARRAY(
118+
SELECT inventory_id
119+
FROM system_inventory
120+
WHERE culled_timestamp < now()
121+
ORDER BY id
122+
LIMIT delete_limit
123+
);
124+
return delete_systems(ids);
125+
END;
126+
$fun$ LANGUAGE plpgsql;
127+
128+
CREATE OR REPLACE FUNCTION mark_stale_systems(mark_limit integer)
129+
RETURNS INTEGER
130+
AS
131+
$fun$
132+
DECLARE
133+
marked integer;
134+
BEGIN
135+
WITH ids AS (
136+
SELECT rh_account_id, id, stale_warning_timestamp < now() as expired
137+
FROM system_inventory
138+
WHERE stale != (stale_warning_timestamp < now())
139+
ORDER BY rh_account_id, id FOR UPDATE OF system_inventory
140+
LIMIT mark_limit
141+
)
142+
UPDATE system_inventory si
143+
SET stale = ids.expired
144+
FROM ids
145+
WHERE si.rh_account_id = ids.rh_account_id
146+
AND si.id = ids.id;
147+
GET DIAGNOSTICS marked = ROW_COUNT;
148+
RETURN marked;
149+
END;
150+
$fun$ LANGUAGE plpgsql;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
DROP FUNCTION IF EXISTS delete_system(inventory_id_in uuid);
2+
CREATE OR REPLACE FUNCTION delete_system(inventory_id_in uuid)
3+
RETURNS uuid
4+
AS
5+
$delete_system$
6+
DECLARE
7+
v_system_id INT;
8+
v_account_id INT;
9+
v_inventory_id uuid;
10+
BEGIN
11+
-- opt out to refresh cache and then delete
12+
SELECT id, rh_account_id
13+
FROM system_inventory
14+
WHERE inventory_id = inventory_id_in
15+
LIMIT 1
16+
FOR UPDATE OF system_inventory
17+
INTO v_system_id, v_account_id;
18+
19+
IF v_system_id IS NULL OR v_account_id IS NULL THEN
20+
RAISE NOTICE 'Not found';
21+
RETURN NULL;
22+
END IF;
23+
24+
UPDATE system_inventory
25+
SET stale = true
26+
WHERE rh_account_id = v_account_id
27+
AND id = v_system_id;
28+
29+
DELETE
30+
FROM system_advisories
31+
WHERE rh_account_id = v_account_id
32+
AND system_id = v_system_id;
33+
34+
DELETE
35+
FROM system_repo
36+
WHERE rh_account_id = v_account_id
37+
AND system_id = v_system_id;
38+
39+
DELETE
40+
FROM system_package2
41+
WHERE rh_account_id = v_account_id
42+
AND system_id = v_system_id;
43+
44+
DELETE
45+
FROM system_patch
46+
WHERE rh_account_id = v_account_id
47+
AND system_id = v_system_id;
48+
49+
DELETE FROM system_inventory
50+
WHERE rh_account_id = v_account_id AND
51+
id = v_system_id
52+
RETURNING inventory_id INTO v_inventory_id;
53+
54+
RETURN v_inventory_id;
55+
END;
56+
$delete_system$ LANGUAGE 'plpgsql';
57+
58+
DROP FUNCTION IF EXISTS delete_systems(UUID[]);
59+
DROP FUNCTION IF EXISTS delete_culled_systems(INTEGER);
60+
DROP FUNCTION IF EXISTS mark_stale_systems(INTEGER);

0 commit comments

Comments
 (0)