diff options
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r-- | server/helpers/database-utils.ts | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index 2b916efc2..f9cb33aca 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | import * as retry from 'async/retry' | 1 | import * as retry from 'async/retry' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Bluebird from 'bluebird' |
3 | import { QueryTypes, Transaction } from 'sequelize' | ||
3 | import { Model } from 'sequelize-typescript' | 4 | import { Model } from 'sequelize-typescript' |
5 | import { sequelizeTypescript } from '@server/initializers/database' | ||
4 | import { logger } from './logger' | 6 | import { logger } from './logger' |
5 | import { Transaction } from 'sequelize' | ||
6 | 7 | ||
7 | function retryTransactionWrapper <T, A, B, C, D> ( | 8 | function retryTransactionWrapper <T, A, B, C, D> ( |
8 | functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>, | 9 | functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>, |
@@ -96,6 +97,18 @@ function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): | |||
96 | .map(f => f.destroy({ transaction: t })) | 97 | .map(f => f.destroy({ transaction: t })) |
97 | } | 98 | } |
98 | 99 | ||
100 | // Sequelize always skip the update if we only update updatedAt field | ||
101 | function setAsUpdated (table: string, id: number, transaction?: Transaction) { | ||
102 | return sequelizeTypescript.query( | ||
103 | `UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`, | ||
104 | { | ||
105 | replacements: { table, id, updatedAt: new Date() }, | ||
106 | type: QueryTypes.UPDATE, | ||
107 | transaction | ||
108 | } | ||
109 | ) | ||
110 | } | ||
111 | |||
99 | // --------------------------------------------------------------------------- | 112 | // --------------------------------------------------------------------------- |
100 | 113 | ||
101 | export { | 114 | export { |
@@ -104,5 +117,6 @@ export { | |||
104 | transactionRetryer, | 117 | transactionRetryer, |
105 | updateInstanceWithAnother, | 118 | updateInstanceWithAnother, |
106 | afterCommitIfTransaction, | 119 | afterCommitIfTransaction, |
107 | deleteNonExistingModels | 120 | deleteNonExistingModels, |
121 | setAsUpdated | ||
108 | } | 122 | } |