added updatedAt

This commit is contained in:
stfrigerio 2025-04-06 18:06:55 +02:00
parent 3de4c0be71
commit 466b239153
2 changed files with 25 additions and 24 deletions

View file

@ -43,9 +43,9 @@ export class BooleanSwitchDataService {
const safeHabitIdCol = quoteSqlIdentifier(args.habitIdCol);
const safeValueCol = quoteSqlIdentifier(args.valueCol);
const safeDateCol = quoteSqlIdentifier(args.dateCol);
const updatedAtCol = quoteSqlIdentifier("updatedAt"); //! 🔒 Hardcoded
let sql = "";
let params: (string | number)[] = [];
const now = new Date().toISOString(); //~ Current timestamp for updatedAt
let existingUuid: string | undefined;
@ -65,22 +65,21 @@ export class BooleanSwitchDataService {
}
if (existingUuid) {
sql = `UPDATE ${safeTable} SET ${safeValueCol} = ? WHERE uuid = ?`;
params = [args.newValue, existingUuid];
const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ?, ${updatedAtCol} = ? WHERE uuid = ?`;
const updateParams = [args.newValue, now, existingUuid];
await this.dbService.runQuery(updateSql, updateParams);
} else {
// Standard UPSERT path (no UUID column configured)
sql = `
INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol})
VALUES (?, ?, ?)
const insertSql = `
INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}, ${updatedAtCol})
VALUES (?, ?, ?, ?)
ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE
SET ${safeValueCol} = excluded.${safeValueCol};
SET ${safeValueCol} = excluded.${safeValueCol},
${updatedAtCol} = excluded.${updatedAtCol};
`;
//! Requires UNIQUE constraint on (keyIdCol, dateCol)
params = [args.habitKey, args.date, args.newValue];
const insertParams = [args.habitKey, args.date, args.newValue, now];
await this.dbService.runQuery(insertSql, insertParams);
}
await this.dbService.runQuery(sql, params);
} catch (error) {
console.error(`[BoolDataService] Error in upsertBooleanValue for ${args.habitKey}:`, error);
if (error instanceof Error && error.message.includes("ON CONFLICT clause does not match")) {

View file

@ -47,6 +47,9 @@ export class HabitDataService {
const safeHabitIdCol = quoteSqlIdentifier(args.habitIdCol);
const safeValueCol = quoteSqlIdentifier(args.valueCol);
const safeDateCol = quoteSqlIdentifier(args.dateCol);
const updatedAtCol = quoteSqlIdentifier("updatedAt"); //! 🔒 Hardcoded
const now = new Date().toISOString(); //~ Current timestamp for updatedAt
let existingUuid: string | undefined;
@ -58,29 +61,28 @@ export class HabitDataService {
existingUuid = uuidResult?.[0]?.uuid;
} catch (error) {
if (error instanceof Error && /no such column|does not exist/i.test(error.message) && error.message.includes('uuid')) {
//^ It's expected that 'uuid' might not exist. Log for info and proceed without UUID.
console.log(`[HabitDataService] Info: Optional 'uuid' column not found in table '${args.table}'. Proceeding with standard upsert.`);
existingUuid = undefined; // Ensure it's undefined so the INSERT path is taken
} else {
//^ A different, unexpected error occurred during the UUID fetch, re-throw it.
console.error(`[HabitDataService] Error fetching potential UUID for ${args.habitKey} on ${args.date}:`, error);
throw error; // Re-throw unexpected errors
}
}
if (existingUuid && typeof existingUuid === 'string') {
const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ? WHERE uuid = ?`;
const updateParams = [args.newValue, existingUuid];
const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ?, ${updatedAtCol} = ? WHERE uuid = ?`;
const updateParams = [args.newValue, now, existingUuid];
await this.dbService.runQuery(updateSql, updateParams);
} else {
//^ Use specified columns in INSERT, ON CONFLICT target, and UPDATE SET without uuid
const sql = `
INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}) VALUES (?, ?, ?)
ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE SET ${safeValueCol} = excluded.${safeValueCol};
const insertSql = `
INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}, ${updatedAtCol})
VALUES (?, ?, ?, ?)
ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE SET
${safeValueCol} = excluded.${safeValueCol},
${updatedAtCol} = excluded.${updatedAtCol};
`;
//! Requires UNIQUE constraint on the specified (habitIdCol, dateCol)
const params = [args.habitKey, args.date, args.newValue];
await this.dbService.runQuery(sql, params);
const insertParams = [args.habitKey, args.date, args.newValue, now];
await this.dbService.runQuery(insertSql, insertParams);
}
} catch (error) {