Renumber article slugs to sequential article-N
One-time PocketBase migration that replaces every article's slug with article-1, article-2, ... ordered by publishedAt (oldest first), requested explicitly by the site owner in place of the existing descriptive slugs.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
// One-time cleanup: replace every article's slug with a sequential
|
||||
// "article-N", ordered by publishedAt (oldest first; drafts, which have no
|
||||
// publishedAt, sort after published articles by creation order). Requested
|
||||
// explicitly by the site owner, aware this drops the existing descriptive
|
||||
// slugs and breaks any already-indexed/shared URLs using them.
|
||||
//
|
||||
// Two passes with a temporary unique slug in between: assigning final slugs
|
||||
// in a single pass can collide with another record's current slug that
|
||||
// hasn't been renamed yet (slug has a unique index).
|
||||
migrate((app) => {
|
||||
const articles = app.findAllRecords('articles')
|
||||
|
||||
articles.sort((a, b) => {
|
||||
const publishedA = a.getString('publishedAt')
|
||||
const publishedB = b.getString('publishedAt')
|
||||
if (publishedA && publishedB) return publishedA < publishedB ? -1 : publishedA > publishedB ? 1 : 0
|
||||
if (publishedA) return -1
|
||||
if (publishedB) return 1
|
||||
return a.getString('created') < b.getString('created') ? -1 : 1
|
||||
})
|
||||
|
||||
articles.forEach((record) => {
|
||||
record.set('slug', `migrating-${record.id}`)
|
||||
app.save(record)
|
||||
})
|
||||
|
||||
articles.forEach((record, index) => {
|
||||
record.set('slug', `article-${index + 1}`)
|
||||
app.save(record)
|
||||
})
|
||||
}, (_app) => {
|
||||
// Not reversible: the original slugs aren't recorded anywhere.
|
||||
})
|
||||
Reference in New Issue
Block a user