Initial Migration Not Happening With Postgres

default discord avatar
aaronksaunders
last month
11

it seems that the initial migration is not happening when you launch with

yarn run serve

... if I launch with

yarn run dev

then the database is properly setup.



I am trying to create a template for Railway and in my testing I have found that the migration is not happening until I run dev.



Do I need to migrate for production manually? -

https://payloadcms.com/docs/database/postgres#migration-workflows

yarn run payload migrate:create
yarn run payload migrate 
  • discord user avatar
    denolfe
    Payload Team
    last month

    Yes, you need to explicitly call migrate for production. The automatic pushing of the scheme only happens during development.

  • default discord avatar
    aaronksaunders
    last month

    so when trying to deploy with docker, we need to add the migration script to the docker file

  • discord user avatar
    denolfe
    Payload Team
    last month

    Yes, you'll want your migrations directory included. By default, it should go to src/migrations, which will be compiled to dist/migrations



    I have not gone through this entire flow including docker, though. Let me know if you run into any issues.

  • default discord avatar
    aaronksaunders
    last month

    I am not a docker expert so I am now just trying to add a start script to run the migrations



    the have added the create and migrate script to package .json serve script so that it runs before launching the server but I am getting errors , Wondering if I should be running is somewhere else?


    yarn run v1.22.19
    $ cross-env ; payload migrate:create; payload migrate; PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js
    [01:49:50] INFO (payload): Starting Payload...
    /home/node/app/node_modules/payload/dist/bin/migrate.js:89
    throw new Error(`Error creating migration: ${err.message}`);
    ^
    Error: Error creating migration: ENOENT: no such file or directory, mkdir '/home/node/app/src/migrations'
    at migrate (/home/node/app/node_modules/payload/dist/bin/migrate.js:89:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    Node.js v18.8.0
    [01:49:54] INFO (payload): Starting Payload...
    [01:49:54] INFO (payload): No migrations to run.
    [01:49:54] INFO (payload): Done.
    Database  postgresql://postgres:XpAl5NyKiajIdNZ5eyyz@containers-us-west-173.railway.app:7214/railway
    (node:28) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    [01:49:58] INFO (payload): Starting Payload...
    [01:49:58] INFO (payload): Payload Admin URL: /admin
  • discord user avatar
    denolfe
    Payload Team
    last month

    I'm a bit confused, it looks like you're trying to create a migration on startup?



    The flow should be:


    - Perform all development changes to your config


    - When ready, run

    yarn payload migrate:create initial

    - This will create a new file in

    src/migrations

    by default


    - In production, before starting your application, you should run

    yarn payload migrate

    to run any migration that has not run.

  • default discord avatar
    aaronksaunders
    last month

    I can do this manually and it works fine, I am trying to integrate it in to CI/CD system based off of a build from a Docker file; so it needs to be integrated into the build process. Trying to determine the rignt place for the script to run and make sure the environment is set properly with proper env variables is what has been tricky for me

  • discord user avatar
    denolfe
    Payload Team
    last month

    How you approach this is going to depend on your environment. Initializing your app with a script that calls migrate before serve is an option here.

  • default discord avatar
    aaronksaunders
    last month

    I am going to wait until there is more documentation around the migration scripts... right now I am getting prompted in the middle of the CI script which is a non-starter and needs a resolution. There needs to be a way to force a default response and I cannot find it in the documentation


    ? No schema changes detected. Would you like to create a blank migration file? › (y/N)

    thanks for your help - I am going to have to find another solution for automating the build

  • discord user avatar
    denolfe
    Payload Team
    last month

    It looks like you're trying to run migrate:create instead of migrate.



    Is this the case?

  • default discord avatar
    aaronksaunders
    last month

    I resolved the issue by removing migrate:create from the automated build process. I am assuming that the migration files should be created and committed to the repo. the workflow requires the user to run migrate:create so all the CI script needs to do is run the migrate script

  • discord user avatar
    denolfe
    Payload Team
    last month

    Yes, this is the correct flow 👍

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.