Alembic "migrations" with existing postgresql database -> Changes only upwards #1425
Replies: 14 comments 11 replies
-
Hi, You can delay your fist |
Beta Was this translation helpful? Give feedback.
-
Hi and thanks for your quick reply. Unfortunately I don't want to delay the first initial version. I want to use it as base version without any commit first the first upgrade. I tried the following:
In step 3 I get an error or information that the database isn't up to date. What did I do wrong? |
Beta Was this translation helpful? Give feedback.
-
Hmm, you wrote "will be empty". But I think it is not. Let me double-check. |
Beta Was this translation helpful? Give feedback.
-
This is my setup: alembic.py
env.py
Execution
The .py file (here: d73198a528f8_initial_setup.py) in the version folder contains the cmplete structure from models.py Is it that what you expected? |
Beta Was this translation helpful? Give feedback.
-
If I then add some new tables or columns to my models.py I cannot create any new revision. If I would delete everything in 'upgrade' and 'downgrade' in my 1st version I get not only the modifications but the complete structure of my inital setup (which will cases errors as the other tables already exists). Hmm, I think I did something wrong. And I have a misunderstanding how to start with an existing database structure (I tries everything I mentioned with an empty DB and that works fine. But not if the database already have existing objects). |
Beta Was this translation helpful? Give feedback.
-
Hi @CaselIT I tried the "stamp" command as well as you suggested.
Problem: The new version not only contains information about table "huhu" but also the information from the stamp revision. Which ends in an error: What did I do wrong? My understanding of stamp would be: stamp the revision "as is" without any further processing. Am I wrong? |
Beta Was this translation helpful? Give feedback.
-
Hi @CaselIT I made a backup of my database and started from skretch. No table in database but models.py available. The second "--autogenerate" with a slightly modified models.py generated a version which contains all tables from the first revision and not only the modifications. It seems that there is a problem with "autogenerate" in combination with (modifed) models.py. |
Beta Was this translation helpful? Give feedback.
-
As you mentioned I modified my env.py in "run_migrations_offline" to:
Unfortunately the behaviour is the same:
In step 4 I've got all the objects from the initial autogenerate + the new huhu table. What did I do in addition: between step 2 and 3 I tried to apply the changes by |
Beta Was this translation helpful? Give feedback.
-
@CaselIT Unfortunately it doesn't work. Let me quickly summaries the setup:
What do I have to do to: Thanks for your patient. |
Beta Was this translation helpful? Give feedback.
-
Since I simply could not imagine that this function would not work, I transferred the data model from the specific (dev) schema to the public schema. And I can confirm that it works as expected. So we don't have a "discussion" here, but a bug. I have to say (for all those who have not yet dealt with databases and PostgreSQL in particular): Not all databases support different schemas. And in production environments they would be used less frequently than in development environments. Therefore, we should state the status here in the discussion as follows:
@CaselIT: Thank you for your support and patient. Should I close this discussion here? |
Beta Was this translation helpful? Give feedback.
-
@zzzeek What would such a context entry for online look like? |
Beta Was this translation helpful? Give feedback.
-
Okay, I found the context. Sorry. I switched from
to
And I can confirm that it is working as well. Just a small change with a hugh impact. Again: Your support is superb! |
Beta Was this translation helpful? Give feedback.
-
@zzzeek Do you think changing the options I mean something the following:
I think this new default would do the correct thing in most cases, meaning that an user has to change the Or does the above sound too complex/magic? |
Beta Was this translation helpful? Give feedback.
-
I realize this is a bit late, but for anyone else facing a similar issue when implementing Alembic with an existing production database that already has initialized tables, here’s a solution that worked for me. Instead of using alembic revision -m "initial script" This will generate a migration file for you. In that file, you can write custom SQL queries to create tables, using conditional logic to ignore them if they already exist. For example: from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '1927398123'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute(
"""
CREATE TABLE IF NOT EXISTS user (
id INT PRIMARY KEY,
name VARCHAR
);
"""
)
``` |
Beta Was this translation helpful? Give feedback.
-
I have an existing and production ready database in my PostgreSQL cluster.
With SQLACodeGen I've created a models.py which represents the structure of my database. And it looks fine to.
Now I want use Alembic as my main database migration (aka modification) framework. How can I achieve this?
I did the following already:
alembic revision --autogenerate -m "inital setup"
I get my first versionBut I don't want to migrate to this "initial setup" version as it is already in my database.
Is there any way to inform Alembic that this "initial setup" version is already there and only new modification to models.py (new tables or new columns) should be taken into account.
Beta Was this translation helpful? Give feedback.
All reactions