Opened 9 years ago

Closed 9 years ago

#24918 closed New feature (wontfix)

Using SchemaEditor to create materialized views in PostgreSQL

Reported by: Andreas Madsack Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: postgresql, schemaeditor
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We use the schemaeditor to create not only tables in the database, but also materialized views.
This is working for us on Django 1.7, but we use the somehow private API of Django migrations.

Our solution:

  • Subclassing the Postgresql-DatabaseWrapper to change the schema_editor to our own:
from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper as PGDatabaseWrapper

class DatabaseWrapper(PGDatabaseWrapper):
    def __init__(self, *args, **kwargs):
        self.SchemaEditorClass = OurDatabaseSchemaEditor
        super(DatabaseWrapper, self).__init__(*args, **kwargs)
  • in OurDatabaseSchemaEditor we overwrite every action method we need changed.

For example:

    def create_model(self, model):
        if check_if_materialized(model):
            from materialized.helpers import create_model
            create_model(model)
            return
        super(OurDatabaseSchemaEditor, self).create_model(model)

The same way for delete_model, add_field, alter_field, remove_field, ...

Purpose of this ticket is the discussion about custom changes to schemaeditor methods like we do.
Is this the way to go or should it be done in another way?

I'm in Cardiff at the sprints today and on freenode as mfandreas.

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed
Version: 1.8

I don't think the SchemaEditor methods are private API as they are documented. That said, this is not really the place to discuss "how to" questions. See TicketClosingReasons/UseSupportChannels. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top