Skip to content

Commit

Permalink
Merge pull request tech-by-design#241 from alan-francis/main
Browse files Browse the repository at this point in the history
feat: update dv tables & stored functions with dv tables tech-by-design#108
  • Loading branch information
ratheesh-kr authored Aug 1, 2024
2 parents 853599c + 7ed8e58 commit 827633f
Show file tree
Hide file tree
Showing 9 changed files with 580 additions and 242 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,7 @@ export function generated() {
return {
driverGenerateMigrationSQL,
pumlERD: dvts.pumlERD(ctx).content,
destroySQL: ws.unindentWhitespace(`
DROP SCHEMA IF EXISTS public CASCADE;
DROP SCHEMA IF EXISTS ${ingressSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${assuranceSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${diagnosticsSchema.sqlNamespace} cascade;
DROP PROCEDURE IF EXISTS "${migrateSP.sqlNS?.sqlNamespace}"."${migrateSP.routineName}" CASCADE;
`),
destroySQL: ws.unindentWhitespace(``),
testDependencies,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,203 +50,36 @@ export const {
serial,
date,
jsonText,
createdAt
createdAt,
} = dvts.domains;

/*
* The following function, techbd_udi_ingress.hub_fhir_bundle_upserted, is designed to insert a record into the hub_fhir_bundle table
*/
const fnHubFhirBundleUpsertBuilder = pgSQLa
.storedRoutineBuilder(
"hub_fhir_bundle_upserted",
{
fhir_bundle_key: text(),
created_by: textNullable(),
provenance: textNullable(),
created_at: createdAt(),
upsert_behavior: boolean().default(true),
},
);
export const fnHubFhirBundleUpsert = pgSQLa.storedFunction(
fnHubFhirBundleUpsertBuilder.routineName,
fnHubFhirBundleUpsertBuilder.argsDefn,
"TEXT",
(name, args) => pgSQLa.typedPlPgSqlBody(name, args, ctx),
{
embeddedStsOptions: tmpl.typicalSqlTextSupplierOptions(),
autoBeginEnd: false,
isIdempotent: true,
sqlNS: ingressSchema,
headerBodySeparator: "$fnHubFhirBundleUpsert$",
},
)`
declare
hub_request_http_client_row techbd_udi_ingress.hub_interaction%ROWTYPE;
hub_exception_row techbd_udi_ingress.hub_exception;
err_returned_sqlstate text; -- Variable to store SQLSTATE of the error
err_message_text text; -- Variable to store the message text of the error
err_pg_exception_detail text; -- Variable to store the detail of the error
err_pg_exception_hint text; -- Variable to store the hint of the error
err_pg_exception_context text; -- Variable to store the context of the error
v_error_msg TEXT;
v_sqlstate TEXT;
v_pg_detail TEXT;
v_pg_hint TEXT;
v_pg_context TEXT;
v_created_at TIMESTAMPTZ := COALESCE(created_at, CURRENT_TIMESTAMP);
v_created_by TEXT := COALESCE(created_by, current_user);
v_provenance TEXT := COALESCE(provenance, 'unknown');
v_exception_id TEXT;
v_hub_fhir_bundle_id TEXT;
BEGIN
BEGIN
v_hub_fhir_bundle_id := gen_random_uuid()::text;
INSERT INTO techbd_udi_ingress.hub_fhir_bundle (hub_fhir_bundle_id, key, created_at, created_by, provenance)
VALUES (v_hub_fhir_bundle_id, fhir_bundle_key, v_created_at, v_created_by, v_provenance)
;
EXCEPTION
WHEN unique_violation THEN
IF NOT hub_upsert_behavior THEN
-- Capture exception details
GET STACKED DIAGNOSTICS
v_error_msg = MESSAGE_TEXT,
v_sqlstate = RETURNED_SQLSTATE,
v_pg_detail = PG_EXCEPTION_DETAIL,
v_pg_hint = PG_EXCEPTION_HINT,
v_pg_context = PG_EXCEPTION_CONTEXT;
-- Call register_issue to log the exception and get the exception ID
PERFORM techbd_udi_ingress.register_issue( NULL, fhir_bundle_key, v_error_msg, v_sqlstate, v_pg_detail, v_pg_hint, v_pg_context, v_created_by, v_provenance);
END IF;
v_hub_fhir_bundle_id := NULL;
END;
RETURN v_hub_fhir_bundle_id;
EXCEPTION
WHEN OTHERS THEN
-- Capture exception details
GET STACKED DIAGNOSTICS
v_error_msg = MESSAGE_TEXT,
v_sqlstate = RETURNED_SQLSTATE,
v_pg_detail = PG_EXCEPTION_DETAIL,
v_pg_hint = PG_EXCEPTION_HINT,
v_pg_context = PG_EXCEPTION_CONTEXT;
-- Log the exception, reusing the previous exception ID if it exists
PERFORM techbd_udi_ingress.register_issue(
COALESCE(v_exception_id, NULL),
fhir_bundle_key,
v_error_msg,
v_sqlstate,
v_pg_detail,
v_pg_hint,
v_pg_context,
v_created_by,
v_provenance);
RETURN NULL;
END;
`;
/*
* This function attempts to insert a new row into the techbd_udi_ingress.hub_uniform_resource table with the given parameters
*/
const fnHubUniformResourceUpsertBuilder = pgSQLa
.storedRoutineBuilder(
"hub_uniform_resource_upserted",
{
uniform_resource_key: text(),
created_by: textNullable(),
provenance: textNullable(),
created_at: createdAt(),
upsert_behavior: boolean().default(true),
},
);
export const fnHubUniformResourceUpsert = pgSQLa.storedFunction(
fnHubUniformResourceUpsertBuilder.routineName,
fnHubUniformResourceUpsertBuilder.argsDefn,
"TEXT",
(name, args) => pgSQLa.typedPlPgSqlBody(name, args, ctx),
{
embeddedStsOptions: tmpl.typicalSqlTextSupplierOptions(),
autoBeginEnd: false,
isIdempotent: true,
sqlNS: ingressSchema,
headerBodySeparator: "$fnHubUniformResourceUpsert$",
},
)`
declare
hub_request_http_client_row techbd_udi_ingress.hub_interaction%ROWTYPE;
hub_exception_row techbd_udi_ingress.hub_exception;
err_returned_sqlstate text; -- Variable to store SQLSTATE of the error
err_message_text text; -- Variable to store the message text of the error
err_pg_exception_detail text; -- Variable to store the detail of the error
err_pg_exception_hint text; -- Variable to store the hint of the error
err_pg_exception_context text; -- Variable to store the context of the error
v_error_msg TEXT;
v_sqlstate TEXT;
v_pg_detail TEXT;
v_pg_hint TEXT;
v_pg_context TEXT;
v_created_at TIMESTAMPTZ := COALESCE(created_at, CURRENT_TIMESTAMP);
v_created_by TEXT := COALESCE(created_by, current_user);
v_provenance TEXT := COALESCE(provenance, 'unknown');
v_exception_id TEXT;
v_hub_uniform_resource_id TEXT;
BEGIN
BEGIN
v_hub_uniform_resource_id := gen_random_uuid()::text;
INSERT INTO techbd_udi_ingress.hub_uniform_resource (hub_uniform_resource_id, key, created_at, created_by, provenance)
VALUES (v_hub_uniform_resource_id, uniform_resource_key, v_created_at, v_created_by, v_provenance)
;
EXCEPTION
WHEN unique_violation THEN
IF NOT hub_upsert_behavior THEN
-- Capture exception details
GET STACKED DIAGNOSTICS
v_error_msg = MESSAGE_TEXT,
v_sqlstate = RETURNED_SQLSTATE,
v_pg_detail = PG_EXCEPTION_DETAIL,
v_pg_hint = PG_EXCEPTION_HINT,
v_pg_context = PG_EXCEPTION_CONTEXT;
-- Call register_issue to log the exception and get the exception ID
PERFORM techbd_udi_ingress.register_issue( NULL, interaction_key, v_error_msg, v_sqlstate, v_pg_detail, v_pg_hint, v_pg_context, v_created_by, v_provenance);
END IF;
v_hub_uniform_resource_id := NULL;
END;
RETURN v_hub_uniform_resource_id;
EXCEPTION
WHEN OTHERS THEN
-- Capture exception details
GET STACKED DIAGNOSTICS
v_error_msg = MESSAGE_TEXT,
v_sqlstate = RETURNED_SQLSTATE,
v_pg_detail = PG_EXCEPTION_DETAIL,
v_pg_hint = PG_EXCEPTION_HINT,
v_pg_context = PG_EXCEPTION_CONTEXT;
-- Log the exception, reusing the previous exception ID if it exists
PERFORM techbd_udi_ingress.register_issue(
COALESCE(v_exception_id, NULL),
uniform_resource_key,
v_error_msg,
v_sqlstate,
v_pg_detail,
v_pg_hint,
v_pg_context,
v_created_by,
v_provenance);
RETURN NULL;
END;
`;

// Function to read SQL from a list of .psql files
async function readSQLFiles(filePaths: readonly string[]): Promise<string[]> {
const sqlContents = [];
for (const filePath of filePaths) {
try {
const absolutePath = new URL(filePath, import.meta.url).pathname;
const data = await Deno.readTextFile(absolutePath);
sqlContents.push(data);
} catch (err) {
console.error(`Error reading file ${filePath}:`, err);
throw err;
}
}
return sqlContents;
}

// List of dependencies and test dependencies
const dependencies = [
"../005_idempotent_stored_routines.psql",
] as const;




// Read SQL queries from files
const dependenciesSQL = await readSQLFiles(dependencies);
const testMigrateDependenciesWithPgtap = [
"../../../../test/postgres/ingestion-center/004-idempotent-migrate-unit-test.psql",
"../../../../test/postgres/ingestion-center/suite.pgtap.psql",
Expand Down Expand Up @@ -291,11 +124,8 @@ const migrateSP = pgSQLa.storedProcedure(
)`
BEGIN
${dependenciesSQL}
${fnHubFhirBundleUpsert}
${fnHubUniformResourceUpsert}
Expand Down Expand Up @@ -341,14 +171,7 @@ export function generated() {
return {
driverGenerateMigrationSQL,
pumlERD: dvts.pumlERD(ctx).content,
destroySQL: ws.unindentWhitespace(`
DROP SCHEMA IF EXISTS public CASCADE;
DROP SCHEMA IF EXISTS ${ingressSchema.sqlNamespace} cascade;
DROP PROCEDURE IF EXISTS "${migrateSP.sqlNS?.sqlNamespace}"."${migrateSP.routineName}" CASCADE;
`),
destroySQL: ws.unindentWhitespace(``),
testDependencies,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,7 @@ export function generated() {
return {
driverGenerateMigrationSQL,
pumlERD: dvts.pumlERD(ctx).content,
destroySQL: ws.unindentWhitespace(`
DROP SCHEMA IF EXISTS public CASCADE;
DROP SCHEMA IF EXISTS ${ingressSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${assuranceSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${diagnosticsSchema.sqlNamespace} cascade;
DROP PROCEDURE IF EXISTS "${migrateSP.sqlNS?.sqlNamespace}"."${migrateSP.routineName}" CASCADE;
`),
destroySQL: ws.unindentWhitespace(``),
testDependencies,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,7 @@ export function generated() {
return {
driverGenerateMigrationSQL,
pumlERD: dvts.pumlERD(ctx).content,
destroySQL: ws.unindentWhitespace(`
DROP SCHEMA IF EXISTS public CASCADE;
DROP SCHEMA IF EXISTS ${ingressSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${assuranceSchema.sqlNamespace} cascade;
DROP SCHEMA IF EXISTS ${diagnosticsSchema.sqlNamespace} cascade;
DROP PROCEDURE IF EXISTS "${migrateSP.sqlNS?.sqlNamespace}"."${migrateSP.routineName}" CASCADE;
`),
destroySQL: ws.unindentWhitespace(``),
testDependencies,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import * as dvp from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.13.34/pattern/data-vault/mod.ts";
import { pgSQLa } from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.13.34/pattern/pgdcp/deps.ts";
import * as ddlTable from "./ddl-table.ts";
import * as ddlTable from "./models-dv.ts";
// import * as migrate from "../../../../../../../../netspective-labs/sql-aide/pattern/postgres/migrate.ts";

// deconstructed modules provide convenient access to internal imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ic1 from "./migrate-basic-infrastructure.ts";
import * as ic2 from "./migrate-interaction-fhir-view.ts";
import * as ic3 from "./migrate-diagnostics-fhir-view.ts";
import * as ic4 from "./migrate-content-fhir-view.ts";
import * as ic5 from "./migrate-ddl-table.ts";
import * as ic5 from "./migrate-models-dv.ts";
import * as ic6 from "./migrate-ddl-stored-routine-interaction.ts";

// Create an array containing the modules
Expand Down
2 changes: 1 addition & 1 deletion udi-prime/udictl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import * as dax from "https://deno.land/x/[email protected]/mod.ts";
import * as pgpass from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.13.27/lib/postgres/pgpass/pgpass-parse.ts";
import * as migrateIc from "./src/main/postgres/ingestion-center/migrations/migrations.ts";
import * as ddlTable from "./src/main/postgres/ingestion-center/migrations/ddl-table.ts";
import * as ddlTable from "./src/main/postgres/ingestion-center/migrations/models-dv.ts";

const $ = dax.build$({
commandBuilder: new dax.CommandBuilder().noThrow(),
Expand Down

0 comments on commit 827633f

Please sign in to comment.