Skip to content

Commit

Permalink
fix sql server pool issues (#3059)
Browse files Browse the repository at this point in the history
* move start transaction in the if statement for saveMetadataToSqlServer

* remove random transaction from conditons api sql server

* refactor service for condition to simplify logic

* [pre-commit.ci] auto fixes from pre-commit hooks

* update jsdocs

* Update sqlserver healthcheck since previously it wasn't working

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
BobanL and pre-commit-ci[bot] authored Dec 27, 2024
1 parent a9d82ef commit 9112d57
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 260 deletions.
3 changes: 1 addition & 2 deletions containers/ecr-viewer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ services:
test:
[
"CMD-SHELL",
"/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Password1! -Q 'IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'ECR_DATA') SELECT 1 ELSE SELECT 0' -C | grep -q 1",
'opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Password1! -Q "SELECT * FROM sys.tables where name=''ECR_DATA''" -C | grep -q "(0 rows affected)" && exit 1 || exit 0',
]
interval: 20s
timeout: 10s
retries: 5
profiles:
- sqlserver
Expand Down
35 changes: 24 additions & 11 deletions containers/ecr-viewer/src/app/api/conditions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@ import { get_conditions_postgres, get_conditions_sqlserver } from "./service";
* @returns A promise resolving to a NextResponse object.
*/
export async function GET() {
const metadataSaveLocation = process.env.METADATA_DATABASE_TYPE;
const metadataLocation = process.env.METADATA_DATABASE_TYPE;

switch (metadataSaveLocation) {
case "postgres":
return await get_conditions_postgres();
case "sqlserver":
return await get_conditions_sqlserver();
default:
return NextResponse.json(
{ message: "Invalid metadata save location: " + metadataSaveLocation },
{ status: 500 },
);
try {
switch (metadataLocation) {
case "postgres":
return NextResponse.json(await get_conditions_postgres(), {
status: 200,
});
case "sqlserver":
return NextResponse.json(await get_conditions_sqlserver(), {
status: 200,
});
default:
return NextResponse.json(
{
message: "Invalid metadata location.",
},
{ status: 500 },
);
}
} catch (e) {
return NextResponse.json(
{ message: "Failed to get conditions." },
{ status: 500 },
);
}
}
56 changes: 18 additions & 38 deletions containers/ecr-viewer/src/app/api/conditions/service.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,45 @@
import { NextResponse } from "next/server";
import sql from "mssql";
import { get_pool } from "../services/sqlserver_db";
import { getDB } from "../services/postgres_db";

/**
* Retrieves all unique conditions from the ecr_rr_conditions table in the PostgreSQL database.
* @returns A promise resolving to a NextResponse object.
* @throws An error if the connection to the PostgreSQL database fails.
* @returns Array of conditions
* @throws An error if it fails to fetch data
*/
export const get_conditions_postgres = async () => {
export const get_conditions_postgres = async (): Promise<string[]> => {
const { database, pgPromise } = getDB();
const { ParameterizedQuery: PQ } = pgPromise;

try {
const response = await database.tx(async (t) => {
const getConditions = new PQ({
text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions ORDER BY "condition"',
});

const conditions = await t.any(getConditions);

return NextResponse.json(
conditions.map((c: any) => c.condition),
{ status: 200 },
);
const getConditions = new PQ({
text: 'SELECT DISTINCT "condition" FROM ecr_rr_conditions ORDER BY "condition"',
});
const conditions = await database.any<{ condition: string }>(getConditions);

return response;
return conditions.map((c) => c.condition);
} catch (error: any) {
console.error("Error fetching data:", error);
return NextResponse.json({ message: error.message }, { status: 500 });
console.error("Error fetching data: ", error);
throw Error("Error fetching data");
}
};

/**
* Retrieves all unique conditions from the ecr_rr_conditions table in the SQL Server database.
* @returns A promise resolving to a NextResponse object.
* @throws An error if the connection to the SQL Server database fails.
* @returns Array of conditions
* @throws An error if it fails to fetch data
*/
export const get_conditions_sqlserver = async () => {
try {
let pool = await get_pool();
if (!pool) {
return NextResponse.json(
{ message: "Failed to connect to SQL Server." },
{ status: 500 },
);
throw Error("Failed to connnect to pool");
}

const transaction = new sql.Transaction(pool);
await transaction.begin();
const request = new sql.Request(transaction);

const result = await request.query(
"SELECT DISTINCT erc.[condition] FROM ecr_rr_conditions erc ORDER BY erc.[condition]",
);
const conditions: string[] = result.recordset.map((row) => row.condition);

return NextResponse.json(conditions, { status: 200 });
const result = await pool.request().query<{
condition: string;
}>("SELECT DISTINCT condition FROM ecr_rr_conditions ORDER BY condition");
return result.recordset.map((row) => row.condition);
} catch (error: any) {
console.error("Error fetching data:", error);
return NextResponse.json({ message: error.message }, { status: 500 });
console.error("Error fetching data: ", error);
throw Error("Error fetching data");
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ export const saveMetadataToSqlServer = async (
return { message: "Failed to connect to SQL Server.", status: 500 };
}

const transaction = new sql.Transaction(pool);
await transaction.begin();

if (process.env.METADATA_DATABASE_SCHEMA == "extended") {
const transaction = new sql.Transaction(pool);
await transaction.begin();
try {
const ecrDataInsertRequest = new sql.Request(transaction);
await ecrDataInsertRequest
Expand Down
206 changes: 0 additions & 206 deletions containers/ecr-viewer/src/app/api/tests/conditions.service.test.ts

This file was deleted.

Loading

0 comments on commit 9112d57

Please sign in to comment.