Skip to main content

CreateExtension

This class represents a reversible database operation for creating PostgreSQL extensions. It manages the installation and removal of extensions while ensuring that type handlers and geometry adapters are correctly registered or cleared. The operation includes checks to verify extension existence and respects database routing rules.

Attributes

AttributeTypeDescription
reversibleboolean = TrueIndicates that the operation can be undone by running the database_backwards method.
category[OperationCategory](../../../db/migrations/operations/base/operationcategory.md?sid=django_db_migrations_operations_base_operationcategory) = OperationCategory.ADDITIONClassification of the operation used to group migration actions, set to ADDITION for extension creation.

Constructor

Signature

def CreateExtension(
name: string,
hints: dict = None
) - > null

Parameters

NameTypeDescription
namestringThe name of the PostgreSQL extension to be created.
hintsdict = NoneA dictionary of hints used by the database router to determine if the migration should be allowed.

Methods


state_forwards()

@classmethod
def state_forwards(
app_label: string,
state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)
) - > null

Updates the project state to reflect the addition of the extension; currently performs no operations as extensions do not affect the internal model state.

Parameters

NameTypeDescription
app_labelstringThe label of the application the migration belongs to.
state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The current state of the project models to be mutated.

Returns

TypeDescription
nullNothing is returned.

database_forwards()

@classmethod
def database_forwards(
app_label: string,
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor),
from_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate),
to_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)
) - > null

Installs the extension into the PostgreSQL database if it does not already exist and refreshes type handlers to ensure the new types are recognized by the connection.

Parameters

NameTypeDescription
app_labelstringThe label of the application the migration belongs to.
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The database abstraction layer used to execute DDL statements.
from_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project before the operation is applied.
to_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project after the operation is applied.

Returns

TypeDescription
nullNothing is returned.

database_backwards()

@classmethod
def database_backwards(
app_label: string,
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor),
from_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate),
to_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)
) - > null

Removes the extension from the PostgreSQL database and clears cached OIDs to prevent stale type references.

Parameters

NameTypeDescription
app_labelstringThe label of the application the migration belongs to.
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The database abstraction layer used to execute DDL statements.
from_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project before the operation is reversed.
to_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project after the operation is reversed.

Returns

TypeDescription
nullNothing is returned.

extension_exists()

@classmethod
def extension_exists(
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor),
extension: string
) - > boolean

Checks the PostgreSQL system catalogs to determine if a specific extension is already installed in the database.

Parameters

NameTypeDescription
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The database abstraction layer providing the active connection.
extensionstringThe name of the PostgreSQL extension to check for.

Returns

TypeDescription
booleanTrue if the extension is found in pg_extension, False otherwise.

describe()

@classmethod
def describe() - > string

Provides a human-readable summary of the operation for console output and logging.

Returns

TypeDescription
stringA string describing the creation of the specific extension.

migration_name_fragment()

@classmethod
def migration_name_fragment() - > string

Generates a string fragment used to automatically name the migration file containing this operation.

Returns

TypeDescription
stringA slugified string containing the extension name.