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

Create custom types #1168

Open
MyGodIsHe opened this issue Jan 20, 2025 · 2 comments
Open

Create custom types #1168

MyGodIsHe opened this issue Jan 20, 2025 · 2 comments

Comments

@MyGodIsHe
Copy link

MyGodIsHe commented Jan 20, 2025

Hi All!

I have several databases in my project and one of them is external. I know, it's bad practice but that's how it is. And I need tests for it and pytest-django creates tables from models perfectly, but problems arise with custom types in postgres. I need to insert the creation of types in the interval between the creation of the database and the creation of tables. I found this solution and I don't like it. Is there another way?

@pytest.fixture(scope="session", autouse=True)
def _create_custom_types():
    from django.db.backends.base.schema import BaseDatabaseSchemaEditor

    def create_model(self, model):
        if model == User:
            self.execute("CREATE EXTENSION hstore;")
        orig_create_model(self, model)

    orig_create_model = BaseDatabaseSchemaEditor.create_model
    with patch.object(BaseDatabaseSchemaEditor, "create_model", new=create_model):
        yield
@bluetech
Copy link
Member

Are you not able to put it in e.g. an initial migration?

@MyGodIsHe
Copy link
Author

Are you not able to put it in e.g. an initial migration?

I can't use migrations in an easy way. This database is read-only in production.

I could probably make special migrations for tests and include them in the project if there is an IS_TESTING flag in settings.py. Maybe it's even simpler than I wrote above.

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

2 participants