Opened 8 years ago

Closed 8 years ago

#27170 closed Cleanup/optimization (fixed)

Make database backend __init__() methods friendlier to subclassing

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The __init__() method for most or all of the database backends looks something like the following:

def __init__(self, *args, **kwargs):
    super(DatabaseWrapper, self).__init__(*args, **kwargs)

    self.features = DatabaseFeatures(self)
    self.ops = DatabaseOperations(self)
    self.client = DatabaseClient(self)
    self.creation = DatabaseCreation(self)
    self.introspection = DatabaseIntrospection(self)
    self.validation = DatabaseValidation(self)

(Here is a link to this code for the MySQL backend.)

Notice that each of the database classes is "hard-coded."

Since much of the behavior of the backends is defined by these classes (and so can be modified by changing these classes), it would be nice if it were easier to change what class is used. I believe this could be done simply by making each of these classes a class attribute. This pattern is used to good effect, for example, throughout the test framework. You can see this being done in the definition of DiscoverRunner, for example, here:

class DiscoverRunner(object):

    test_suite = unittest.TestSuite
    parallel_test_suite = ParallelTestSuite
    test_runner = unittest.TextTestRunner
    test_loader = unittest.defaultTestLoader
    reorder_by = (TestCase, SimpleTestCase)

Change History (6)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 by Chris Jerdonek, 8 years ago

Owner: changed from nobody to Chris Jerdonek
Status: newassigned

comment:3 by Chris Jerdonek, 8 years ago

I started working on this in this PR.

comment:4 by Chris Jerdonek, 8 years ago

Has patch: set

comment:5 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 7ca3b391:

Fixed #27170 -- Added DatabaseWrapper class attributes to ease subclassing.

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