Skip to content

Commit

Permalink
Add support to schema editor for moving tables between schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios committed Apr 7, 2023
1 parent 8bb964b commit 3218a7a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
43 changes: 43 additions & 0 deletions psqlextra/backend/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class PostgresSchemaEditor(SchemaEditor):
sql_alter_table_storage_setting = "ALTER TABLE %s SET (%s = %s)"
sql_reset_table_storage_setting = "ALTER TABLE %s RESET (%s)"

sql_alter_table_schema = "ALTER TABLE %s SET SCHEMA %s"

sql_create_view = "CREATE VIEW %s AS (%s)"
sql_replace_view = "CREATE OR REPLACE VIEW %s AS (%s)"
sql_drop_view = "DROP VIEW IF EXISTS %s"
Expand Down Expand Up @@ -438,6 +440,47 @@ def reset_model_storage_setting(

self.reset_table_storage_setting(model._meta.db_table, name)

def alter_table_schema(self, table_name: str, schema_name: str) -> None:
"""Moves the specified table into the specified schema.
WARNING: Moving models into a different schema than the default
will break querying the model.
Arguments:
table_name:
Name of the table to move into the specified schema.
schema_name:
Name of the schema to move the table to.
"""

self.execute(
self.sql_alter_table_schema
% (self.quote_name(table_name), self.quote_name(schema_name))
)

def alter_model_schema(self, model: Type[Model], schema_name: str) -> None:
"""Moves the specified model's table into the specified schema.
WARNING: Moving models into a different schema than the default
will break querying the model.
Arguments:
model:
Model of which to move the table.
schema_name:
Name of the schema to move the model's table to.
"""

self.execute(
self.sql_alter_table_schema
% (
self.quote_name(model._meta.db_table),
self.quote_name(schema_name),
)
)

def refresh_materialized_view_model(
self, model: Type[Model], concurrently: bool = False
) -> None:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_schema_editor_alter_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from django.db import connection, models

from psqlextra.backend.schema import PostgresSchemaEditor

from .fake_model import get_fake_model


@pytest.fixture
def fake_model():
return get_fake_model(
{
"text": models.TextField(),
}
)


def test_schema_editor_alter_table_schema(fake_model):
obj = fake_model.objects.create(text="hello")

with connection.cursor() as cursor:
cursor.execute("CREATE SCHEMA target")

schema_editor = PostgresSchemaEditor(connection)
schema_editor.alter_table_schema(fake_model._meta.db_table, "target")

with connection.cursor() as cursor:
cursor.execute(f"SELECT * FROM target.{fake_model._meta.db_table}")
assert cursor.fetchall() == [(obj.id, obj.text)]


def test_schema_editor_alter_model_schema(fake_model):
obj = fake_model.objects.create(text="hello")

with connection.cursor() as cursor:
cursor.execute("CREATE SCHEMA target")

schema_editor = PostgresSchemaEditor(connection)
schema_editor.alter_model_schema(fake_model, "target")

with connection.cursor() as cursor:
cursor.execute(f"SELECT * FROM target.{fake_model._meta.db_table}")
assert cursor.fetchall() == [(obj.id, obj.text)]

0 comments on commit 3218a7a

Please sign in to comment.