Skip to content

Commit 4ca4704

Browse files
committed
Add support of DROP INDEX statement
1 parent 8c589ae commit 4ca4704

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

test/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,25 @@ def test_index_with_join_usage(self, connection: sa.Connection, metadata: sa.Met
11001100
cursor = connection.execute(select_stmt)
11011101
assert cursor.one() == ("Sarah Connor", "wanted")
11021102

1103+
def test_index_deletion(self, connection: sa.Connection, metadata: sa.MetaData):
1104+
persons = Table(
1105+
"test_index_deletion/persons",
1106+
metadata,
1107+
sa.Column("id", sa.Integer(), primary_key=True),
1108+
sa.Column("tax_number", sa.Integer()),
1109+
sa.Column("full_name", sa.Unicode()),
1110+
)
1111+
persons.create(connection)
1112+
index = sa.Index("ix_tax_number", "tax_number", _table=persons)
1113+
index.create(connection)
1114+
indexes = sa.inspect(connection).get_indexes(persons.name)
1115+
assert len(indexes) == 1
1116+
1117+
index.drop(connection)
1118+
1119+
indexes = sa.inspect(connection).get_indexes(persons.name)
1120+
assert len(indexes) == 0
1121+
11031122

11041123
class TestTablePathPrefix(TablesTest):
11051124
__backend__ = True

ydb_sqlalchemy/sqlalchemy/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def visit_upsert(self, insert_stmt, visited_bindparam=None, **kw):
462462

463463

464464
class YqlDDLCompiler(DDLCompiler):
465-
def visit_create_index(self, create, include_schema=False, include_table_schema=True, **kw):
465+
def visit_create_index(self, create, include_schema=False, include_table_schema=True, **kw) -> str:
466466
index: sa.Index = create.element
467467
ydb_opts = index.dialect_options.get("ydb", {})
468468

@@ -491,6 +491,16 @@ def visit_create_index(self, create, include_schema=False, include_table_schema=
491491

492492
return text
493493

494+
def visit_drop_index(self, drop, include_schema=False, include_table_schema=True, **kw) -> str:
495+
index: sa.Index = drop.element
496+
497+
self._verify_index_table(index)
498+
499+
table_name = self.preparer.format_table(index.table)
500+
index_name = self._prepared_index_name(index)
501+
502+
return f"ALTER TABLE {table_name} DROP INDEX {index_name}"
503+
494504
def post_create_table(self, table: sa.Table) -> str:
495505
ydb_opts = table.dialect_options["ydb"]
496506
with_clause_list = self._render_table_partitioning_settings(ydb_opts)

0 commit comments

Comments
 (0)