This commit is contained in:
2024-06-29 22:30:23 +03:00
parent fb8dbd1220
commit 1c104ab567
9 changed files with 116 additions and 19 deletions

View File

@@ -60,7 +60,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako # are written from script.py.mako
# output_encoding = utf-8 # output_encoding = utf-8
sqlalchemy.url = sqlalchemy.url = postgresql://{PG_LOGIN}:{PG_PASSWORD}@{PG_HOST}/:{PG_PORT}/{PG_DATABASE} sqlalchemy.url = postgresql://{PG_LOGIN}:{PG_PASSWORD}@{PG_HOST}/:{PG_PORT}/{PG_DATABASE}
[post_write_hooks] [post_write_hooks]

View File

@@ -1,10 +1,14 @@
import asyncio
import backend.config as settings
from logging.config import fileConfig from logging.config import fileConfig
from sqlalchemy import engine_from_config from sqlalchemy.engine import Connection
from sqlalchemy import pool
from alembic import context from alembic import context
from backend.session import engine
from models import BaseModel
# this is the Alembic Config object, which provides # this is the Alembic Config object, which provides
# access to the values within the .ini file in use. # access to the values within the .ini file in use.
config = context.config config = context.config
@@ -18,12 +22,28 @@ if config.config_file_name is not None:
# for 'autogenerate' support # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # target_metadata = mymodel.Base.metadata
target_metadata = None target_metadata = BaseModel.metadata
print(target_metadata.schema)
# other values from the config, defined by the needs of env.py, # other values from the config, defined by the needs of env.py,
# can be acquired: # can be acquired:
# my_important_option = config.get_main_option("my_important_option") # my_important_option = config.get_main_option("my_important_option")
# ... etc. # ... etc.
def include_object(object, name, type_, reflected, compare_to):
if type_ == 'table' and object.schema != target_metadata.schema:
return False
return True
def get_url():
url = config.get_main_option("sqlalchemy.url").format(
PG_LOGIN=settings.PG_LOGIN,
PG_PASSWORD=settings.PG_PASSWORD,
PG_HOST=settings.PG_HOST,
PG_DATABASE=settings.PG_DATABASE,
)
return url
def run_migrations_offline() -> None: def run_migrations_offline() -> None:
@@ -38,38 +58,59 @@ def run_migrations_offline() -> None:
script output. script output.
""" """
url = config.get_main_option("sqlalchemy.url") url = get_url()
context.configure( context.configure(
url=url, url=url,
target_metadata=target_metadata, target_metadata=target_metadata,
literal_binds=True, literal_binds=True,
dialect_opts={"paramstyle": "named"}, dialect_opts={"paramstyle": "named"},
version_table_schema=target_metadata.schema,
include_schemas=True
) )
with context.begin_transaction(): with context.begin_transaction():
"""
By default search_path is setted to "$user",public
that why alembic can't create foreign keys correctly
"""
context.execute('SET search_path TO public')
context.run_migrations() context.run_migrations()
def run_migrations_online() -> None: def do_run_migrations(connection: Connection) -> None:
"""Run migrations in 'online' mode. # Here we set our custom schema configuration
context.configure(
connection=connection,
target_metadata=target_metadata,
version_table="asia_alembic_version",
# Table in the db which will save the current alembic version of this schema
version_table_schema=target_metadata.schema, # Alternative: "public", to put version tables in seperate schema
include_schemas=True,
include_object=include_object,
)
with context.begin_transaction():
# context.execute(f'set search_path to {target_metadata.schema}')
In this scenario we need to create an Engine context.run_migrations()
async def run_async_migrations() -> None:
"""In this scenario we need to create an Engine
and associate a connection with the context. and associate a connection with the context.
""" """
connectable = engine_from_config( connectable = engine
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection: async with connectable.connect() as connection:
context.configure( await connection.run_sync(do_run_migrations)
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction(): await connectable.dispose()
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode(): if context.is_offline_mode():

0
backend/__init__.py Normal file
View File

10
backend/config.py Normal file
View File

@@ -0,0 +1,10 @@
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
PG_LOGIN = os.environ.get('PG_LOGIN')
PG_PASSWORD = os.environ.get('PG_PASSWORD')
PG_PORT = os.environ.get('PG_PORT')
PG_HOST = os.environ.get('PG_HOST')
PG_DATABASE = os.environ.get('PG_DATABASE')

24
backend/session.py Normal file
View File

@@ -0,0 +1,24 @@
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from .config import (
PG_HOST,
PG_LOGIN,
PG_PORT,
PG_DATABASE,
PG_PASSWORD,
)
database_url = (
f"postgresql+asyncpg://"
f"{PG_LOGIN}:{PG_PASSWORD}@"
f"{PG_HOST}:{PG_PORT}/{PG_DATABASE}"
)
engine = create_async_engine(database_url)
session_factory = async_sessionmaker(engine, expire_on_commit=False, autoflush=False, autocommit=False)
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with session_factory() as session:
yield session

2
migrate.sh Executable file
View File

@@ -0,0 +1,2 @@
alembic revision --autogenerate
alembic upgrade head

4
models/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
from sqlalchemy.orm import configure_mappers
from .base import *
configure_mappers()

13
models/base.py Normal file
View File

@@ -0,0 +1,13 @@
from sqlalchemy import MetaData, ForeignKey
from sqlalchemy.orm import declarative_base, Mapped, mapped_column
BaseModel = declarative_base(metadata=MetaData(schema='stocks'))
class DailyStock(BaseModel):
__tablename__ = 'daily_stocks'
__table_args__ = {
'schema': 'stocks',
}
product_id: Mapped[int] = mapped_column( primary_key=True)
sold_today: Mapped[int] = mapped_column()

3
test.py Normal file
View File

@@ -0,0 +1,3 @@
from alembic.env import run_migrations_offline
run_migrations_offline()