Created
August 14, 2021 18:45
-
-
Save amalshaji/a156d737aa06ce12c5d7a8f7ce4e6ffc to your computer and use it in GitHub Desktop.
GIST created by python code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
├── Pipfile | |
├── Pipfile.lock | |
├── alembic.ini | |
├── db.py | |
├── db.sqlite | |
├── main.py | |
└── migrations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqlalchemy.url = sqlite:///db.sqlite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db.py | |
class Users(ormar.Model): | |
class Meta(BaseMeta): | |
tablename = "users" | |
id: int = ormar.Integer(primary_key=True) | |
email: str = ormar.String(max_length=64, unique=True) | |
password: str = ormar.String(max_length=128) | |
is_active: bool = ormar.Boolean(default=True) # new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fastapi-ormar-alembic) $ alembic init migrations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from db import database | |
app = FastAPI() | |
@app.on_event("startup") | |
async def startup(): | |
if not database.is_connected: | |
await database.connect() | |
@app.on_event("shutdown") | |
async def shutdown(): | |
if database.is_connected: | |
await database.disconnect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sqlite3 db.sqlite | |
SQLite version 3.31.1 2020-01-27 19:55:54 | |
Enter ".help" for usage hints. | |
sqlite> .schema users | |
CREATE TABLE users ( | |
id INTEGER NOT NULL, | |
email VARCHAR(64) NOT NULL, | |
password VARCHAR(128) NOT NULL, is_active BOOLEAN, | |
PRIMARY KEY (id), | |
UNIQUE (email) | |
); | |
sqlite> .quit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fastapi-ormar-alembic) $ alembic revision --autogenerate -m "Added is_active to users table" | |
INFO [alembic.runtime.migration] Context impl SQLiteImpl. | |
INFO [alembic.runtime.migration] Will assume non-transactional DDL. | |
INFO [alembic.autogenerate.compare] Detected added column 'users.is_active' | |
Generating /home/amalshaji/Workspace/Python/blog-code-repository/fastapi-ormar- | |
alembic/migrations/versions/026a9a23ebbe_added_is_active_to_users_table.py ... done | |
(fastapi-ormar-alembic) $ alembic upgrade head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
from db import BaseMeta | |
... | |
target_metadata = BaseMeta.metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir fastapi-ormar-alembic && cd $_ | |
$ mkdir .venv | |
$ pipenv install fastapi uvicorn ormar alembic aiosqlite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import databases | |
import ormar | |
import sqlalchemy | |
database = databases.Database("sqlite:///db.sqlite") | |
metadata = sqlalchemy.MetaData() | |
class BaseMeta(ormar.ModelMeta): | |
database = database | |
metadata = metadata | |
class Users(ormar.Model): | |
class Meta(BaseMeta): | |
tablename = "users" | |
id: int = ormar.Integer(primary_key=True) | |
email: str = ormar.String(max_length=64, unique=True) | |
password: str = ormar.String(max_length=128) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Added users table | |
Revision ID: c07fe5d55962 | |
Revises: | |
Create Date: 2021-08-14 11:55:46.845709 | |
""" | |
from alembic import op | |
import sqlalchemy as sa | |
# revision identifiers, used by Alembic. | |
revision = 'c07fe5d55962' | |
down_revision = None | |
branch_labels = None | |
depends_on = None | |
def upgrade(): | |
# ### commands auto generated by Alembic - please adjust! ### | |
op.create_table('users', | |
sa.Column('id', sa.Integer(), nullable=False), | |
sa.Column('email', sa.String(length=64), nullable=False), | |
sa.Column('password', sa.String(length=128), nullable=False), | |
sa.PrimaryKeyConstraint('id'), | |
sa.UniqueConstraint('email') | |
) | |
# ### end Alembic commands ### | |
def downgrade(): | |
# ### commands auto generated by Alembic - please adjust! ### | |
op.drop_table('users') | |
# ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fastapi-ormar-alembic) $ alembic revision --autogenerate -m "Added users table" | |
INFO [alembic.runtime.migration] Context impl SQLiteImpl. | |
INFO [alembic.runtime.migration] Will assume non-transactional DDL. | |
INFO [alembic.autogenerate.compare] Detected added table 'users' | |
Generating /home/amalshaji/Workspace/Python/blog-code-repository/fastapi-ormar- | |
alembic/migrations/versions/c07fe5d55962_added_users_table.py ... done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Added is_active to users table | |
Revision ID: 026a9a23ebbe | |
Revises: c07fe5d55962 | |
Create Date: 2021-08-14 12:20:36.817128 | |
""" | |
from alembic import op | |
import sqlalchemy as sa | |
# revision identifiers, used by Alembic. | |
revision = '026a9a23ebbe' | |
down_revision = 'c07fe5d55962' | |
branch_labels = None | |
depends_on = None | |
def upgrade(): | |
# ### commands auto generated by Alembic - please adjust! ### | |
op.add_column('users', sa.Column('is_active', sa.Boolean(), nullable=True)) | |
# ### end Alembic commands ### | |
def downgrade(): | |
# ### commands auto generated by Alembic - please adjust! ### | |
op.drop_column('users', 'is_active') | |
# ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fastapi-ormar-alembic) $ alembic upgrade head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment