Postgres data loss in development (SOLVED)

Hello! Really loving the workflow and productivity of RedwoodJS. Thanks to all who develop and maintain! I have a specific problem that happens a little too often and I’ve finally reached my breaking point resulting in a new post here.

With yarn rw dev running, it appears file changes in the /api directory sometimes will basically wipe my database. Tables and columns still exist, but all the rows are gone in every table.

Here is a loom demo: Loom | Free Screen & Video Recording Software | Loom

Aha! So this is related specifically to running postgres on vercel. If you follow the standard setup, you are given env variables like POSTGRES_PRISMA_URL for your database url. I made the mistake of changing the DATABASE_URL reference in schema.prisma to POSTGRES_PRISMA_URL.

The RedwoodJS test helper overrides DATABASE_URL only:

    process.env.DATABASE_URL = process.env.TEST_DATABASE_URL || defaultDb

Source

You can’t edit the auto-generated vercel postgres env vars, so I added DATABASE_URL and DATABASE_URL_NON_POOLING manually, just pointing to the same auto-generated values for POSTGRES_PRISMA_URL and POSTGRES_URL_NON_POOLING, respectively.

The reason this was happening on save was actually from the Jest extension was auto running the test, causing yarn rw prisma db push to get called, which was hitting my dev database because the schema pointed to POSTGRES_PRISMA_URL which the RedwoodJS testing utils obviously don’t override.

I’ll leave this up for searchability in case anyone else stumbles into this. Thanks for the rubber duck!

1 Like