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

Better support for default schemas #718

Open
stevennic opened this issue Jul 6, 2024 · 0 comments
Open

Better support for default schemas #718

stevennic opened this issue Jul 6, 2024 · 0 comments

Comments

@stevennic
Copy link

stevennic commented Jul 6, 2024

We should be able to specify a different default schema at the DB level from the beginning, and have all operations operate in that scope without further annotation. If I want to set a default schema on initialization with Postgres, I have to use the options binding to specify -c search_path=<schema> as a string. There should be a native way to set the schema.

There are also hard-coded assumptions that we are in the default schema. For example, dbapiprovider.py:split_table_name starts with this:

    def split_table_name(provider, table_name):
        if isinstance(table_name, str): return provider.default_schema_name, table_name

Given a table name, it hard codes its schema scope to the default_schema_name, even if the current DB connection is using a different schema. This is called by DBAPIProvider.table_exists to check if a table should be created when mappings are being generated. The default schema hard-code causes this to return False, as it's looking for the tables in the wrong schema, causing it to attempt to create a table that already exists (because the connection it uses to attempt to then create it is in the correct schema).

As a workaround, I subclassed the PGProvider and overrode split_table_name to return the current schema, but the correct solution is for the native Provider to handle this.

class MyPGProvider(PGProvider):
    def __init__(self, *args, schema: str = None, **kwargs):
        self._schema = schema
        super().__init__(*args, **kwargs)

    def split_table_name(self, table_name):
        if self._schema:
            return self._schema, table_name
        else:
            return super(self.__class__.__base__, self).split_table_name(table_name)  # Let it use the default schema
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

No branches or pull requests

1 participant