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
| Attribute | Type | Description |
|---|---|---|
| reversible | boolean = True | Indicates 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.ADDITION | Classification 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
| Name | Type | Description |
|---|---|---|
| name | string | The name of the PostgreSQL extension to be created. |
| hints | dict = None | A 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
| Name | Type | Description |
|---|---|---|
| app_label | string | The 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
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| app_label | string | The 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
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| app_label | string | The 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
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| schema_editor | [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor) | The database abstraction layer providing the active connection. |
| extension | string | The name of the PostgreSQL extension to check for. |
Returns
| Type | Description |
|---|---|
boolean | True 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
| Type | Description |
|---|---|
string | A 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
| Type | Description |
|---|---|
string | A slugified string containing the extension name. |