diff --git a/pocketbase/pb_migrations/1757500003_renumber_article_slugs.js b/pocketbase/pb_migrations/1757500003_renumber_article_slugs.js new file mode 100644 index 0000000..8e527b9 --- /dev/null +++ b/pocketbase/pb_migrations/1757500003_renumber_article_slugs.js @@ -0,0 +1,35 @@ +/// + +// 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. +})