Skip to content

Commit

Permalink
fix: do not archive posts with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakia committed May 7, 2024
1 parent 2615fc1 commit 5ee263d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions scripts/archive-mysql.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ const archive_posts = async ({ batch_size = 20000 }) => {
const hours = 6 * 7 * 24 // 6 weeks

const stale_timestamps = await db.raw(
`SELECT created_at as timestamp, COUNT(*) as count FROM posts WHERE created_at < UNIX_TIMESTAMP(NOW() - INTERVAL ${hours} HOUR) GROUP BY created_at ORDER BY created_at ASC`
`SELECT created_at as timestamp, COUNT(*) as count FROM posts
WHERE created_at < UNIX_TIMESTAMP(NOW() - INTERVAL ${hours} HOUR)
AND id NOT IN (SELECT post_id FROM post_labels)
GROUP BY created_at ORDER BY created_at ASC`
)

let current_batch_size = 0
Expand All @@ -338,7 +341,14 @@ const archive_posts = async ({ batch_size = 20000 }) => {
.unix(timestamps[0])
.format('YYYY-MM-DD HH:mm:ss')}`
)
const posts = await db('posts').whereIn('created_at', timestamps).select()
const posts = await db('posts')
.whereIn('created_at', timestamps)
.andWhereNotExists(function () {
this.select('*')
.from('post_labels')
.whereRaw('posts.id = post_labels.post_id')
})
.select()

if (!posts.length) {
logger('no posts to archive')
Expand Down

0 comments on commit 5ee263d

Please sign in to comment.