Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/sqale-closure-update'
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydamage committed Oct 2, 2023
2 parents 0d04ef1 + a72c04a commit 1cf0a5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
27 changes: 27 additions & 0 deletions config/sql/native/postgres-upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,33 @@ call apply_change(24, $aa$
ALTER TABLE m_connector ADD displayNameNorm TEXT;
$aa$);


call apply_change(25, $aa$
CREATE OR REPLACE PROCEDURE m_refresh_org_closure(force boolean = false)
LANGUAGE plpgsql
AS $$
DECLARE
flag_val text;
BEGIN
-- We use advisory session lock only for the check + refresh, then release it immediately.
-- This can still dead-lock two transactions in a single thread on the select/delete combo,
-- (I mean, who would do that?!) but works fine for parallel transactions.
PERFORM pg_advisory_lock(47);
BEGIN
SELECT value INTO flag_val FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
IF flag_val = 'true' OR force THEN
REFRESH MATERIALIZED VIEW m_org_closure;
DELETE FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
END IF;
PERFORM pg_advisory_unlock(47);
EXCEPTION WHEN OTHERS THEN
-- Whatever happens we definitely want to release the lock.
PERFORM pg_advisory_unlock(47);
RAISE;
END;
END;
$$;
$aa$);
---
-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_change number at the end of postgres-new.sql
Expand Down
40 changes: 19 additions & 21 deletions config/sql/native/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -897,26 +897,24 @@ AS $$
DECLARE
flag_val text;
BEGIN
SELECT value INTO flag_val FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
IF flag_val = 'true' OR force THEN
-- We use advisory session lock only for the check + refresh, then release it immediately.
-- This can still dead-lock two transactions in a single thread on the select/delete combo,
-- (I mean, who would do that?!) but works fine for parallel transactions.
PERFORM pg_advisory_lock(47);
BEGIN
SELECT value INTO flag_val FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
IF flag_val = 'true' OR force THEN
REFRESH MATERIALIZED VIEW m_org_closure;
DELETE FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
END IF;
PERFORM pg_advisory_unlock(47);
EXCEPTION WHEN OTHERS THEN
-- Whatever happens we definitely want to release the lock.
PERFORM pg_advisory_unlock(47);
RAISE;
END;
END IF;
END; $$;
-- We use advisory session lock only for the check + refresh, then release it immediately.
-- This can still dead-lock two transactions in a single thread on the select/delete combo,
-- (I mean, who would do that?!) but works fine for parallel transactions.
PERFORM pg_advisory_lock(47);
BEGIN
SELECT value INTO flag_val FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
IF flag_val = 'true' OR force THEN
REFRESH MATERIALIZED VIEW m_org_closure;
DELETE FROM m_global_metadata WHERE name = 'orgClosureRefreshNeeded';
END IF;
PERFORM pg_advisory_unlock(47);
EXCEPTION WHEN OTHERS THEN
-- Whatever happens we definitely want to release the lock.
PERFORM pg_advisory_unlock(47);
RAISE;
END;
END;
$$;
-- endregion

-- region OTHER object tables
Expand Down Expand Up @@ -2220,4 +2218,4 @@ END $$;
-- This is important to avoid applying any change more than once.
-- Also update SqaleUtils.CURRENT_SCHEMA_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
call apply_change(24, $$ SELECT 1 $$, true);
call apply_change(25, $$ SELECT 1 $$, true);
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SqaleUtils {
*/
public static final String SCHEMA_AUDIT_CHANGE_NUMBER = "schemaAuditChangeNumber";

public static final int CURRENT_SCHEMA_CHANGE_NUMBER = 24;
public static final int CURRENT_SCHEMA_CHANGE_NUMBER = 25;

public static final int CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER = 8;

Expand Down

0 comments on commit 1cf0a5d

Please sign in to comment.