Skip to content

Latest commit

 

History

History
81 lines (52 loc) · 4.39 KB

README.md

File metadata and controls

81 lines (52 loc) · 4.39 KB

django_types

Provides a base implementation for custom database types in Django 1.11+.
It tracks data types within the migration state and migration apps so that fields relying on these can more easily hook into auto-migration, and have stateful field instances appropriate for prior states of the project.

CustomTypeField

The CustomTypeField requires only the type_name, which should uniquely identify it regardless of app_labels, and be used as the database type name where possible. This is what should be use with migrations to track the state of the type and act accordingly.

The type_def is an implementation class for the type represented, and should be set by subclasses upon instantiation, and will be retrieved via the app state during migrations, so should never be included in deconstruct() or other stateful methods.

Type implementation classes that contain a Meta can override the database type and app_label that will be used. Where not provided, the default is to generate a type name based upon the app the class is contained within, and the name of the implementation class.

contribute_to_class() provides the hooks to regain previous states during migration and make the field actually operable in that state, based on the type_name.

clone() will copy the type_def where it exists, so that stateful fields remain stateful through model/field cloning, especially during migration.

Django patches

Changes to Django that are necessary are applied by patch_types() This should be called by any app that requires custom types.

Custom types

Patches to make custom types work.

ProjectState (django.db.migrations.state.ProjectState)
Modified to store custom types that may be relevant to the database, and ensure that apps objects based on this state also have them available.

  • __init__( * ): Also initialises db_types dict.
  • apps: Will pass through db_types when creating StateApps.
  • add_type(self, type_name, type_def, app_label): Add a new type to the local db_types and if apps exists to there too.
  • remove_type(self, type_name): Remove a type from db_types and if apps exists from there too.
  • clone(self): Also clones db_types and apps.db_types_.

StateApps (django.db.migrations.state.StateApps)

  • __init__( * ): Accepts db_types keyword argument.

BaseDatabaseSchemaEditor (django.db.backends.base.schema.BaseDatabaseSchemaEditor)

  • column_sql_paramatized(self, col_type): Convenience method to turn any valid col_type into an sql, params pair.
  • _alter_column_type_sql(): Changed to support parametised DBTypes.
  • _column_sql(): Changed to support parametised DBTypes.

Field dependencies

Patches to make field based dependencies work.

Field (django.db.models.Field)

  • has_dependencies: New flag that defaults to False.
  • dependencies: cached_property of what is given by get_dependencies().
  • get_dependencies(): Skeleton that returns an empty list, so inherited classes can always use super().

RelatedField (django.db.models.fields.related.RelatedField)

  • has_dependencies: Flag set to True.
  • get_dependencies(): will return the list of dependencies this field has, performing the same function as MigrationAutodetector._get_dependencies_for_foreign_key(field) would for any existing foreign key types.

MigrationAutodetector (django.db.migrations.autodetector.MigrationAutodetector)

  • generate_created_models(): Changed to use field based dependencies logic.
  • _generate_added_field(): Changed to use field based dependencies logic.
  • _generate_altered_foo_together(): Changed to use field based dependencies logic.

Utility Classes

Various classes necessary for exposed functionality.

DBType

An overloaded str class that allows for the use of parametised values in places where Django doesn't otherwise support them, such as in the db_type. (ie, this is necessary for enum usage on mysql)

dependency_tuple

A namedtuple that represents the required fields for migration dependencies.

find_fields(state, db_type, field_type)

Convenience function to scan an entire state for fields of the specified field_type and db_type, returning a list of field_info tuples.

field_info

A namedtuple that represents all the information about a field that should be necessary to generate migration operations for it.