Skip to content

Commit efdee12

Browse files
committed
Move cron migrations
1 parent 9075356 commit efdee12

8 files changed

Lines changed: 30 additions & 27 deletions

File tree

datastore/datastore.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package datastore
22

33
import (
44
"context"
5-
"embed"
65
"errors"
76
"fmt"
87
"os"
@@ -71,14 +70,7 @@ func NewDatastore(minSessionVersion int, isKeyService bool, isTesting bool) (*Da
7170
}
7271
}
7372

74-
var files embed.FS
75-
if isTesting || isKeyService {
76-
files = migrations.MigrationFiles
77-
} else {
78-
files = migrations.MigrationFilesWithExtension
79-
}
80-
81-
iofsDriver, err := iofs.New(files, ".")
73+
iofsDriver, err := iofs.New(migrations.MigrationFiles, ".")
8274
if err != nil {
8375
return nil, fmt.Errorf("failed to load iofs driver for migrations: %w", err)
8476
}

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- POSTGRES_PASSWORD=password
1616
volumes:
1717
- ./misc/create_additional_dbs.sql:/docker-entrypoint-initdb.d/create_additional_dbs.sql
18+
- ./misc/init_pg_cron.sh:/docker-entrypoint-initdb.d/init_pg_cron.sh
1819
ses-local:
1920
build:
2021
context: ./misc/ses-v2-local

migrations/11_cron.down.sql

Lines changed: 0 additions & 5 deletions
This file was deleted.

migrations/11_cron.up.sql

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DO $$
2+
BEGIN
3+
IF EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = 'cron') THEN
4+
PERFORM cron.unschedule('remove-old-verifications');
5+
PERFORM cron.unschedule('remove-old-interim-password-states');
6+
PERFORM cron.unschedule('clean-job-history');
7+
PERFORM cron.unschedule('remove-old-totp-used-codes');
8+
PERFORM cron.unschedule('delete-unverified-accounts');
9+
END IF;
10+
END;
11+
$$;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DO $$
2+
BEGIN
3+
IF EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = 'cron') THEN
4+
PERFORM cron.schedule('remove-old-verifications', '0 0 * * *', $q$DELETE FROM verifications WHERE created_at < CURRENT_TIMESTAMP - interval '24 hours'$q$);
5+
PERFORM cron.schedule('remove-old-interim-password-states', '0 0 * * *', $q$DELETE FROM interim_password_states WHERE created_at < CURRENT_TIMESTAMP - interval '1 hours'$q$);
6+
PERFORM cron.schedule('clean-job-history', '0 0 * * *', $q$DELETE FROM cron.job_run_details WHERE end_time < now() - interval '7 days'$q$);
7+
PERFORM cron.schedule('remove-old-totp-used-codes', '0 0 * * *', $q$DELETE FROM totp_used_codes WHERE created_at < CURRENT_TIMESTAMP - interval '1 hours'$q$);
8+
PERFORM cron.schedule('delete-unverified-accounts', '0,15,30,45 * * * *', $q$DELETE FROM accounts WHERE last_email_verified_at IS NULL AND created_at < CURRENT_TIMESTAMP - interval '30 minutes'$q$);
9+
END IF;
10+
END;
11+
$$;

migrations/embedded.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ package migrations
33
import "embed"
44

55
//go:embed *
6-
var MigrationFilesWithExtension embed.FS
7-
8-
//go:embed 20*
96
var MigrationFiles embed.FS

misc/init_pg_cron.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
set -e
3+
4+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL
5+
CREATE EXTENSION IF NOT EXISTS pg_cron;
6+
EOSQL

0 commit comments

Comments
 (0)