Skip to content

Commit

Permalink
Take separator into account when computing maximum schema name prefix…
Browse files Browse the repository at this point in the history
… length
  • Loading branch information
Photonios committed Apr 12, 2023
1 parent 4daceb6 commit 784e7a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
20 changes: 12 additions & 8 deletions psqlextra/schema.py
Expand Up @@ -71,9 +71,9 @@ def create_time_based(
"""

suffix = timezone.now().strftime("%Y%m%d%H%m%S")
cls._verify_generated_name_length(prefix, suffix)
name = cls._create_generated_name(prefix, suffix)

return cls.create(f"{prefix}_{suffix}", using=using)
return cls.create(name, using=using)

@classmethod
def create_random(
Expand All @@ -91,9 +91,9 @@ def create_random(
"""

suffix = os.urandom(4).hex()
cls._verify_generated_name_length(prefix, suffix)
name = cls._create_generated_name(prefix, suffix)

return cls.create(f"{prefix}_{suffix}", using=using)
return cls.create(name, using=using)

@classmethod
def delete_and_create(
Expand Down Expand Up @@ -160,14 +160,18 @@ def delete(
schema_editor.delete_schema(self.name, cascade=cascade)

@classmethod
def _verify_generated_name_length(cls, prefix: str, suffix: str) -> None:
max_prefix_length = cls.NAME_MAX_LENGTH - len(suffix)
def _create_generated_name(cls, prefix: str, suffix: str) -> str:
separator = "_"
generated_name = f"{prefix}{separator}{suffix}"
max_prefix_length = cls.NAME_MAX_LENGTH - len(suffix) - len(separator)

if len(prefix) > max_prefix_length:
if len(generated_name) > cls.NAME_MAX_LENGTH:
raise ValidationError(
f"Schema prefix '{prefix}' is longer than {max_prefix_length} characters. Together with the generated suffix of {len(suffix)} characters, the name would exceed Postgres's limit of {cls.NAME_MAX_LENGTH} characters."
f"Schema prefix '{prefix}' is longer than {max_prefix_length} characters. Together with the separator and generated suffix of {len(suffix)} characters, the name would exceed Postgres's limit of {cls.NAME_MAX_LENGTH} characters."
)

return generated_name


PostgresSchema.default = PostgresSchema("public")

Expand Down
8 changes: 4 additions & 4 deletions tests/test_schema.py
Expand Up @@ -57,9 +57,9 @@ def test_postgres_schema_create_time_based():
def test_postgres_schema_create_time_based_long_prefix():
with pytest.raises(ValidationError) as exc_info:
with freezegun.freeze_time("2023-04-07 13:37:23.4"):
PostgresSchema.create_time_based("a" * 100)
PostgresSchema.create_time_based("a" * 49)

assert "is longer than 49 characters" in str(exc_info.value)
assert "is longer than 48 characters" in str(exc_info.value)


def test_postgres_schema_create_random():
Expand All @@ -74,9 +74,9 @@ def test_postgres_schema_create_random():

def test_postgres_schema_create_random_long_prefix():
with pytest.raises(ValidationError) as exc_info:
PostgresSchema.create_random("a" * 100)
PostgresSchema.create_random("a" * 55)

assert "is longer than 55 characters" in str(exc_info.value)
assert "is longer than 54 characters" in str(exc_info.value)


def test_postgres_schema_delete_and_create():
Expand Down

0 comments on commit 784e7a1

Please sign in to comment.