Using Alembic for Single Database with Multiple Schemas

See original GitHub issue

Describe your question I’m trying to use alembic to manage database migrations for a postgres database that contains multiple schemas with each schema being used by a different micro-service.

I realised that I have to use a different table for each micro-service otherwise one micro-service’s migration starts deleting other micro-service tables. So I configured each micro-service alembic_version table to be in it’s own schema to avoid clashes.

The problem that I’m facing is that every migration now recreates all the existing tables of the schema. I tried to debug it and one of the solutions was to set include_schemas=True. But this makes alembic scan all other schemas and try to delete all the tables present in them

I’m not sure if I explained the issue well. I can more details as required

Useful links

Have a nice day!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

10reactions
zzzeekcommented, Jul 12, 2020

OK so you have entirely separate alembic environments each with its own env.py correct?

OK last question do your metadata models have a “schema” in them? that is, they look like:

class MyModel(Base):
   # ...

   __table_args__ = {"schema": "user_schema"}

the include_object we are looking for here looks like this:


def include_object(object, name, type_, reflected, compare_to):
    if type_ == "table" and object.schema != "user_schema":
        return False
   else:
        return True

context.configure(
    # ...
    include_schemas=True,
    include_object = include_object
)

within the autogenerate process (I’m assuming that’s what we’re talking about when you say “recreate the tables for every migration script.”), you have complete control over what objects are considered by using that hook.

1reaction
zzzeekcommented, Jun 18, 2021

Hi @zzzeek I have the same requirement

or, is each schema a copy of one “master” schema, and your application has just one model that is copied to all of them? this would be multitenancy, and youd’ need to alter your env.py to loop through all the schemas individually.

Can you explain to me how can I achieve this. Do you have some snippets on this.?

it’s linked in the comment above, that link is:

https://alembic.sqlalchemy.org/en/latest/cookbook.html#rudimental-schema-level-multi-tenancy-for-postgresql-databases

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alembic support for multiple Postgres schemas - Stack Overflow
My use case is slightly different (multiple distributed builds with a single database containing multiple identical schemas), however I was ...
Read more >
Alembic and postgresql multiple schema question
When I make some changes in my model structures I want to refactor table in all schemas using Alembic, how can I do...
Read more >
Managing multiple databases migrations with Alembic - Medium
So in the meantime, we have to support different DBS in a single project. The easiest way is to use different alembic.ini and...
Read more >
Cookbook — Alembic 1.9.0 documentation
Since all schemas are to be maintained in sync, autogenerate should be run against only one schema, generating new Alembic migration files. Autogenerated ......
Read more >
How to modify the database schema - CodeChecker
Automatic migration script generation (Online) · Step 1: Update the database model · Step 2: Check the alembic.ini configuration settings · Step 3:...
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