Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions ydb_sqlalchemy/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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", {})

Expand Down Expand Up @@ -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)
Expand Down