Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Adding versions to an existing collection

default discord avatar
rafalkowalewski12 months ago
2 1

Hi,

I've added versions: { drafts: true } to my Articles collection, which already contains some articles.
After doing it:

  • I can see on my UI (Next.js) the existing and new articles
  • I can see in the database (MongoDB) the existing (articles collection) and new (_articles_versions collection) articles
  • I can see on Admin UI only the new articles, but I cannot see the existing ones

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.

Note regarding adding versions to an existing collection
If you already have a collection with documents, and you opt in to draft functionality after you have already created existing documents, all of your old documents will not have a _status field until you resave them. For this reason, if you are adding versions into an existing collection, you might want to write your access control function to allow for users to read both documents where _status is equal to "published" as well as where _status does not exist.

Do you know how to solve it? Shall I do it via migration script or something else?

  • Selected Answer
    discord user avatar
    DanRibbens
    12 months ago

    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.

    9 replies
  • default discord avatar
    rafalkowalewski12 months ago

    thank you very much 💪

  • default discord avatar
    komal122011 months ago

    I do have same issue and i can't able to resolve it can you please guide me in details how can i resolve this ?
    Help will be much appreciated !!

  • default discord avatar
    komal122011 months ago

    @rafalkowalewski can you help me on it?

  • default discord avatar
    rafalkowalewski11 months ago

    @komal1220
    to be honest - we are not on production still, so I just removed all data in db
    but I think it could be fixed just by adding a migration script and resave all of the documents

  • default discord avatar
    komal122011 months ago

    Thanks @rafalkowalewski . Actually I do have two migration scripts and I'm bit confuse which i have to use . if you have any migration scripts can you please share with me if you don't have any problem?
    Thanks!!
    @DanRibbens

  • default discord avatar
    rafalkowalewski11 months ago

    @komal1220
    I haven't created any yet, so I can't help you with that :/

  • default discord avatar
    ThibaultWalterspieler4 months ago

    Hi @komal1220 ! I had the same problem and managed to migrate correctly.

    After generating your migration file, eg:

    pnpm run payload migrate:create ‘add versionning to blog post && xp post’

    You modify the up() script in /migrations/${theNameOfYourMigration}.ts. In my case, after the await payload.db.drizzle.execute(sql...), I added this:

    export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
        await payload.db.drizzle.execute(sql`...);
        
        
        await payload.update({
        collection: "blogPosts",
        where: {},
        data: { _status: "published" },
      });
    }

    Hope this helps !

  • default discord avatar
    dhatGuy2 months ago

    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.

    This doesn't work. The migration runs successfully but the tables/collections are not created. After removing payload.update, the tables/collections are created

  • default discord avatar
    tomekwlod2 months ago

    For me the provided solution worked but only when I put suggested code into a separate migration. Combined migration was timing out so I realised that the update script needs to be run in a separate transaction.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.