Skip to content

Commit

Permalink
Merge pull request tech-by-design#179 from alan-francis/main
Browse files Browse the repository at this point in the history
feat: recent interaction fhir for QE view migration tech-by-design#178
  • Loading branch information
ratheesh-kr authored Jul 19, 2024
2 parents a2a11e6 + 3c6ba15 commit 9065724
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
Binary file modified hub-prime/lib/techbd-udi-jooq-ingress.auto.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,26 @@ GROUP BY
rc.rejection_count,
wc.warning_count;


/*******************************************************************************************
* Comprehensive view of Latest FHIR Interactions per Tenant IDs*
******************************************************************************************/
DROP VIEW IF EXISTS techbd_udi_ingress.interaction_recent_fhir CASCADE;
CREATE OR REPLACE VIEW techbd_udi_ingress.interaction_recent_fhir
AS WITH ranked_records AS (
SELECT sihr.sat_interaction_http_request_id,
hi.hub_interaction_id AS interaction_id,
sihr.nature ->> 'tenant_id'::text AS tenant_id,
hi.created_at AS interaction_created_at,
row_number() OVER (PARTITION BY (upper(sihr.nature ->> 'tenant_id'::text)) ORDER BY hi.created_at DESC) AS rn
FROM techbd_udi_ingress.hub_interaction hi
JOIN techbd_udi_ingress.sat_interaction_http_request sihr ON hi.hub_interaction_id = sihr.hub_interaction_id
WHERE hi.key = ANY (ARRAY['/Bundle'::text, '/Bundle/$validate'::text])
)
SELECT sat_interaction_http_request_id,
interaction_id,
tenant_id,
interaction_created_at
FROM ranked_records
WHERE rn = 1;

Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env -S deno run --allow-all

/**
* This TypeScript file implements a SQL migration feature for PostgreSQL databases using Deno.
* It provides methods for defining and executing migrations.
*
* @module Information_Schema_Lifecycle_Management_Migration
*/

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 migrate from "../../../../../../../../netspective-labs/sql-aide/pattern/postgres/migrate.ts";

// deconstructed modules provide convenient access to internal imports
const { typical: typ, typical: { SQLa, ws } } = dvp;

type EmitContext = dvp.typical.SQLa.SqlEmitContext;

interface MigrationVersion {
readonly description: string;
readonly dateTime: Date;
}

enum StateStatus {
STATEFUL = "_stateful_",
IDEMPOTENT = "_idempotent_",
}

const prependMigrateSPText = "migrate_";

const ingressSchema = SQLa.sqlSchemaDefn("techbd_udi_ingress", {
isIdempotent: true,
});

export const dvts = dvp.dataVaultTemplateState<EmitContext>({
defaultNS: ingressSchema,
});

// 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 = [
"./001_idempotent_interaction.psql",
] as const;

// Read SQL queries from files
const dependenciesSQL = await readSQLFiles(dependencies);
const testMigrateDependenciesWithPgtap = [
"../../../test/postgres/ingestion-center/suite.pgtap.psql",
] as const;

const ctx = SQLa.typicalSqlEmitContext({
sqlDialect: SQLa.postgreSqlDialect(),
});

const infoSchemaLifecycle = SQLa.sqlSchemaDefn("info_schema_lifecycle", {
isIdempotent: true,
});


export const migrationInput: MigrationVersion = {
description: "interaction-view",
dateTime: new Date(2024, 7, 18, 18, 16),
};
function formatDateToCustomString(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth()).padStart(2, "0"); // Month is zero-based
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");

return `${year}_${month}_${day}_${hours}_${minutes}`;
}

export const migrateVersion = formatDateToCustomString(migrationInput.dateTime);

const migrateSP = pgSQLa.storedProcedure(
prependMigrateSPText + "v" + migrateVersion + StateStatus.IDEMPOTENT +
migrationInput.description,
{},
(name, args, _) =>
pgSQLa.typedPlPgSqlBody(name, args, ctx, {
autoBeginEnd: false,
}),
{
embeddedStsOptions: SQLa.typicalSqlTextSupplierOptions(),
autoBeginEnd: false,
isIdempotent: true,
sqlNS: infoSchemaLifecycle,
headerBodySeparator: "$migrateVersionSP$",
},
)`
BEGIN
${dependenciesSQL}
END
`;

/**
* Generates SQL Data Definition Language (DDL) for the migrations.
*
* @returns {string} The SQL DDL for migrations.
*/

function sqlDDLGenerateMigration() {
return SQLa.SQL<EmitContext>(dvts.ddlOptions)`
${migrateSP}
`;
}

export function generated() {
const ctx = SQLa.typicalSqlEmitContext({
sqlDialect: SQLa.postgreSqlDialect(),
});
const testDependencies: string[] = [];
for (const filePath of testMigrateDependenciesWithPgtap) {
try {
const absolutePath = import.meta.resolve(filePath);
testDependencies.push(absolutePath);
} catch (err) {
console.error(`Error reading filepath ${filePath}:`, err);
throw err;
}
}

// after this execution `ctx` will contain list of all tables which will be
// passed into `dvts.pumlERD` below (ctx should only be used once)
const driverGenerateMigrationSQL = ws.unindentWhitespace(
sqlDDLGenerateMigration().SQL(ctx),
);
return {
driverGenerateMigrationSQL,
pumlERD: dvts.pumlERD(ctx).content,
destroySQL: ws.unindentWhitespace(`
`),
testDependencies,
};
}
4 changes: 2 additions & 2 deletions udi-prime/udictl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "https://deno.land/x/[email protected]/command/mod.ts";
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 ic from "./src/main/postgres/ingestion-center/migrate-basic-infrastructure.ts";
import * as ic from "./src/main/postgres/ingestion-center/migrate-interaction-fhir-view.ts";

const $ = dax.build$({
commandBuilder: new dax.CommandBuilder().noThrow(),
Expand Down Expand Up @@ -291,7 +291,7 @@ const CLI = new Command()
.option("-c, --conn-id <id:string>", "pgpass connection ID to use for psql", { required: true, default: "UDI_PRIME_DESTROYABLE_DEVL" })
.action(async (options) => {
const psqlCreds = pgpassPsqlArgs(options.connId);
console.log((await $.raw`${options.psql} ${psqlCreds} -q -t -A -P border=0 -X -c "CALL info_schema_lifecycle.islm_migrate();"`.captureCombined().lines()).join("\n"));
console.log((await $.raw`${options.psql} ${psqlCreds} -q -t -A -P border=0 -X -c "CALL info_schema_lifecycle.islm_migrate('info_schema_lifecycle',false);"`.captureCombined().lines()).join("\n"));
})
.command("omnibus-fresh", "Freshen the given connection ID")
.option("-c, --conn-id <id:string>", "pgpass connection ID to use for psql", { required: true, default: "UDI_PRIME_DESTROYABLE_DEVL" })
Expand Down

0 comments on commit 9065724

Please sign in to comment.