ariga / atlas

Manage your database schema as code
https://atlasgo.io
Apache License 2.0
5.64k stars 251 forks source link

cannot diff a schema with a database connection #2431

Open advdv opened 7 months ago

advdv commented 7 months ago

Frustrated by not being able use versioned migrations approach (see #2430 ) I'm trying the declarative approach. I want to take my schema from Ent.

variable "database_url" {
  type = string
  default = "postgres://postgres:postgres@localhost:55435/postgres?sslmode=disable"
}

env "ent" {
  url = var.database_url 
  dev = "docker://postgres/14/dev?search_path=public"
}

schema/user.go

// Package schema defines the Ent schema.
package schema

import (
    "entgo.io/ent"
    "entgo.io/ent/dialect"
    "entgo.io/ent/schema/field"
)

// User holds the schema definition for the User entity.
type User struct {
    ent.Schema
}

// Fields of the User.
func (User) Fields() []ent.Field {
    return []ent.Field{
        field.String("foo"),
        field.String("bar"),
        field.String("id").SchemaType(map[string]string{
            dialect.Postgres: "varchar(21)",
        }),
    }
}

// Edges of the User.
func (User) Edges() []ent.Edge {
    return nil
}

When I run atlas schema apply --env=ent --to=ent://schema it gives me:

Error: cannot diff a schema with a database connection: "" <> "public"

This is a confusing error, and no way to run it in verbose mode or anything. I'm GUESSING that it has to do with "ent" not providing a postgres schema and somehow "public" is not the default. I've tried adding the "--schema" but that doesn't help.

MAYBE it's about the search_path n the dev-url, but removing that gives me: Error: missing schema in --dev-url. See: https://atlasgo.io/url

a8m commented 7 months ago

Thanks for reporting this. We'll give this a look, it might be an issue with the ent/schema loader.

advdv commented 7 months ago

I've solved this for my use case by annotating the User entity with an explicit schema:

func (User) Annotations() []schema.Annotation {
    return []schema.Annotation{
        entsql.Schema("public"),
    }
}

from a usability perspective it would probably still make sense if: