Initial Migration Not Happening With Postgres

default discord avatar
aaronksaunders11 months ago
16

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
    11 months ago

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

  • default discord avatar
    aaronksaunders11 months ago

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

  • discord user avatar
    denolfe
    11 months ago

    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
    aaronksaunders11 months ago

    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:XpAl5NyKiajIersdfqqz@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
    11 months ago

    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
    aaronksaunders11 months ago

    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
    11 months ago

    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
    aaronksaunders11 months ago

    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
    11 months ago

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



    Is this the case?

  • default discord avatar
    aaronksaunders11 months ago

    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
    11 months ago

    Yes, this is the correct flow 👍

  • default discord avatar
    aestrro8 months ago

    I've run into similar problems in CI/CD and after being able to do the

    yarn payload migrate:create initial

    in my local environment - which successfully creates the

    src/migrations

    entry, I then run into the following pesky issue in subsequent calls when CI/CD runs this call

    yarn payload migrate

    :



    ? It looks like you've run Payload in dev mode, meaning you've dynamically pushed changes to your database.


    I have confirmed that CI/CD creates a migration record after

    yarn payload migrate

    with a name of

    dev
  • default discord avatar
    bobdotjs5 months ago

    Did you ever solve this? I'm having the same exact issue



    Might want to edit this to hide your DB info

  • default discord avatar
    aestrro5 months ago

    Indeed, I have. Essentially, I run the following command before I push my code:


        "migrate:create": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload migrate:create",

    If I have any migrations that need to be generated, this does it for me locally, and then I push my code.



    to be clear that is a package.json script invoked via:


     
    yarn migrate:create
  • default discord avatar
    bobdotjs5 months ago

    I appreciate that, but I have my migrations generated and pushed to version control. It's when I try to run the migrations, I get this error:


    > cross-env PAYLOAD_CONFIG_PATH=src/payload/payload.config.ts payload migrate
    [04:38:59] INFO (payload): Starting Payload...
    [04:38:59] INFO (payload): Reading migration files from /opt/render/project/src/src/migrations
    ? It looks like you've run Payload in dev mode, meaning you've dynamically pushed changes to your database.


    I'm deploying on Render with this build command:


    npm --production=false install; npm run build

    and this pre-deploy command


    npm --production=true run payload migrate

    and I have


    NODE_ENV=production

    in my environment variables. So... I'm just a bit miffed



    Ahh nevermind, I think I've realized what happened. The 1st time I built the app, I didn't have my NODE_ENV variable set in render, so it automatically updated the database structure. Then I added it in, and made more changes before generating the 1st migration. So my dev and prod databases are in different places, causing the migrations to fail in production.

    CleanShot_2024-03-21_at_23.35.082x.png
  • default discord avatar
    ixoye365 months ago

    Hi guuys



    Having the exact same issue here (with initial migration)



    Would any of you mind sharing their dockerfile ?



    I fail to understand what commands to run before running the 'payload migrate' command



    Do you need to throw a 'yarn build' in there ?



    Thanks

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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