Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Postgres schema support in schema migrationsΒ #1564

@naormatania

Description

@naormatania
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary πŸ’‘

I am using ent in my project with postgres database.
The tables are created in different postgres schemas (using https://www.postgresql.org/docs/9.1/ddl-schemas.html) which allows me to give different microservices access to only certain tables while still having the ability to access the entire db together if needed (in contrast to different database per microservice).

I am using the feature flag schema config (https://entgo.io/docs/feature-flags#schema-config) which lets me define in which schema each table resides. However, the migration code that creates the tables ignores the schema config (which means it creates all tables in the default schema). I'll explain the issue with an example.
I define client with the following code:

client, err := ent.Open(dialect, conn, ent.AlternateSchema(ent.SchemaConfig{
    User: "userschema",
}))

client.Schema.Create(ctx) currently runs: CREATE TABLE IF NOT EXISTS "users" (...)
What I would want it to run is: CREATE TABLE IF NOT EXISTS "userschema"."users" (...)

Right now as a workaround I use rewriter driver as defined in /entc/integration/multischema/multischema_test.go which basically replaces table names in queries.
Example from code:

	rp := strings.NewReplacer("`groups`", "`db1`.`groups`", "`pets`", "`db1`.`pets`", "`users`", "`db2`.`users`", "`group_users`", "`db2`.`group_users`")

However, it requires me to maintain another list of schemas and it's also seems like an error prone approach.

Does adding schemas to ent migration sounds like a good idea ?
I would love to help with contributing code if you'd like. However, I wasn't sure what is the best way to support it.
I thought it might be easier to support it if we could add Schema field to entsql.Annotation but there might be another way to support it in the current approach.
Also worth noting that schemas exist in postgres but not in other databases which complicates what is the best way to support it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions