diff --git a/test/test_core.py b/test/test_core.py index 88f0aab..5fdcb84 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -1100,6 +1100,25 @@ def test_index_with_join_usage(self, connection: sa.Connection, metadata: sa.Met cursor = connection.execute(select_stmt) assert cursor.one() == ("Sarah Connor", "wanted") + def test_index_deletion(self, connection: sa.Connection, metadata: sa.MetaData): + persons = Table( + "test_index_deletion/persons", + metadata, + sa.Column("id", sa.Integer(), primary_key=True), + sa.Column("tax_number", sa.Integer()), + sa.Column("full_name", sa.Unicode()), + ) + persons.create(connection) + index = sa.Index("ix_tax_number", "tax_number", _table=persons) + index.create(connection) + indexes = sa.inspect(connection).get_indexes(persons.name) + assert len(indexes) == 1 + + index.drop(connection) + + indexes = sa.inspect(connection).get_indexes(persons.name) + assert len(indexes) == 0 + class TestTablePathPrefix(TablesTest): __backend__ = True diff --git a/ydb_sqlalchemy/sqlalchemy/__init__.py b/ydb_sqlalchemy/sqlalchemy/__init__.py index 326b47f..0890e0d 100644 --- a/ydb_sqlalchemy/sqlalchemy/__init__.py +++ b/ydb_sqlalchemy/sqlalchemy/__init__.py @@ -13,7 +13,7 @@ from sqlalchemy.engine import characteristics, reflection from sqlalchemy.engine.default import DefaultExecutionContext, StrCompileDialect from sqlalchemy.exc import CompileError, NoSuchTableError -from sqlalchemy.sql import functions, literal_column +from sqlalchemy.sql import ddl, functions, literal_column from sqlalchemy.sql.compiler import ( DDLCompiler, IdentifierPreparer, @@ -462,7 +462,7 @@ def visit_upsert(self, insert_stmt, visited_bindparam=None, **kw): class YqlDDLCompiler(DDLCompiler): - def visit_create_index(self, create, include_schema=False, include_table_schema=True, **kw): + def visit_create_index(self, create: ddl.CreateIndex, **kw) -> str: index: sa.Index = create.element ydb_opts = index.dialect_options.get("ydb", {}) @@ -491,6 +491,16 @@ def visit_create_index(self, create, include_schema=False, include_table_schema= return text + def visit_drop_index(self, drop: ddl.DropIndex, **kw) -> str: + index: sa.Index = drop.element + + self._verify_index_table(index) + + table_name = self.preparer.format_table(index.table) + index_name = self._prepared_index_name(index) + + return f"ALTER TABLE {table_name} DROP INDEX {index_name}" + def post_create_table(self, table: sa.Table) -> str: ydb_opts = table.dialect_options["ydb"] with_clause_list = self._render_table_partitioning_settings(ydb_opts)