1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
import os
from typing import Set, Tuple
import django
import pytest
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import GinIndex
from django.db import connection, models, transaction
from django.db.models import Q
from psqlextra.backend.schema import PostgresSchemaEditor
from . import db_introspection
from .fake_model import delete_fake_model, get_fake_model
django_32_skip_reason = "Django < 3.2 can't support cloning models because it has hard coded references to the public schema"
def _create_schema() -> str:
name = os.urandom(4).hex()
with connection.cursor() as cursor:
cursor.execute(
"DROP SCHEMA IF EXISTS %s CASCADE"
% connection.ops.quote_name(name),
tuple(),
)
cursor.execute(
"CREATE SCHEMA %s" % connection.ops.quote_name(name), tuple()
)
return name
@transaction.atomic
def _assert_cloned_table_is_same(
source_table_fqn: Tuple[str, str],
target_table_fqn: Tuple[str, str],
excluding_constraints_and_indexes: bool = False,
):
source_schema_name, source_table_name = source_table_fqn
target_schema_name, target_table_name = target_table_fqn
source_columns = db_introspection.get_columns(
source_table_name, schema_name=source_schema_name
)
target_columns = db_introspection.get_columns(
target_table_name, schema_name=target_schema_name
)
assert source_columns == target_columns
source_relations = db_introspection.get_relations(
source_table_name, schema_name=source_schema_name
)
target_relations = db_introspection.get_relations(
target_table_name, schema_name=target_schema_name
)
if excluding_constraints_and_indexes:
assert target_relations == {}
else:
assert source_relations == target_relations
source_constraints = db_introspection.get_constraints(
source_table_name, schema_name=source_schema_name
)
target_constraints = db_introspection.get_constraints(
target_table_name, schema_name=target_schema_name
)
if excluding_constraints_and_indexes:
assert target_constraints == {}
else:
assert source_constraints == target_constraints
source_sequences = db_introspection.get_sequences(
source_table_name, schema_name=source_schema_name
)
target_sequences = db_introspection.get_sequences(
target_table_name, schema_name=target_schema_name
)
assert source_sequences == target_sequences
source_storage_settings = db_introspection.get_storage_settings(
source_table_name,
schema_name=source_schema_name,
)
target_storage_settings = db_introspection.get_storage_settings(
target_table_name, schema_name=target_schema_name
)
assert source_storage_settings == target_storage_settings
def _list_lock_modes_in_schema(schema_name: str) -> Set[str]:
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT
l.mode
FROM pg_locks l
INNER JOIN pg_class t ON t.oid = l.relation
INNER JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE
t.relnamespace >= 2200
AND n.nspname = %s
ORDER BY n.nspname, t.relname, l.mode
""",
(schema_name,),
)
return {lock_mode for lock_mode, in cursor.fetchall()}
def _clone_model_into_schema(model):
schema_name = _create_schema()
with PostgresSchemaEditor(connection) as schema_editor:
schema_editor.clone_model_structure_to_schema(
model, schema_name=schema_name
)
schema_editor.clone_model_constraints_and_indexes_to_schema(
model, schema_name=schema_name
)
schema_editor.clone_model_foreign_keys_to_schema(
model, schema_name=schema_name
)
return schema_name
@pytest.fixture
def fake_model_fk_target_1():
model = get_fake_model(
{
"name": models.TextField(),
},
)
yield model
delete_fake_model(model)
@pytest.fixture
def fake_model_fk_target_2():
model = get_fake_model(
{
"name": models.TextField(),
},
)
yield model
delete_fake_model(model)
@pytest.fixture
def fake_model(fake_model_fk_target_1, fake_model_fk_target_2):
model = get_fake_model(
{
"first_name": models.TextField(null=True),
"last_name": models.TextField(),
"age": models.PositiveIntegerField(),
"height": models.FloatField(),
"nicknames": ArrayField(base_field=models.TextField()),
"blob": models.JSONField(),
"family": models.ForeignKey(
fake_model_fk_target_1, on_delete=models.CASCADE
),
"alternative_family": models.ForeignKey(
fake_model_fk_target_2, null=True, on_delete=models.SET_NULL
),
},
meta_options={
"indexes": [
models.Index(fields=["age", "height"]),
models.Index(fields=["age"], name="age_index"),
GinIndex(fields=["nicknames"], name="nickname_index"),
],
"constraints": [
models.UniqueConstraint(
fields=["first_name", "last_name"],
name="first_last_name_uniq",
),
models.CheckConstraint(
check=Q(age__gt=0, height__gt=0), name="age_height_check"
),
],
"unique_together": (
"first_name",
"nicknames",
),
"index_together": (
"blob",
"age",
),
},
)
yield model
delete_fake_model(model)
@pytest.mark.skipif(
django.VERSION < (3, 2),
reason=django_32_skip_reason,
)
@pytest.mark.django_db(transaction=True)
def test_schema_editor_clone_model_to_schema(
fake_model, fake_model_fk_target_1, fake_model_fk_target_2
):
"""Tests that cloning a model into a separate schema without obtaining
AccessExclusiveLock on the source table works as expected."""
schema_editor = PostgresSchemaEditor(connection)
with schema_editor:
schema_editor.alter_table_storage_setting(
fake_model._meta.db_table, "autovacuum_enabled", "false"
)
table_name = fake_model._meta.db_table
source_schema_name = "public"
target_schema_name = _create_schema()
with schema_editor:
schema_editor.clone_model_structure_to_schema(
fake_model, schema_name=target_schema_name
)
assert _list_lock_modes_in_schema(source_schema_name) == {
"AccessShareLock"
}
_assert_cloned_table_is_same(
(source_schema_name, table_name),
(target_schema_name, table_name),
excluding_constraints_and_indexes=True,
)
with schema_editor:
schema_editor.clone_model_constraints_and_indexes_to_schema(
fake_model, schema_name=target_schema_name
)
assert _list_lock_modes_in_schema(source_schema_name) == {
"AccessShareLock",
"ShareRowExclusiveLock",
}
_assert_cloned_table_is_same(
(source_schema_name, table_name),
(target_schema_name, table_name),
)
with schema_editor:
schema_editor.clone_model_foreign_keys_to_schema(
fake_model, schema_name=target_schema_name
)
assert _list_lock_modes_in_schema(source_schema_name) == {
"AccessShareLock",
"RowShareLock",
}
_assert_cloned_table_is_same(
(source_schema_name, table_name),
(target_schema_name, table_name),
)
@pytest.mark.skipif(
django.VERSION < (3, 2),
reason=django_32_skip_reason,
)
def test_schema_editor_clone_model_to_schema_custom_constraint_names(
fake_model, fake_model_fk_target_1
):
"""Tests that even if constraints were given custom names, the cloned table
has those same custom names."""
table_name = fake_model._meta.db_table
source_schema_name = "public"
constraints = db_introspection.get_constraints(table_name)
primary_key_constraint = next(
(
name
for name, constraint in constraints.items()
if constraint["primary_key"]
),
None,
)
foreign_key_constraint = next(
(
name
for name, constraint in constraints.items()
if constraint["foreign_key"]
== (fake_model_fk_target_1._meta.db_table, "id")
),
None,
)
check_constraint = next(
(
name
for name, constraint in constraints.items()
if constraint["check"] and constraint["columns"] == ["age"]
),
None,
)
with connection.cursor() as cursor:
cursor.execute(
f"ALTER TABLE {table_name} RENAME CONSTRAINT {primary_key_constraint} TO custompkname"
)
cursor.execute(
f"ALTER TABLE {table_name} RENAME CONSTRAINT {foreign_key_constraint} TO customfkname"
)
cursor.execute(
f"ALTER TABLE {table_name} RENAME CONSTRAINT {check_constraint} TO customcheckname"
)
target_schema_name = _clone_model_into_schema(fake_model)
_assert_cloned_table_is_same(
(source_schema_name, table_name),
(target_schema_name, table_name),
)
|