Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
JaiPannu-IITI committed Feb 26, 2025
1 parent b08ad21 commit 13d618f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
16 changes: 11 additions & 5 deletions scripts/dbManagement/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ export async function askUserToContinue(question: string): Promise<boolean> {
export async function formatDatabase(): Promise<boolean> {
const adminEmail = envConfig.API_ADMINISTRATOR_USER_EMAIL_ADDRESS;

Check warning on line 69 in scripts/dbManagement/helpers.ts

View check run for this annotation

Codecov / codecov/patch

scripts/dbManagement/helpers.ts#L69

Added line #L69 was not covered by tests

type TableRow = { tablename: string };
if (!adminEmail) {
throw new Error(
"Missing adminEmail environment variable. Aborting to prevent accidental deletion of all users.",
);
}

Check warning on line 75 in scripts/dbManagement/helpers.ts

View check run for this annotation

Codecov / codecov/patch

scripts/dbManagement/helpers.ts#L71-L75

Added lines #L71 - L75 were not covered by tests

type TableRow = { tablename: string };
const USERS_TABLE = "users";

Check warning on line 78 in scripts/dbManagement/helpers.ts

View check run for this annotation

Codecov / codecov/patch

scripts/dbManagement/helpers.ts#L78

Added line #L78 was not covered by tests
try {
await db.transaction(async (tx) => {
const tables: TableRow[] = await tx.execute(sql`
Expand All @@ -78,7 +84,7 @@ export async function formatDatabase(): Promise<boolean> {
`);
const tableNames = tables
.map((row) => row.tablename)
.filter((name) => name !== "users");
.filter((name) => name !== USERS_TABLE);

Check warning on line 87 in scripts/dbManagement/helpers.ts

View check run for this annotation

Codecov / codecov/patch

scripts/dbManagement/helpers.ts#L84-L87

Added lines #L84 - L87 were not covered by tests

if (tableNames.length > 0) {
await tx.execute(sql`
Expand All @@ -91,9 +97,9 @@ export async function formatDatabase(): Promise<boolean> {
}

await tx.execute(sql`
DELETE FROM "users"
WHERE "email_address" != ${adminEmail};
`);
DELETE FROM ${sql.identifier(USERS_TABLE)}
WHERE email != ${adminEmail};
`);

Check warning on line 102 in scripts/dbManagement/helpers.ts

View check run for this annotation

Codecov / codecov/patch

scripts/dbManagement/helpers.ts#L99-L102

Added lines #L99 - L102 were not covered by tests
});

return true;
Expand Down
3 changes: 1 addition & 2 deletions scripts/dbManagement/resetData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ export async function main(): Promise<void> {
try {
await formatDatabase();
console.log("\n\x1b[32mSuccess:\x1b[0m Database formatted successfully");
console.log("\x1b[32mSuccess:\x1b[0m Administrator access preserved\n");
console.log("\x1b[32mSuccess:\x1b[0m Administrator preserved\n");
} catch (error: unknown) {
console.error(
"\n\x1b[31mError: Database formatting failed\n\x1b[0m",
error,
);
console.error("\n\x1b[33mRolled back to previous state\x1b[0m");
console.error("\n\x1b[33mPreserving administrator access\x1b[0m");
}
try {
await emptyMinioBucket();
Expand Down
5 changes: 1 addition & 4 deletions test/scripts/dbManagement/addSampleData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ suite("addSampleData main function tests", () => {
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining("Database connected successfully"),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining("Administrator setup complete"),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining("Sample Data added to the database"),
);
Expand All @@ -57,7 +54,7 @@ suite("addSampleData main function tests", () => {
});

test("should throw an error when insertCollections fails", async () => {
// Arrange: pingDB and ensureAdministratorExists succeed, but insertCollections fails.
// Arrange: pingDB succeed, but insertCollections fails.
vi.spyOn(helpers, "pingDB").mockResolvedValue(true);
const errorMsg = "insert error";
vi.spyOn(helpers, "insertCollections").mockRejectedValue(
Expand Down
14 changes: 1 addition & 13 deletions test/scripts/dbManagement/resetDB.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite("resetData main function tests", () => {
);
});

test("should log errors for failing formatDatabase, emptyMinioBucket, and ensureAdministratorExists, but not throw", async () => {
test("should log errors for failing formatDatabase, emptyMinioBucket but not throw", async () => {
// Arrange: simulate user confirming and pingDB succeeding.
vi.spyOn(helpers, "askUserToContinue").mockResolvedValue(true);
vi.spyOn(helpers, "pingDB").mockResolvedValue(true);
Expand Down Expand Up @@ -65,22 +65,10 @@ suite("resetData main function tests", () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Rolled back to previous state"),
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Preserving administrator access"),
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Error: Bucket formatting failed"),
expect.any(Error),
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Error: Administrator creation failed"),
expect.any(Error),
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(
"Administrator access may be lost, try reformatting DB to restore access",
),
);
});

test("should log success messages when all operations succeed", async () => {
Expand Down

0 comments on commit 13d618f

Please sign in to comment.