clickhouse-sqlalchemy icon indicating copy to clipboard operation
clickhouse-sqlalchemy copied to clipboard

Table reflection for DateTime64 timezone will be extra quoted

Open littlebtc opened this issue 9 months ago • 0 comments

Describe the bug Table reflection for DateTime64 timezone will be extra quoted

To Reproduce If you have a table like

class TestTable(Base):
    time = Column(types.DateTime64(3, "UTC"), primary_key=True)
    __tablename__ = "test_table"
    __table_args__ = (engines.MergeTree(order_by=("time",)),)

The first migration and table creation will work as expected. But running alembic revision --autogenerate after table created, something weird will happen:

INFO  [alembic.autogenerate.compare] Detected type change from DateTime64(timezone="'UTC'") to DateTime64(timezone='UTC') on 'test_table.time'

After tracing the code, it seems that _parse_detetime64_params did not handle the quotes correctly; when the inner spec is 3, 'UTC', it will return [3, "'UTC'"] thus hit the issue. In my own code I create patch to clickhouse_sqlalchemy.drivers.base.ClickHouseDialect._parse_detetime64_params:

    # ...
    if len(params) > 1:
        params[1] = params[1].strip()[1:-1]
    # ...

It looks dirty but the autogenerate works correctly.

Expected behavior

_parse_detetime64_params shall return [3, "UTC"] for inner spec like 3, 'UTC', not resulting weird detected type change.

Versions

  • clickhouse-sqlalchemy 0.3.1
  • SQLAlchemy 2.0.29
  • Python 3.11

littlebtc avatar May 09 '24 08:05 littlebtc