question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-apply Migration problems

See original GitHub issue

I am facing two issues both related to applying migrations to the database. I have initialized Hasura using docker to a pre-existing project and Postgres database. I used the Hasura CLI to initialize the migrations and metadata files within a sub-directory of my project. Here are the two problems I am facing:

(1) Auto-applying migration scripts is not working

  • I am following this guide: https://hasura.io/docs/1.0/graphql/core/migrations/advanced/auto-apply-migrations.html#auto-apply-migrations to set up auto-applying upon starting the Docker container.
  • I have followed the steps in the ‘Applying migrations’ section where I have tried both setting the migration and metadata env variables as well as trying -v.
  • I am also using the hasura/graphql-engine:v1.3.2.cli-migrations-v2 image.
  • I try setting the directory for the env variable in the script to something like this: -e HASURA_GRAPHQL_MIGRATIONS_DIR=/Users/<name>/Desktop/<app>/db/hasura-migrations/migrations.
  • I even tried putting the bash script next to the migrations folder and just using ./migrations as the path.
  • In both cases, I get the following error in my Docker log: {"kind":"migrations-apply", "info":"directory /Users/<name>/Desktop/<app>/db/hasura-migrations/migrations does not exist, skipping migrations"}}
  • I have repeatedly verified that the directory is accurate, but I continue to get the same error saying the directory does not exist, and subsequently skips the migration apply. I am also unsure if this is a Docker or Hasura related error I am making, so I apologize if this error is not caused by Hasura.

(2) hasura migrate status is misleading

  • I have been testing the migration scripts to see if they work by regressing my database such that some metadata inconsistency exists when I run hasura console.
  • The console correctly gives the inconsistency error, but when I go to verify this using hasura migrate status, the DATABASE STATUS column reads PRESENT for all migration files which should be in theory not present, since they are not in the database.
  • As a result of this, running hasura apply simply returns nothing to apply.
  • I have actually tried manually running all of the up.sql migration files against the database and everything works, hasura metadata apply then works, and everything is all good, it’s just that it won’t work automatically through the cli.

Overall, I am facing such challenges in automating the migration/metadata application process and would appreciate any help. Apologies once more if this is not the right place to post these, I had tried reaching out to a representative over the Hasura website, but have gotten little to no response.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
RobSchilderrcommented, Oct 16, 2021

It’s all good, I managed to figure out the problem (one of the paths was was missing). Thank you very much for your help I have managed to get it working! I am closing this issue.

Hey man, do you remember what path was missing? I am having the same issue now, it is saying 'INFO nothing to apply on database default ’ whereas the metadata does work.

2reactions
GavinRay97commented, Oct 2, 2020

I try setting the directory for the env variable in the script to something like this: -e HASURA_GRAPHQL_MIGRATIONS_DIR=/Users/<name>/Desktop/<app>/db/hasura-migrations/migrations

So, the way that Docker containers work is that they are self-contained. The filesystem on a Docker image is not connected to your host filesystem (unless you use a bind-mount).

Inside of your Docker image, /Users/<name>/Desktop/<app>/db/hasura-migrations/migrations does not exist, unless in your Dockerfile you have something like this:

RUN mkdir -p /Users/<name>/Desktop/<app>/db/hasura-migrations/migrations

So what you need to do is put the metadata and migrations folder into the built container image. You can do this with:

FROM hasura/graphql-engine:v1.3.2.cli-migrations-v2
COPY ./metadata /hasura-metadata
COPY ./migrations /hasura-migrations

Assuming that this Dockerfile exists in the same directory as your metadata and migrations folder.

The other thing you can do in local development if you don’t want to write a custom Dockerfile, is to mount your migrations and metadata images from your host into the container directories:

version: "3.8"

services:
  postgres:
    image: postgres:12
    restart: always
    ports:
      - 5432:5432
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword

  graphql-engine:
    image: hasura/graphql-engine:v1.3.1.cli-migrations-v2
    ports:
      - 8080:8080
    depends_on:
      - "postgres"
    restart: always
    volumes:
      - ./migrations:/hasura-migrations
      - ./metadata:/hasura-metadata
    environment:
      # your environment variables here

volumes:
  db_data:

The reason why this works is because of this:

    volumes:
      - ./migrations:/hasura-migrations
      - ./metadata:/hasura-metadata

You are mounting the local directory ./migrations into the container directory /hasura-migrations.

Hope that answers your questions and clears things up 👍

(2) hasura migrate status is misleading

When you run a migration against a Hasura instance, it makes an entry in the DB it’s connected to that the migration has been successfully applied on it.

hasura migrate status essentially just checks the list of migrations against what the database has had run on it before.

How are you “regressing the database” to break this? Are you deleting tables without tracking the deletions with migrations?

If you don’t track all changes ever made, Hasura’s migrations and metadata management become useless because there’s a “break” in the history. You need to manually fix it if this happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EntityFramework Core automatic migrations - Stack Overflow
When the model changes a lot and and multiple people work on the same project, migrations leads more problems than solutions. Moreover, having...
Read more >
Automated Migration in Entity Framework 6
Learn how to implement automated migration in Entity Framework 6.x using enable-migrations command.
Read more >
Migrations & Metadata Setup | Hasura GraphQL Docs
The following command will squash all migration files from the given migration to the latest migration into a single migration file. hasura migrate...
Read more >
Recovery components | Microsoft Learn
Auto-Apply folders · Capturing Windows desktop applications using Windows User State Migration Tool (USMT)'s ScanState tool · Restoring settings ...
Read more >
6.0 Migration Guide - JBoss.org
This guide discusses migration from Hibernate ORM version 6.0. ... Allows to "auto apply" a UserCollectionType whenever Hibernate encounters ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found