Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🎨 update: code format
  • Loading branch information
ezeparziale committed Sep 7, 2025
commit 7bcf499b4dac05f91ce492bb82e8533ee8fdc7a5
4 changes: 2 additions & 2 deletions docs/advanced/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Then go to `migrations\script.py.mako` to add sqlmodel module.
```Python hl_lines="5-5"
# Code above omitted 👆

{!./docs_src/advanced/migrations/tutorial003.py[ln:8-10]!}
{!./docs_src/advanced/migrations/tutorial003.mako[ln:8-10]!}

# Code below omitted 👇
```
Expand All @@ -104,7 +104,7 @@ Then go to `migrations\script.py.mako` to add sqlmodel module.
<summary>👀 Full script.py.mako example</summary>

```Python
{!./docs_src/advanced/migrations/tutorial003.py!}
{!./docs_src/advanced/migrations/tutorial003.mako!}
```

</details>
Expand Down
1 change: 1 addition & 0 deletions docs_src/advanced/migrations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Hero(SQLModel, table=True):

engine = create_engine(sqlite_url, echo=True)


def create_heroes():
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
Expand Down
21 changes: 7 additions & 14 deletions docs_src/advanced/migrations/tutorial004.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from logging.config import fileConfig
from logging.config import fileConfig # isort:skip

from main import Hero
from sqlmodel import SQLModel
from main import Hero # isort:skip
from sqlmodel import SQLModel # isort:skip

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from sqlalchemy import engine_from_config # isort:skip
from sqlalchemy import pool # isort:skip

from alembic import context
from alembic import context # isort:skip

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -32,15 +32,12 @@

def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
Expand All @@ -56,10 +53,8 @@ def run_migrations_offline() -> None:

def run_migrations_online() -> None:
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
Expand All @@ -68,9 +63,7 @@ def run_migrations_online() -> None:
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()
Expand Down
25 changes: 14 additions & 11 deletions docs_src/advanced/migrations/tutorial005.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@
Create Date:

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel # (1)
from alembic import op # isort:skip
import sqlalchemy as sa # isort:skip
import sqlmodel # (1), # isort:skip


# revision identifiers, used by Alembic.
revision = '34abfb7ac266'
revision = "34abfb7ac266"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None: # (2)
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('hero', # (3)
sa.Column('id', sa.Integer(), nullable=False), # (4)
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False), # (5)
sa.Column('secret_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False), # (6)
sa.Column('age', sa.Integer(), nullable=True), # (7)
sa.PrimaryKeyConstraint('id') # (8)
op.create_table(
"hero", # (3)
sa.Column("id", sa.Integer(), nullable=False), # (4)
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), # (5)
sa.Column(
"secret_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False
), # (6)
sa.Column("age", sa.Integer(), nullable=True), # (7)
sa.PrimaryKeyConstraint("id"), # (8)
)
# ### end Alembic commands ###


def downgrade() -> None: # (9)
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('hero') # (10)
op.drop_table("hero") # (10)
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions docs_src/advanced/migrations/tutorial006.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Hero(SQLModel, table=True):

engine = create_engine(sqlite_url, echo=True)


def create_heroes():
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
Expand Down
14 changes: 7 additions & 7 deletions docs_src/advanced/migrations/tutorial007.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
Create Date:

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
from alembic import op # isort:skip
import sqlalchemy as sa # isort:skip
import sqlmodel # isort:skip


# revision identifiers, used by Alembic.
revision = 'b39b8d3c77f0' # (1)
down_revision = '357d6ebcfadf' # (2)
revision = "b39b8d3c77f0" # (1)
down_revision = "357d6ebcfadf" # (2)
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('hero', sa.Column('power', sa.Integer(), nullable=True)) # (3)
op.add_column("hero", sa.Column("power", sa.Integer(), nullable=True)) # (3)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('hero', 'power') # (4)
op.drop_column("hero", "power") # (4)
# ### end Alembic commands ###