I see in the documentation that alembic is intended to be easy to integrate with SQLAlchemy's `metadata.create_all(engine)`
but I'm confused about how this actually works out. The docs include this snippet:
# inside of a "create the database" script, first create
# tables:
my_metadata.create_all(engine)
# then, load the Alembic configuration and generate the
# version table, "stamping" it with the most recent rev:
from alembic.config import Config
from alembic import command
alembic_cfg = Config("/path/to/yourapp/alembic.ini")
command.stamp(alembic_cfg, "head")It's quite appealing to use alembic only for changes that `create_all` can't handle (like adding columns to an existing table). I'm confused about how this actually works out, though.
Suppose in environment A, I have an existing table, T1, and would like to add a new column C1 to it. First, I add the Column definition to my Model, and then I create a version file (maybe with `--autogenerate`), with an upgrade method that adds the column.
In environment B, suppose I didn't have T1 yet at all, so I can run the script snippet, and it will create T1 for me, complete with C1 since that's now in the Model, and I'm up to date and stamp myself that way. Perfect.
But environment C starts off just like environment A did, with T1 missing C1. Now I run the same script snippet, and `create_all` does nothing because `create_all` doesn't add new columns to existing tables. Environment C still gets stamped as being up to date, even though it is still missing C1. In this case, it seems I should have run `alembic upgrade head` instead of doing `create_all`.
What would have happened in environment B if I did `alembic upgrade head` instead of `create_all`? It would have failed, right, because there's no way to add C1 to the not-yet-existing T1.
Is there one procedure that works for both B and C? I'm tempted to write something that first runs `create_all`, then asks autogenerate's `compare_metadata` whether there are still differences, and only if there are runs `alembic upgrade head`, but I'm not sure that procedure covers all bases either.
Am I missing something here?