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

Wrong index detection on cockroachdb #1356

Closed
OndrejIT opened this issue Nov 14, 2023 · 4 comments
Closed

Wrong index detection on cockroachdb #1356

OndrejIT opened this issue Nov 14, 2023 · 4 comments
Labels
external driver issues the issue involves a misbehavior on the part of the DBAPI itself, probably not SQLAlchemy or Alembic index expressions

Comments

@OndrejIT
Copy link

OndrejIT commented Nov 14, 2023

Hi, I'm trying to make a migration on my cockroachdb DB + sqlmodel.
when I first init alembic and create DB everything works, if I scan again (alembic revision --autogenerate) for possible changes, alembic prints "Detected changed index 'ix_file_bucket' on 'file': expression ('INDEX', 'bucket nulls first') to ('INDEX', 'bucket')" (I didn't change anything in code and database)
and alembic creates the following migrations:

op.drop_index(op.f('ix_file_bucket'), table_name='file')
op.create_index('ix_file_bucket', 'file', [sa.text('bucket NULLS FIRST')], unique=False, postgresql_using='prefix')

the next time the alembic is triggered, it fails.
with the message:
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.ObjectNotInPrerequisiteStateError'>: index "ix_file_bucket" is cancelled, try again later

is there any way to solve this? currently I have to manually delete bad migrations line every time.

Versions.

  • OS: Docker
  • Python: docker.io/python:3.12-alpine
  • Alembic: 1.12.1
  • SQLAlchemy: 1.4.41 + sqlmodel==0.0.11
  • Database: cockroachdb v23.1.11
  • DBAPI: sqlalchemy-cockroachdb==2.0.1
@OndrejIT OndrejIT added the requires triage New issue that requires categorization label Nov 14, 2023
@zzzeek
Copy link
Member

zzzeek commented Nov 14, 2023

this is some issue with both index reflection and something with the driver itself that is out of scope for alembic, that error message is unknown to us here so you'd want to ask on the sqlalchemy-cockroachDB driver tracker, cc @gordthompson

@zzzeek zzzeek closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2023
@zzzeek zzzeek added external driver issues the issue involves a misbehavior on the part of the DBAPI itself, probably not SQLAlchemy or Alembic and removed requires triage New issue that requires categorization labels Nov 14, 2023
@CaselIT
Copy link
Member

CaselIT commented Nov 14, 2023

is there any way to solve this? currently I have to manually delete bad migrations line every time.

this is similar to the other issue with expression reflection. you are placing order modifying in the expression.
Instead of sa.text('bucket NULLS FIRST') use bucket.nulls_first() or sa.column('bucket').nulls_first() or sa.nulls_first(sa.text('bucket')) in the index definition

@gordthompson
Copy link
Member

Alembic is doing that text() thing. When I tested it with model

class Account(Base):
    __tablename__ = "account"
    account_number = Column(Integer, primary_key=True)
    customer_name = Column(String(50))
    invoices = relationship("Invoice", back_populates="account")
    Index("ix_account_customer_name", customer_name.nullsfirst())

running

alembic revision --autogenerate -m "Added account and invoice tables"

produced a revision file that included

op.create_index('ix_account_customer_name', 'account', [sa.text('customer_name NULLS FIRST')], unique=False)

That said, I am unable to reproduce the issue against CockroachDB using psycopg2, and if I try using asyncpg then Alembic doesn't like it at all:

sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/14/xd2s)  
sys:1: RuntimeWarning: coroutine 'connect' was never awaited

@sqla-tester
Copy link
Collaborator

Federico Caselli has proposed a fix for this issue in the main branch:

More PostgreSQL expression index compare fixes https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4912

sqlalchemy-bot pushed a commit that referenced this issue Nov 21, 2023
Additional fixes to PostgreSQL expression index compare feature.
The compare now correctly accommodates casts and differences in
spacing.
Added detection logic for operation clauses inside the expression,
skipping the compare of these expressions.
To accommodate these changes the logic for the comparison of the
indexes and unique constraints was moved to the dialect
implementation, allowing greater flexibility.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #1321
Fixes: #1327
Fixes: #1356
Change-Id: Icad15bc556a63bfa55b84779e7691c745d943c63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external driver issues the issue involves a misbehavior on the part of the DBAPI itself, probably not SQLAlchemy or Alembic index expressions
Projects
None yet
Development

No branches or pull requests

5 participants