From 907648fedc1841751c60566442fa44ae76f28be3 Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Mon, 14 Sep 2026 14:41:03 +0200 Subject: [PATCH] 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. --- .../1757500003_renumber_article_slugs.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pocketbase/pb_migrations/1757500003_renumber_article_slugs.js 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. +})