Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alembic with asynch driver fails on commit() #352

Open
kusaku opened this issue Nov 20, 2024 · 0 comments
Open

Alembic with asynch driver fails on commit() #352

kusaku opened this issue Nov 20, 2024 · 0 comments

Comments

@kusaku
Copy link

kusaku commented Nov 20, 2024

Describe the bug
When using asynch driver, alembic migrations fail on commit:

alembic upgrade head     
INFO  [sqlalchemy.engine.Engine] select version()
INFO  [sqlalchemy.engine.Engine] [generated in 0.00012s] {}
INFO  [sqlalchemy.engine.Engine] select currentDatabase()
INFO  [sqlalchemy.engine.Engine] [generated in 0.00008s] {}
INFO  [alembic.runtime.migration] Context impl ClickHouseDialectImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [sqlalchemy.engine.Engine] BEGIN (implicit)
INFO  [sqlalchemy.engine.Engine] EXISTS TABLE "alembic_version"
INFO  [sqlalchemy.engine.Engine] [generated in 0.00010s] {}
INFO  [sqlalchemy.engine.Engine] SELECT alembic_version.version_num 
FROM alembic_version
INFO  [sqlalchemy.engine.Engine] [generated in 0.00007s] {}
INFO  [sqlalchemy.engine.Engine] EXISTS TABLE "alembic_version"
INFO  [sqlalchemy.engine.Engine] [cached since 0.008872s ago] {}
INFO  [alembic.runtime.migration] Running upgrade  -> 66b14befc5fc, init
INFO  [sqlalchemy.engine.Engine] 
CREATE TABLE metrics (
    ...
) ENGINE = ReplacingMergeTree()
 PARTITION BY ...
 ORDER BY (...)


INFO  [sqlalchemy.engine.Engine] [no key 0.00006s] {}
INFO  [sqlalchemy.engine.Engine] INSERT INTO alembic_version (version_num) VALUES ('66b14befc5fc')
INFO  [sqlalchemy.engine.Engine] [generated in 0.00009s] {}
INFO  [sqlalchemy.engine.Engine] COMMIT
 ...

  File "/Users/User/Projects/chx/.venv/lib/python3.12/site-packages/asynch/connection.py", line 181, in commit
    raise NotSupportedError
asynch.errors.NotSupportedError: Code: None.

To Reproduce

  • use async engine:
async_engine: AsyncEngine = create_async_engine(
    url='clickhouse+asynch://localhost/default',
    echo=True,
    echo_pool=True,
    enable_from_linting=True,
)
async_session: AsyncSession = make_session(async_engine, is_async=True)
Base = get_declarative_base()

class Metrics(Base):
    ... # omitted

    __tablename__ = 'metrics'
    __table_args__ = (
        ReplacingMergeTree(
            ... # omitted
        ),
    )
  • use alembic env.py file for async engine:
import asyncio
from logging.config import fileConfig

from alembic import context
from clickhouse_sqlalchemy.alembic.dialect import include_object, patch_alembic_version

from ... import async_engine, Base

if context.config.config_file_name is not None:
    fileConfig(context.config.config_file_name)

def run_migrations_sync(conn):
    context.configure(
        connection=conn,
        include_object=include_object,
        target_metadata=Base.metadata,
        compare_server_default=True,
        transactional_ddl=False,
        literal_binds=context.is_offline_mode(),
    )

    patch_alembic_version(context)
    context.run_migrations()


async def run_migrations_async():
    async with async_engine.connect() as conn:
        await conn.run_sync(run_migrations_sync)


if context.is_offline_mode():
    context.run_migrations()
else:
    asyncio.run(run_migrations_async())
  • generate new alembic revision and apply it

Expected behavior

Alembic may commit, but it is not executed, asynch.connection.Connection.commit() is never called.

Versions

poetry show --tree 
alembic 1.14.0 A database migration tool for SQLAlchemy.
├── mako *
│   └── markupsafe >=0.9.2 
├── sqlalchemy >=1.3.0
│   ├── greenlet !=0.4.17 
│   └── typing-extensions >=4.6.0 
└── typing-extensions >=4
clickhouse-sqlalchemy 0.3.2 Simple ClickHouse SQLAlchemy Dialect
├── asynch >=0.2.2
│   ├── ciso8601 * 
│   ├── leb128 * 
│   ├── lz4 * 
│   ├── pytz * 
│   ├── tzlocal * 
│   │   └── tzdata * 
│   └── zstd * 
├── clickhouse-driver >=0.1.2
│   ├── pytz * 
│   └── tzlocal * 
│       └── tzdata * 
├── requests *
│   ├── certifi >=2017.4.17 
│   ├── charset-normalizer >=2,<4 
│   ├── idna >=2.5,<4 
│   └── urllib3 >=1.21.1,<3 
└── sqlalchemy >=2.0.0,<2.1.0
    ├── greenlet !=0.4.17 
    └── typing-extensions >=4.6.0 
greenlet 3.1.1 Lightweight in-process concurrent programming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant