Skip to content

Commit

Permalink
feat: backfill licensed users
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Nov 19, 2024
1 parent f7e0f46 commit 4236862
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions src/migrations/20241119105837-licensed-users-backfill.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports.up = (db, cb) => {
db.runSql(`
WITH user_events AS (
WITH user_events AS (
SELECT
DISTINCT CASE WHEN type = 'user-deleted' THEN pre_data ->> 'email' ELSE data ->> 'email' END AS email,
type,
Expand All @@ -9,66 +9,66 @@ exports.up = (db, cb) => {
events
WHERE
type IN ('user-created', 'user-deleted')
),
dates AS (
WITH RECURSIVE generated_dates AS (
SELECT
MIN(event_date):: timestamp AS date
FROM
user_events
UNION ALL
SELECT
date + INTERVAL '1 day'
FROM
generated_dates
WHERE
date + INTERVAL '1 day' <= CURRENT_DATE
)
),
dates AS (
WITH RECURSIVE generated_dates AS (
SELECT
date :: date
MIN(event_date):: timestamp AS date
FROM
generated_dates
),
active_emails AS (
SELECT
d.date,
ue.email,
MAX(
CASE WHEN ue.type = 'user-created' THEN ue.event_date ELSE NULL END
) AS created_date,
MAX(
CASE WHEN ue.type = 'user-deleted' THEN ue.event_date ELSE NULL END
) AS deleted_date
FROM
dates d
LEFT JOIN user_events ue ON ue.event_date <= d.date
GROUP BY
d.date,
ue.email
),
result AS (
user_events
UNION ALL
SELECT
d.date,
COUNT(DISTINCT ae.email) AS active_emails_count
date + INTERVAL '1 day'
FROM
dates d
LEFT JOIN active_emails ae ON d.date = ae.date
generated_dates
WHERE
(
ae.deleted_date IS NULL
OR ae.deleted_date >= ae.date - INTERVAL '30 days'
OR ae.deleted_date < ae.created_date
)
GROUP BY
d.date
ORDER BY
d.date
) INSERT INTO licensed_users (date, count)
SELECT
date,
active_emails_count
FROM
result ON CONFLICT (date) DO NOTHING;
date + INTERVAL '1 day' <= CURRENT_DATE
)
SELECT
date :: date
FROM
generated_dates
),
active_emails AS (
SELECT
d.date,
ue.email,
MAX(
CASE WHEN ue.type = 'user-created' THEN ue.event_date ELSE NULL END
) AS created_date,
MAX(
CASE WHEN ue.type = 'user-deleted' THEN ue.event_date ELSE NULL END
) AS deleted_date
FROM
dates d
LEFT JOIN user_events ue ON ue.event_date <= d.date
GROUP BY
d.date,
ue.email
),
result AS (
SELECT
d.date,
COUNT(DISTINCT ae.email) AS active_emails_count
FROM
dates d
LEFT JOIN active_emails ae ON d.date = ae.date
WHERE
(
ae.deleted_date IS NULL
OR ae.deleted_date >= ae.date - INTERVAL '30 days'
OR ae.deleted_date < ae.created_date
)
GROUP BY
d.date
ORDER BY
d.date
) INSERT INTO licensed_users (date, count)
SELECT
date,
active_emails_count
FROM
result ON CONFLICT (date) DO NOTHING;
`, cb);

};
Expand Down

0 comments on commit 4236862

Please sign in to comment.