forked from tech-by-design/polyglot-prime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tech-by-design#179 from alan-francis/main
feat: recent interaction fhir for QE view migration tech-by-design#178
- Loading branch information
Showing
4 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
udi-prime/src/main/postgres/ingestion-center/migrate-interaction-fhir-view.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
|
@@ -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" }) | ||
|