No playground link as im not sure how to write it because this involves atlas for this issue, but I will include all the code I can.

We use a combination of atlas and gorm. I have not upgraded or touched atlas. Five days ago, I upgraded gorm.

our atlas hcl, which has not changed in forever, looks like:

data "external_schema" "gorm" {
  program = [
    "go",
    "run",
    "-mod=mod",
    "./db/tools/atlas_gorm_loader.go",
  ]
}

env "gorm" {
  src = data.external_schema.gorm.url
  dev = "postgresql://root@localhost:26257/MY_DB_NAME?search_path=public&sslmode=disable"
  migration {
    dir = "file://db/migrations?format=goose"
  }
  format {
    migrate {
      diff = "{{ sql . \"  \" }}"
    }
  }
}

our atlas go loader, which has not changed in forever, looks like this, where v1 is our gorm models.

// Atlas gorm loader
package main

import (
    "fmt"
    "io"
    "os"

    _ "ariga.io/atlas-go-sdk/recordriver"
    "ariga.io/atlas-provider-gorm/gormschema"

    v1 "github.com/XXXXXXX/auth_api/pkg/api/v1"
)

// Define the models to generate migrations for.
var models = []any{
    &v1.Entitlement{},
    &v1.Account{},
    &v1.User{},
}

func main() {
    stmts, err := gormschema.New("postgres").Load(models...)
    if err != nil {
        fmt.Fprintf(os.Stderr, "failed to load gorm schema: %v\n", err)
        os.Exit(1)
    }

    _, err = io.WriteString(os.Stdout, stmts)
    if err != nil {
        fmt.Fprintf(os.Stderr, "failed to write gorm schema: %v\n", err)
        os.Exit(1)
    }
}

when we make a change to our gorm models, we:

  1. bring up a clean postgres in docker
  2. create our dev database MY_DB_NAME, and leave it blank - atlas takes care of the rest
  3. run:
atlas migrate diff "description of changes" --env gorm

to generate a new date_desc.sql migration file.

what atlas does is 1. populates the dev_db (localhost) with the latest gorm models 2. compares that to the migrations dir, and generates a new migrations file if the state of dev (the latest gorm models) doesnt match the state of the migrations dir. Im not exactly sure how this is done as this is inside atlas

~~

For two days I thought atlas broke; i thought my binary of atlas had updated, or the atlas-gorm package had updated, because the error was when running atlas. However I finally narrowed it down to an upgrade from 1.30.0 to 1.30.1 in my go.mod of gorm.

The error is:

error: schema.sql:1: executing statement: ALTER TABLE "entitlements" ADD "created_at" timestamptz NOT NULL;: pq: relation "entitlements" does not exist

however, changing nothing else but downgrading to 1.30.1 -> 1.30.0 fixes the issue.

From dumping the stmts - gormschema.New("postgres").Load(models...) is what broke; however gormschema has not been updated. The only thing that was updated was gorm. Which leads me to believe that something has changed on the gorm models. Im only guessing, but by looking at 1.30.1, maybe it was this PR? https://github.com/go-gorm/gorm/pull/7530/files or this one https://github.com/go-gorm/gorm/pull/7499/files?

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking