Custom data

To preempt the need to write an explicit test for every new migration, there is a mechanism built into the config to automatically insert data into the database before or upon reaching a given revision.

To that end:

def alembic_config():
    return {
        "before_revision_data": {
            "fb4d3ab5f38d": [
                {"__tablename__": "foo", "id": 1, "name": "foo"},
                {"__tablename__": "bar", "id": 1, "name": "bar"},
        "at_revision_data": {
            "a5a9ccc4e535": {
                "__tablename__": "foo",
                "id": 1,
                "name": "foo2",

There are two, similiar but distinct options. before_revision_data, and at_revision_data.

  • before_revision_data will insert the given data for all upgrade commands before performing the upgrade to the given revision.

  • at_revision_data will insert the given data for all upgrade commands after having performed the upgrade to the given revision.

To be clear, you can use either of them to describe the same operation. But depending on the circumstance (if for example, you are testing a real-world situation that you are trying to semantically model more closely), one or the other option may be more appropriate.


The value for either before_revision_data or at_revision_data, should be a dict() where the keys are the revision for which the data is being described.

The value can either be a dict() (single row), or a list() of dict() (multiple rows). __tablename__ is a special key which tells us the name of the table to insert the data into, and the rest of the spec should describe the columns and the column data for that row, similar to what you might do for a table.insert().values(...) call.

Alternatively, you can directly import and instantiate a RevisionSpec and set that as the value to either before_revision_data or at_revision_data.


Given the above data, and history of:

23256c7bf855 > fb4d3ab5f38d -> a5a9ccc4e535 -> b835ae9ff1d1

We would:

  • Upgrade to 23256c7bf855

  • Insert {"__tablename__": "foo", "id": 1, "name": "foo"} and {"__tablename__": "bar", "id": 1, "name": "bar"}

  • Upgrade to fb4d3ab5f38d

  • Upgrade to a5a9ccc4e535

  • Insert {"__tablename__": "foo", "id": 1, "name": "foo2"}

  • Upgrade to b835ae9ff1d1