Skip to content

Commit 78aa333

Browse files
authored
chore: integrations table backups (#3589)
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 7f09a25 commit 78aa333

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

backend/src/database/migrations/U1762773409__integrations-backup.sql

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
create table "integrationsBackup" as
2+
select *
3+
from "integrations"
4+
where 1 = 2;
5+
6+
alter table "integrationsBackup"
7+
add column "backupCreatedAt" timestamptz not null default now();
8+
9+
create index if not exists ix_integration_history_integration_id on "integrationsBackup" ("id");
10+
create index if not exists ix_integration_history_history_created_at on "integrationsBackup" ("backupCreatedAt");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import CronTime from 'cron-time-generator'
2+
3+
import { IS_PROD_ENV } from '@crowd/common'
4+
import { WRITE_DB_CONFIG, getDbConnection } from '@crowd/data-access-layer/src/database'
5+
6+
import { IJobDefinition } from '../types'
7+
8+
const job: IJobDefinition = {
9+
name: 'integrations-backup',
10+
cronTime: CronTime.every(1).days(),
11+
timeout: 60 * 60, // 1 hour = 60 * 60 seconds
12+
enabled: async () => IS_PROD_ENV,
13+
process: async (ctx) => {
14+
ctx.log.info('Starting integrations backup job!')
15+
const dbConnection = await getDbConnection(WRITE_DB_CONFIG(), 1, 0)
16+
await dbConnection.query(`insert into "integrationsBackup" select * from "integrations";`)
17+
await dbConnection.query(
18+
`delete from "integrationsBackup" where "backupCreatedAt" < now() - interval '2 months'`,
19+
)
20+
ctx.log.info('Integrations backup job completed!')
21+
},
22+
}
23+
24+
export default job

0 commit comments

Comments
 (0)