Skip to content

Commit bff441c

Browse files
authored
Merge pull request #54 from LuckySting/deletion-of-index
Add support of DROP INDEX statement
2 parents 8c589ae + f58ee2b commit bff441c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from sqlalchemy.engine import characteristics, reflection
1414
from sqlalchemy.engine.default import DefaultExecutionContext, StrCompileDialect
1515
from sqlalchemy.exc import CompileError, NoSuchTableError
16-
from sqlalchemy.sql import functions, literal_column
16+
from sqlalchemy.sql import ddl, functions, literal_column
1717
from sqlalchemy.sql.compiler import (
1818
DDLCompiler,
1919
IdentifierPreparer,
@@ -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: ddl.CreateIndex, **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: ddl.DropIndex, **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)