-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
097 Feb 20: Add demo flag in table for mock data #824
base: master
Are you sure you want to change the base?
097 Feb 20: Add demo flag in table for mock data #824
Conversation
WalkthroughThe pull request introduces a new boolean column Changes
Sequence Diagram(s)sequenceDiagram
participant D as autoDriver.driver.ts
participant U as autoDriver.util.ts
D->>D: deleteMockData()
loop For each table in demo deletion list
D->>U: deleteDEMOData(tableName)
alt tableName is "vendors"
U->>U: Retrieve vendor IDs where is_demo = true
U->>U: Delete matching entries from vendors_projects
end
U->>U: Delete entries where is_demo is true in table
end
D->>D: Complete deletion process
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🔭 Outside diff range comments (2)
Servers/driver/autoDriver.driver.ts (1)
41-345
: 🛠️ Refactor suggestionUse boolean values for
is_demo
flag.The insert queries use string '1' for the
is_demo
flag instead of proper boolean values. This could cause type mismatches with the database schema.Replace all occurrences of
'1'
withtrue
in thegenerateValuesString
functions. Example:- '1' + trueSQL_Commands.sql (1)
1-208
: 🛠️ Refactor suggestionEnhance database schema for demo data.
Consider the following improvements:
- Add default value for
is_demo
column- Add index for performance optimization
- Include migration strategy for existing data
Apply these changes to the schema:
ALTER TABLE roles ADD COLUMN is_demo BOOLEAN DEFAULT false; +CREATE INDEX idx_roles_is_demo ON roles(is_demo); ALTER TABLE users ADD COLUMN is_demo BOOLEAN DEFAULT false; +CREATE INDEX idx_users_is_demo ON users(is_demo); -- Repeat for other tablesAlso, consider adding a migration script to handle existing data:
-- Set is_demo = false for existing records UPDATE roles SET is_demo = false WHERE is_demo IS NULL; UPDATE users SET is_demo = false WHERE is_demo IS NULL; -- Repeat for other tables
🧹 Nitpick comments (1)
Servers/mocks/users.data.ts (1)
27-59
: Remove commented-out code.Instead of keeping commented-out mock user entries, they should be removed entirely. If needed, these entries can be retrieved from version control history.
Apply this diff to clean up the code:
- // { - // id: 3, - // name: "Cathy", - // surname: "Brown", - // email: "[email protected]", - // password_hash: - // "$2b$10$7qXYDROKyGgH3lpWL0dJR.dRN1T0AvG.J7EmZHF9iVqptQWAWHq.a", - // role: role3, // Editor - // created_at: new Date("2024-03-10"), - // last_login: new Date("2024-09-28"), - // }, - // { - // id: 4, - // name: "David", - // surname: "Wilson", - // email: "[email protected]", - // password_hash: - // "$2b$10$7yo46.rtqbrn4fxxLENu5eqfvfi8pYXm00sceVM3Ash7PlZuGONOa", - // role: role4, // Auditor - // created_at: new Date("2024-04-05"), - // last_login: new Date("2024-09-30"), - // }, - // { - // id: 5, - // name: "Eva", - // surname: "Clark", - // email: "[email protected]", - // password_hash: - // "$2b$10$3T5VhzKdBpUPXqHuiKZXJusPdR7k.SmwRbl3fopf4XEJhMfgyYSOO", - // role: role2, // Reviewer - // created_at: new Date("2024-05-20"), - // last_login: new Date("2024-09-18"), - // },
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
SQL_Commands.sql
(10 hunks)Servers/driver/autoDriver.driver.ts
(12 hunks)Servers/mocks/users.data.ts
(1 hunks)Servers/utils/autoDriver.util.ts
(1 hunks)
export async function deleteDEMOData(tablename: string) { | ||
if (tablename === "vendors") { | ||
const result = await pool.query(`SELECT id FROM vendors WHERE is_demo;`) | ||
await Promise.all(result.rows.map(async r => { | ||
await pool.query(`DELETE FROM vendors_projects WHERE vendor_id = ${r.id}`) | ||
})) | ||
} | ||
await pool.query(`DELETE FROM ${tablename} WHERE is_demo;`) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix SQL injection vulnerability and add error handling.
The function has several issues that need to be addressed:
- SQL injection vulnerability in the vendor ID interpolation
- Missing error handling for database operations
- No transaction management for cascading deletes
Apply this diff to fix the issues:
export async function deleteDEMOData(tablename: string) {
+ const client = await pool.connect();
+ try {
+ await client.query('BEGIN');
if (tablename === "vendors") {
- const result = await pool.query(`SELECT id FROM vendors WHERE is_demo;`)
+ const result = await client.query(`SELECT id FROM vendors WHERE is_demo;`);
await Promise.all(result.rows.map(async r => {
- await pool.query(`DELETE FROM vendors_projects WHERE vendor_id = ${r.id}`)
+ await client.query(`DELETE FROM vendors_projects WHERE vendor_id = $1`, [r.id]);
}))
}
- await pool.query(`DELETE FROM ${tablename} WHERE is_demo;`)
+ await client.query(`DELETE FROM $1 WHERE is_demo;`, [tablename]);
+ await client.query('COMMIT');
+ } catch (error) {
+ await client.query('ROLLBACK');
+ throw new Error(`Failed to delete demo data from ${tablename}: ${error.message}`);
+ } finally {
+ client.release();
+ }
}
Committable suggestion skipped: line range outside the PR's diff.
export async function deleteMockData() { | ||
const projects = await getDEMOProjects() | ||
for (let project of projects) { | ||
await deleteProjectByIdQuery(project.id) | ||
}; | ||
await deleteExistingData("vendors", "vendor_name"); | ||
await deleteExistingData("users", "name"); | ||
// await deleteExistingData("roles", "name"); | ||
// const projects = await getDEMOProjects() | ||
// for (let project of projects) { | ||
// await deleteProjectByIdQuery(project.id) | ||
// }; | ||
// await deleteExistingData("vendors", "vendor_name"); | ||
// await deleteExistingData("users", "name"); | ||
// // await deleteExistingData("roles", "name"); | ||
|
||
for (let table of ["questions", "subtopics", "topics", "projectscopes", "projectrisks", "vendorrisks", | ||
"subcontrols", "controls", "controlcategories", "assessments", "vendors", "projects", "users"]) { | ||
await deleteDEMOData(table) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve demo data deletion logic.
The function has several issues:
- Commented code should be removed
- Table names are hardcoded and could be out of sync with schema
- No error handling for failed deletions
Apply this diff to improve the code:
export async function deleteMockData() {
- // const projects = await getDEMOProjects()
- // for (let project of projects) {
- // await deleteProjectByIdQuery(project.id)
- // };
- // await deleteExistingData("vendors", "vendor_name");
- // await deleteExistingData("users", "name");
- // // await deleteExistingData("roles", "name");
-
- for (let table of ["questions", "subtopics", "topics", "projectscopes", "projectrisks", "vendorrisks",
- "subcontrols", "controls", "controlcategories", "assessments", "vendors", "projects", "users"]) {
- await deleteDEMOData(table)
+ const tables = [
+ "questions", "subtopics", "topics", "projectscopes",
+ "projectrisks", "vendorrisks", "subcontrols", "controls",
+ "controlcategories", "assessments", "vendors", "projects", "users"
+ ];
+
+ for (const table of tables) {
+ try {
+ await deleteDEMOData(table);
+ } catch (error) {
+ console.error(`Failed to delete demo data from ${table}:`, error);
+ throw error;
+ }
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export async function deleteMockData() { | |
const projects = await getDEMOProjects() | |
for (let project of projects) { | |
await deleteProjectByIdQuery(project.id) | |
}; | |
await deleteExistingData("vendors", "vendor_name"); | |
await deleteExistingData("users", "name"); | |
// await deleteExistingData("roles", "name"); | |
// const projects = await getDEMOProjects() | |
// for (let project of projects) { | |
// await deleteProjectByIdQuery(project.id) | |
// }; | |
// await deleteExistingData("vendors", "vendor_name"); | |
// await deleteExistingData("users", "name"); | |
// // await deleteExistingData("roles", "name"); | |
for (let table of ["questions", "subtopics", "topics", "projectscopes", "projectrisks", "vendorrisks", | |
"subcontrols", "controls", "controlcategories", "assessments", "vendors", "projects", "users"]) { | |
await deleteDEMOData(table) | |
} | |
} | |
export async function deleteMockData() { | |
const tables = [ | |
"questions", "subtopics", "topics", "projectscopes", | |
"projectrisks", "vendorrisks", "subcontrols", "controls", | |
"controlcategories", "assessments", "vendors", "projects", "users" | |
]; | |
for (const table of tables) { | |
try { | |
await deleteDEMOData(table); | |
} catch (error) { | |
console.error(`Failed to delete demo data from ${table}:`, error); | |
throw error; | |
} | |
} | |
} |
@HarshP4585 |
Describe your changes
is_demo
flag in tables for mock dataIssue number
Please ensure all items are checked off before requesting a review:
Summary by CodeRabbit
New Features
Refactor
Chore
These changes improve data consistency and simplify the management of demo versus production records.