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

manager.has_index for migrations #1827

Closed
IgnisDa opened this issue Aug 25, 2023 · 4 comments · Fixed by #1828
Closed

manager.has_index for migrations #1827

IgnisDa opened this issue Aug 25, 2023 · 4 comments · Fixed by #1828

Comments

@IgnisDa
Copy link
Contributor

IgnisDa commented Aug 25, 2023

Motivation

The SchemaManager has convenience methods like has_table and has_column while writing migrations. I propose a has_index method.

Proposed Solutions

It will return true if an index exists for a given table.

Current Workarounds

Execute the query manually.

If accepted I would like to make a PR.

@IgnisDa
Copy link
Contributor Author

IgnisDa commented Aug 25, 2023

For reference, here are the queries:

-- SQLITE
SELECT EXISTS (
    SELECT 1
    FROM sqlite_master
    WHERE type = 'index'
      AND name = 'metadata_identifier__index'
      AND tbl_name = 'metadata'
) AS index_exists;

-- POSTGRES
SELECT EXISTS (
    SELECT 1
    FROM pg_indexes
    WHERE schemaname = 'public'
      AND tablename = 'metadata'
      AND indexname = 'metadata_identifier__index'
) AS index_exists;

-- MYSQL
SELECT EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = 'your_database_name'
      AND TABLE_NAME = 'metadata'
      AND INDEX_NAME = 'metadata_identifier__index'
) AS index_exists;

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 26, 2023

Sure. Such a feature would be welcomed. The relevant code should go into SeaSchema and then wrapped in SeaORM.

@IgnisDa
Copy link
Contributor Author

IgnisDa commented Aug 26, 2023

@tyt2y3 Thanks for the hint. For Postgres and MySQL(?) do I need to schema into account or do I assume that we are working with the public schema?

EDIT: NVM figured it out.

@IgnisDa
Copy link
Contributor Author

IgnisDa commented Aug 26, 2023

@tyt2y3 Made the PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants