Skip to content

Commit

Permalink
Database: Let db set the changed_at column
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 12, 2024
1 parent e511890 commit b0c890b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions library/Notifications/Common/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,36 @@ private static function getConnection(): Connection
$select->where([$baseTableAlias . '.' . $condition => 'n']);
});

$db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) {
$db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) use ($adapter) {
$tableName = $insert->getInto();

if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) {
if ($adapter instanceof Pgsql) {
$changedAt = new Expression('EXTRACT(EPOCH FROM NOW()) * 1000');
} else {
$changedAt = new Expression('FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)');
}

$columns = array_combine($insert->getColumns(), $insert->getValues());
$columns['changed_at'] = time() * 1000;
$columns['changed_at'] = $changedAt;

$insert->values($columns);
}
});

$db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_UPDATE, function (Update $update) {
$db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_UPDATE, function (Update $update) use ($adapter) {
$table = $update->getTable();
$tableName = reset($table);

if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) {
if ($adapter instanceof Pgsql) {
$changedAt = new Expression('EXTRACT(EPOCH FROM NOW()) * 1000');
} else {
$changedAt = new Expression('FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)');
}

$columns = $update->getSet();
$columns['changed_at'] = time() * 1000;
$columns['changed_at'] = $changedAt;

$update->set($columns);
}
Expand Down

0 comments on commit b0c890b

Please sign in to comment.