Adding versions to an existing collection #5353
-
|
Hi, I've added
I couldn't find anything on the Internet, only one thing on the drafts documentation, but it doesn't work for me to see the "old" articles on Admin UI.
Do you know how to solve it? Shall I do it via migration script or something else? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
|
The list uses the versions collection, which makes it look like there are no documents even when the database has them. If you make a migration that saves all the documents using |
Beta Was this translation helpful? Give feedback.
-
|
Unfortunately, the approach described by @DanRibbens may not work with the SQLite adapter under certain circumstances. Consider a The generated migration used drizzle's "insert into new table, drop old table and rename" strategy - I assume it did this because adding versions to the collection automatically changed some required fields from non-nullable to nullable in the database schema and this is one of the many operations that SQLite does not support with So then calling update({collection: 'page', where: {}, data: { _status: 'published'})fixed the page entities by populating the versions table, but the content was gone forever. I tried to work around this by loading the content before applying the actual migration. This required modifying the drizzle schema because it already reflects the current configuration, rather than the pre-migration one. There is also a export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
const pages = await withPatchedSchema(payload, ({collections}) => {
const pageConfig = collections.find(c => c.slug === 'page')!;
// Revert to pre-drafts configuration
pageConfig.versions = {drafts: false, maxPerDoc: 10};
pageConfig.flattenedFields = pageConfig.flattenedFields.filter(f => !('name' in f) || f.name !== '_status');
}, () => payload.find({collection: 'page', pagination: false}));
// Rest of migration
await db.run(sql`CREATE TABLE...`);
// Populate versions
for (const page of pages.docs) {
await payload.update({collection: 'page', data: {...page, _status: 'published'}, where: {}});
}
await payload.db.drizzle.update(payload.db.tables._page_v).set({latest: 1});
}// Helper to make temporary changes to the Payload collection config.
import { drizzle } from 'drizzle-orm/libsql';
import { Payload, SanitizedConfig } from 'payload';
import { produce, WritableDraft } from 'immer';
const withPatchedSchema = async <T>(payload: Payload, patch: (config: WritableDraft<SanitizedConfig>) => void, callback: () => Promise<T>) => {
const prevConfig = payload.config;
payload.config = produce(payload.config, patch);
await payload.db.init!();
payload.db.drizzle = drizzle(payload.db.client as never, {schema: {
...payload.db.tables,
...payload.db.relations,
}});
const result = await callback();
payload.config = prevConfig;
await payload.db.init!();
payload.db.drizzle = drizzle(payload.db.client as never, {schema: {
...payload.db.tables,
...payload.db.relations,
}});
return result;
}This was definitely a major PITA and my key takeaway is to plan ahead if you think your collection is going to need versions :) |
Beta Was this translation helpful? Give feedback.
The list uses the versions collection, which makes it look like there are no documents even when the database has them. If you make a migration that saves all the documents using
payload.update({ _status: 'published', where: {})it should resave all your docs to have a version history.