Skip to content

Commit 13e1695

Browse files
authored
Merge pull request #17 from amirtds/main
Bump dependancies
2 parents 9cdcad4 + ede3484 commit 13e1695

13 files changed

+127
-50
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Michael Herman
3+
Copyright (c) 2023 Michael Herman
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Add a song:
2121
$ curl -d '{"name":"Midnight Fit", "artist":"Mogwai", "year":"2021"}' -H "Content-Type: application/json" -X POST http://localhost:8004/songs
2222
```
2323

24-
Get all songs: [http://localhost:8004/songs](http://localhost:8004/songs)
24+
Get all songs: [http://localhost:8004/songs](http://localhost:8004/songs)

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- db
1616

1717
db:
18-
image: postgres:13.4
18+
image: postgres:15.3
1919
expose:
2020
- 5432
2121
environment:

project/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pull official base image
2-
FROM python:3.9-slim-buster
2+
FROM python:3.11-slim-buster
33

44
# set working directory
55
WORKDIR /usr/src/app

project/alembic.ini

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# path to migration scripts
55
script_location = migrations
66

7-
# template used to generate migration files
8-
# file_template = %%(rev)s_%%(slug)s
7+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
8+
# Uncomment the line below if you want the files to be prepended with date and time
9+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
910

1011
# sys.path path, will be prepended to sys.path if present.
1112
# defaults to the current working directory.
@@ -35,16 +36,23 @@ prepend_sys_path = .
3536
# version location specification; This defaults
3637
# to migrations/versions. When using multiple version
3738
# directories, initial revisions must be specified with --version-path.
38-
# The path separator used here should be the separator specified by "version_path_separator"
39+
# The path separator used here should be the separator specified by "version_path_separator" below.
3940
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
4041

4142
# version path separator; As mentioned above, this is the character used to split
42-
# version_locations. Valid values are:
43+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
44+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
45+
# Valid values for version_path_separator are:
4346
#
4447
# version_path_separator = :
4548
# version_path_separator = ;
4649
# version_path_separator = space
47-
version_path_separator = os # default: use os.pathsep
50+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
51+
52+
# set to 'true' to search source files recursively
53+
# in each "version_locations" directory
54+
# new in Alembic version 1.10
55+
# recursive_version_locations = false
4856

4957
# the output encoding used when revision files
5058
# are written from script.py.mako

project/app/db.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import os
22

3-
from sqlmodel import SQLModel
3+
from sqlmodel import SQLModel, create_engine
4+
from sqlmodel.ext.asyncio.session import AsyncSession, AsyncEngine
45

5-
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
66
from sqlalchemy.orm import sessionmaker
77

88

99
DATABASE_URL = os.environ.get("DATABASE_URL")
1010

11-
engine = create_async_engine(DATABASE_URL, echo=True, future=True)
12-
11+
engine = AsyncEngine(create_engine(DATABASE_URL, echo=True, future=True))
1312

1413
async def init_db():
1514
async with engine.begin() as conn:
@@ -22,4 +21,4 @@ async def get_session() -> AsyncSession:
2221
engine, class_=AsyncSession, expire_on_commit=False
2322
)
2423
async with async_session() as session:
25-
yield session
24+
yield session

project/app/main.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from fastapi import Depends, FastAPI
2-
from sqlalchemy.future import select
3-
from sqlalchemy.ext.asyncio import AsyncSession
2+
from sqlmodel import select
3+
from sqlmodel.ext.asyncio.session import AsyncSession
44

5-
from app.db import get_session
5+
from app.db import get_session, init_db
66
from app.models import Song, SongCreate
77

88
app = FastAPI()
@@ -26,4 +26,4 @@ async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session
2626
session.add(song)
2727
await session.commit()
2828
await session.refresh(song)
29-
return song
29+
return song

project/app/models.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from typing import Optional
2-
31
from sqlmodel import SQLModel, Field
2+
from typing import Optional
43

54

65
class SongBase(SQLModel):
@@ -10,8 +9,8 @@ class SongBase(SQLModel):
109

1110

1211
class Song(SongBase, table=True):
13-
id: int = Field(default=None, primary_key=True)
12+
id: int = Field(default=None, nullable=False, primary_key=True)
1413

1514

1615
class SongCreate(SongBase):
17-
pass
16+
pass

project/migrations/env.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
import asyncio
22
from logging.config import fileConfig
33

4-
from sqlalchemy import engine_from_config
54
from sqlalchemy import pool
6-
from sqlalchemy.ext.asyncio import AsyncEngine
7-
from sqlmodel import SQLModel
5+
from sqlalchemy.engine import Connection
6+
from sqlalchemy.ext.asyncio import async_engine_from_config
7+
from sqlmodel import SQLModel # NEW
8+
89

910
from alembic import context
1011

11-
from app.models import Song
12+
from app.models import Song # NEW
1213

1314
# this is the Alembic Config object, which provides
1415
# access to the values within the .ini file in use.
1516
config = context.config
1617

1718
# Interpret the config file for Python logging.
1819
# This line sets up loggers basically.
19-
fileConfig(config.config_file_name)
20+
if config.config_file_name is not None:
21+
fileConfig(config.config_file_name)
2022

2123
# add your model's MetaData object here
2224
# for 'autogenerate' support
2325
# from myapp import mymodel
2426
# target_metadata = mymodel.Base.metadata
25-
target_metadata = SQLModel.metadata
27+
target_metadata = SQLModel.metadata # UPDATED
2628

2729
# other values from the config, defined by the needs of env.py,
2830
# can be acquired:
2931
# my_important_option = config.get_main_option("my_important_option")
3032
# ... etc.
3133

3234

33-
def run_migrations_offline():
35+
def run_migrations_offline() -> None:
3436
"""Run migrations in 'offline' mode.
3537
3638
This configures the context with just a URL
@@ -54,34 +56,38 @@ def run_migrations_offline():
5456
context.run_migrations()
5557

5658

57-
def do_run_migrations(connection):
59+
def do_run_migrations(connection: Connection) -> None:
5860
context.configure(connection=connection, target_metadata=target_metadata)
5961

6062
with context.begin_transaction():
6163
context.run_migrations()
6264

6365

64-
async def run_migrations_online():
65-
"""Run migrations in 'online' mode.
66-
67-
In this scenario we need to create an Engine
66+
async def run_async_migrations() -> None:
67+
"""In this scenario we need to create an Engine
6868
and associate a connection with the context.
6969
7070
"""
71-
connectable = AsyncEngine(
72-
engine_from_config(
73-
config.get_section(config.config_ini_section),
74-
prefix="sqlalchemy.",
75-
poolclass=pool.NullPool,
76-
future=True,
77-
)
71+
72+
connectable = async_engine_from_config(
73+
config.get_section(config.config_ini_section, {}),
74+
prefix="sqlalchemy.",
75+
poolclass=pool.NullPool,
7876
)
7977

8078
async with connectable.connect() as connection:
8179
await connection.run_sync(do_run_migrations)
8280

81+
await connectable.dispose()
82+
83+
84+
def run_migrations_online() -> None:
85+
"""Run migrations in 'online' mode."""
86+
87+
asyncio.run(run_async_migrations())
88+
8389

8490
if context.is_offline_mode():
8591
run_migrations_offline()
8692
else:
87-
asyncio.run(run_migrations_online())
93+
run_migrations_online()

project/migrations/script.py.mako

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Create Date: ${create_date}
77
"""
88
from alembic import op
99
import sqlalchemy as sa
10-
import sqlmodel
10+
import sqlmodel # NEW
1111
${imports if imports else ""}
1212

1313
# revision identifiers, used by Alembic.
@@ -17,9 +17,9 @@ branch_labels = ${repr(branch_labels)}
1717
depends_on = ${repr(depends_on)}
1818

1919

20-
def upgrade():
20+
def upgrade() -> None:
2121
${upgrades if upgrades else "pass"}
2222

2323

24-
def downgrade():
24+
def downgrade() -> None:
2525
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""init
2+
3+
Revision ID: 842abcd80d3e
4+
Revises:
5+
Create Date: 2023-07-10 17:10:45.380832
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel # NEW
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = '842abcd80d3e'
15+
down_revision = None
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade() -> None:
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.create_table('song',
23+
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
24+
sa.Column('artist', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
25+
sa.Column('id', sa.Integer(), nullable=False),
26+
sa.PrimaryKeyConstraint('id')
27+
)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade() -> None:
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_table('song')
34+
# ### end Alembic commands ###
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""add year
2+
3+
Revision ID: f68b489cdeb0
4+
Revises: 842abcd80d3e
5+
Create Date: 2023-07-10 17:12:35.936572
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel # NEW
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = 'f68b489cdeb0'
15+
down_revision = '842abcd80d3e'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.add_column('song', sa.Column('year', sa.Integer(), nullable=True))
23+
op.create_index(op.f('ix_song_year'), 'song', ['year'], unique=False)
24+
# ### end Alembic commands ###
25+
26+
27+
def downgrade():
28+
# ### commands auto generated by Alembic - please adjust! ###
29+
op.drop_index(op.f('ix_song_year'), table_name='song')
30+
op.drop_column('song', 'year')
31+
# ### end Alembic commands ###

project/requirements.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
alembic==1.7.1
2-
asyncpg==0.24.0
3-
fastapi==0.68.1
4-
sqlmodel==0.0.4
5-
uvicorn==0.15.0
1+
alembic==1.11.1
2+
asyncpg==0.28.0
3+
fastapi==0.100.0
4+
sqlmodel==0.0.8
5+
uvicorn==0.22.0

0 commit comments

Comments
 (0)