Skip to content

Commit 6f8fdb4

Browse files
committed
Raise specific validation error for schema name prefix + suffix exceeding limit
1 parent b368877 commit 6f8fdb4

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

psqlextra/schema.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ def create_time_based(
104104
Name of the database connection to use.
105105
"""
106106

107-
name_suffix = timezone.now().strftime("%Y%m%d%H%m%s")
108-
return cls.create(f"{prefix}_{name_suffix}", using=using)
107+
suffix = timezone.now().strftime("%Y%m%d%H%m%s")
108+
cls._verify_generated_name_length(prefix, suffix)
109+
110+
return cls.create(f"{prefix}_{suffix}", using=using)
109111

110112
@classmethod
111113
def create_random(
@@ -122,8 +124,10 @@ def create_random(
122124
Name of the database connection to use.
123125
"""
124126

125-
name_suffix = os.urandom(4).hex()
126-
return cls.create(f"{prefix}_{name_suffix}", using=using)
127+
suffix = os.urandom(4).hex()
128+
cls._verify_generated_name_length(prefix, suffix)
129+
130+
return cls.create(f"{prefix}_{suffix}", using=using)
127131

128132
@classmethod
129133
def delete_and_create(
@@ -201,6 +205,15 @@ def connection(self) -> BaseDatabaseWrapper:
201205

202206
return PostgresSchemaConnectionWrapper(connections[self.using], self)
203207

208+
@classmethod
209+
def _verify_generated_name_length(cls, prefix: str, suffix: str) -> None:
210+
max_prefix_length = cls.NAME_MAX_LENGTH - len(suffix)
211+
212+
if len(prefix) > max_prefix_length:
213+
raise ValidationError(
214+
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."
215+
)
216+
204217

205218
PostgresSchema.default = PostgresSchema("public")
206219

tests/test_schema.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ def test_postgres_schema_does_not_overwrite():
3737

3838

3939
def test_postgres_schema_create_max_name_length():
40-
with pytest.raises(ValidationError):
40+
with pytest.raises(ValidationError) as exc_info:
4141
PostgresSchema.create(
4242
"stringthatislongerhtan63charactersforsureabsolutelysurethisislongerthanthat"
4343
)
4444

45+
assert "is longer than Postgres's limit" in str(exc_info.value)
46+
4547

4648
def test_postgres_schema_create_name_that_requires_escaping():
4749
# 'table' needs escaping because it conflicts with
@@ -60,6 +62,13 @@ def test_postgres_schema_create_time_based():
6062
assert _does_schema_exist(schema.name)
6163

6264

65+
def test_postgres_schema_create_time_based_long_prefix():
66+
with pytest.raises(ValidationError) as exc_info:
67+
PostgresSchema.create_time_based("a" * 100)
68+
69+
assert "is longer than 55 characters" in str(exc_info.value)
70+
71+
6372
def test_postgres_schema_create_random():
6473
schema = PostgresSchema.create_random("myprefix")
6574

@@ -70,6 +79,13 @@ def test_postgres_schema_create_random():
7079
assert _does_schema_exist(schema.name)
7180

7281

82+
def test_postgres_schema_create_random_long_prefix():
83+
with pytest.raises(ValidationError) as exc_info:
84+
PostgresSchema.create_random("a" * 100)
85+
86+
assert "is longer than 55 characters" in str(exc_info.value)
87+
88+
7389
def test_postgres_schema_delete_and_create():
7490
schema = PostgresSchema.create("test")
7591

0 commit comments

Comments
 (0)