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

Remove leftover indices #137

Merged
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
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Changelog of threedi-schema
0.228.1 (unreleased)
--------------------

- Nothing chaned yet
- Remove indices referring to removed tables in previous migrations



Expand Down
21 changes: 20 additions & 1 deletion threedi_schema/migrations/versions/0229_clean_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

"""

from typing import List

import sqlalchemy as sa
from alembic import op

Expand All @@ -16,8 +18,25 @@
depends_on = None


def remove_tables(tables: List[str]):
for table in tables:
op.drop_table(table)



def find_tables_by_pattern(pattern: str) -> List[str]:
connection = op.get_bind()
query = connection.execute(sa.text(f"select name from sqlite_master where type = 'table' and name like '{pattern}'"))
return [item[0] for item in query.fetchall()]


def remove_old_tables():
remaining_v2_idx_tables = find_tables_by_pattern('idx_v2_%_the_geom')
remaining_alembic = find_tables_by_pattern('%_alembic_%_the_geom')
remove_tables(remaining_v2_idx_tables+remaining_alembic)

def upgrade():
pass
remove_old_tables()


def downgrade():
Expand Down
2 changes: 1 addition & 1 deletion threedi_schema/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_set_spatial_indexes(in_memory_sqlite):
connection.execute(
text("SELECT DisableSpatialIndex('connection_node', 'geom')")
).scalar()
connection.execute(text("DROP TABLE idx_v2_connection_nodes_the_geom"))
connection.execute(text("DROP TABLE idx_connection_node_geom"))

schema.set_spatial_indexes()

Expand Down