Skip to content

Commit

Permalink
Implement wrapper function for babelfish inconsistency checks (#2465) (
Browse files Browse the repository at this point in the history
…#2553)

This commit implements a wrapper function for all the checks to detect
inconsistency between babelfish catalogs and pg catalogs. As of now, we
are only checking the metadata consistency between these catalogs.

Task: BABEL-4139

Signed-off-by: Roshan Kanwar <[email protected]>
  • Loading branch information
roshan0708 authored May 6, 2024
1 parent 288cf89 commit 688142e
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .github/composite-actions/check-babelfish-inconsistency/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: 'Check Babelfish metadata inconsistency'
description: Check for Babelfish metadata inconsistency before Major/Minor Version Upgrade
runs:
using: 'composite'
steps:
- name: Check Babelfish metadata inconsistency
id: check-babelfish-inconsistency
run: |
output=$(sqlcmd -S localhost -U jdbc_user -P 12345678 -Q "SELECT sys.check_for_inconsistent_metadata() GO" | tail -n +2)
echo "check_result=$output" >> "$GITHUB_OUTPUT"
shell: bash
10 changes: 9 additions & 1 deletion .github/workflows/major-version-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,17 @@ jobs:
sudo echo "host all all $ipaddress/32 trust" >> pg_hba.conf
shell: bash

- name: Check Babelfish metadata inconsistency before Major Version Upgrade
id: check-babelfish-inconsistency
if: always() && steps.setup-new-datadir.outcome == 'success'
uses: ./.github/composite-actions/check-babelfish-inconsistency

- name: Run pg_upgrade
id: run-pg_upgrade
if: always() && steps.setup-new-datadir.outcome == 'success'
if: |
always() && steps.setup-new-datadir.outcome == 'success'
&& steps.check-babelfish-inconsistency.outcome == 'success'
&& steps.check-babelfish-inconsistency.outputs.check_result == 0
uses: ./.github/composite-actions/run-pg-upgrade

- name: Run JDBC Tests
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/singledb-version-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ jobs:
install_dir: ${{ env.OLD_INSTALL_DIR }}
migration_mode: 'single-db'

- name: Check Babelfish metadata inconsistency before Major Version Upgrade
id: check-babelfish-inconsistency
if: always() && steps.setup-base-version.outcome == 'success'
uses: ./.github/composite-actions/check-babelfish-inconsistency

- name: Upgrade to latest version and run verify-cleanup tests
id: upgrade-and-verify
if: always() && steps.setup-base-version.outcome == 'success'
if: |
always() && steps.setup-base-version.outcome == 'success'
&& steps.check-babelfish-inconsistency.outcome == 'success'
&& steps.check-babelfish-inconsistency.outputs.check_result == 0
uses: ./.github/composite-actions/major-version-upgrade-util
with:
engine_branch: latest
Expand Down
20 changes: 20 additions & 0 deletions contrib/babelfishpg_tsql/sql/ownership.sql
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,26 @@ RETURNS table (
detail jsonb
) AS 'babelfishpg_tsql', 'babelfish_inconsistent_metadata' LANGUAGE C STABLE;

CREATE OR REPLACE FUNCTION sys.check_for_inconsistent_metadata()
RETURNS BOOLEAN AS $$
DECLARE
has_inconsistent_metadata BOOLEAN;
num_rows INT;
BEGIN
has_inconsistent_metadata := FALSE;

-- Count the number of inconsistent metadata rows from Babelfish catalogs
SELECT COUNT(*) INTO num_rows
FROM sys.babelfish_inconsistent_metadata();

has_inconsistent_metadata := num_rows > 0;

-- Additional checks can be added here to update has_inconsistent_metadata accordingly

RETURN has_inconsistent_metadata;
END;
$$
LANGUAGE plpgsql STABLE;

CREATE OR REPLACE FUNCTION sys.role_id(role_name SYS.SYSNAME)
RETURNS INT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@ CREATE OR REPLACE FUNCTION sys.sp_tables_internal(
$$
LANGUAGE plpgsql STABLE;

CREATE OR REPLACE FUNCTION sys.check_for_inconsistent_metadata()
RETURNS BOOLEAN AS $$
DECLARE
has_inconsistent_metadata BOOLEAN;
num_rows INT;
BEGIN
has_inconsistent_metadata := FALSE;

-- Count the number of inconsistent metadata rows from Babelfish catalogs
SELECT COUNT(*) INTO num_rows
FROM sys.babelfish_inconsistent_metadata();

has_inconsistent_metadata := num_rows > 0;

-- Additional checks can be added here to update has_inconsistent_metadata accordingly

RETURN has_inconsistent_metadata;
END;
$$
LANGUAGE plpgsql STABLE;

-- Drops the temporary procedure used by the upgrade script.
-- Please have this be one of the last statements executed in this upgrade script.
DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP FUNCTION IF EXISTS check_for_inconsistent_metadata_vu_prepare_func
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE FUNCTION check_for_inconsistent_metadata_vu_prepare_func()
RETURNS BOOLEAN
AS
BEGIN
RETURN (SELECT sys.check_for_inconsistent_metadata())
END
GO
50 changes: 50 additions & 0 deletions test/JDBC/expected/check_for_inconsistent_metadata-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- psql





-- Updating one entry from pg_proc pg catalog so that metadata inconsistency fails during check against babelfish_function_ext babelfish catalog
UPDATE pg_catalog.pg_proc SET proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong' WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func';
SELECT proname, prosrc FROM pg_catalog.pg_proc WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong';
SELECT nspname, funcname FROM sys.babelfish_function_ext WHERE funcname = 'check_for_inconsistent_metadata_vu_prepare_func';
-- should return true because of inconsistency
SELECT sys.check_for_inconsistent_metadata();
-- should return the inconsistent row data
SELECT sys.babelfish_inconsistent_metadata();
UPDATE pg_catalog.pg_proc SET proname = 'check_for_inconsistent_metadata_vu_prepare_func' WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong';
GO
~~ROW COUNT: 1~~

~~START~~
name#!#text
check_for_inconsistent_metadata_vu_prepare_func_wrong#!#BEGIN<newline>RETURN (SELECT sys.check_for_inconsistent_metadata())<newline>END
~~END~~

~~START~~
name#!#name
master_dbo#!#check_for_inconsistent_metadata_vu_prepare_func
~~END~~

~~START~~
bool
t
~~END~~

~~START~~
record
(name,pg_catalog,proname,"{""Rule"": ""<funcname> in babelfish_function_ext must also exist in pg_proc""}")
~~END~~

~~ROW COUNT: 1~~


-- tsql
-- since data is consistent now, this should return 0
SELECT check_for_inconsistent_metadata_vu_prepare_func()
GO
~~START~~
bit
0
~~END~~

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP FUNCTION IF EXISTS check_for_inconsistent_metadata_vu_prepare_func
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE FUNCTION check_for_inconsistent_metadata_vu_prepare_func()
RETURNS BOOLEAN
AS
BEGIN
RETURN (SELECT sys.check_for_inconsistent_metadata())
END
GO
21 changes: 21 additions & 0 deletions test/JDBC/input/check_for_inconsistent_metadata-vu-verify.mix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Updating one entry from pg_proc pg catalog so that metadata inconsistency fails during check against babelfish_function_ext babelfish catalog
-- psql
UPDATE pg_catalog.pg_proc SET proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong' WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func';

SELECT proname, prosrc FROM pg_catalog.pg_proc WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong';

SELECT nspname, funcname FROM sys.babelfish_function_ext WHERE funcname = 'check_for_inconsistent_metadata_vu_prepare_func';

-- should return true because of inconsistency
SELECT sys.check_for_inconsistent_metadata();

-- should return the inconsistent row data
SELECT sys.babelfish_inconsistent_metadata();

UPDATE pg_catalog.pg_proc SET proname = 'check_for_inconsistent_metadata_vu_prepare_func' WHERE proname = 'check_for_inconsistent_metadata_vu_prepare_func_wrong';
GO

-- tsql
-- since data is consistent now, this should return 0
SELECT check_for_inconsistent_metadata_vu_prepare_func()
GO
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BABEL-3613

babelfish_cast_floor
check_for_inconsistent_metadata
babel_try_parse
TestBigInt
TestBinary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Function sys.char_to_fixeddecimal(sys."varchar")
Function sys.char_to_fixeddecimal(sys.bpchar)
Function sys.char_to_fixeddecimal(text)
Function sys.charindex(text,text,integer)
Function sys.check_for_inconsistent_metadata()
Function sys.checksum(text[])
Function sys.collationproperty(text,text)
Function sys.columnproperty(oid,name,text)
Expand Down

0 comments on commit 688142e

Please sign in to comment.