Skip to content

Commit 4d727a0

Browse files
committed
Move using parameter for PostgresSchema to individual methods
1 parent 57d95b1 commit 4d727a0

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

psqlextra/schema.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ class PostgresSchema:
1616
NAME_MAX_LENGTH = 63
1717

1818
name: str
19-
using: str
2019

2120
default: "PostgresSchema"
2221

23-
def __init__(self, name: str, *, using: str = DEFAULT_DB_ALIAS) -> None:
22+
def __init__(self, name: str) -> None:
2423
self.name = name
25-
self.using = using
2624

2725
@classmethod
2826
def create(
@@ -51,7 +49,7 @@ def create(
5149
with connections[using].schema_editor() as schema_editor:
5250
schema_editor.create_schema(name)
5351

54-
return cls(name, using=using)
52+
return cls(name)
5553

5654
@classmethod
5755
def create_time_based(
@@ -116,7 +114,7 @@ def delete_and_create(
116114
"""
117115

118116
with transaction.atomic(using=using):
119-
cls(name, using=using).delete(cascade=cascade)
117+
cls(name).delete(cascade=cascade, using=using)
120118
return cls.create(name, using=using)
121119

122120
@classmethod
@@ -137,7 +135,9 @@ def exists(cls, name: str, *, using: str = DEFAULT_DB_ALIAS) -> bool:
137135
with connection.cursor() as cursor:
138136
return name in connection.introspection.get_schema_list(cursor)
139137

140-
def delete(self, *, cascade: bool = False) -> None:
138+
def delete(
139+
self, *, cascade: bool = False, using: str = DEFAULT_DB_ALIAS
140+
) -> None:
141141
"""Deletes the schema and optionally deletes the contents of the schema
142142
and anything that references it.
143143
@@ -156,7 +156,7 @@ def delete(self, *, cascade: bool = False) -> None:
156156
"Pretty sure you are about to make a mistake by trying to drop the 'public' schema. I have stopped you. Thank me later."
157157
)
158158

159-
with connections[self.using].schema_editor() as schema_editor:
159+
with connections[using].schema_editor() as schema_editor:
160160
schema_editor.delete_schema(self.name, cascade=cascade)
161161

162162
@classmethod
@@ -207,8 +207,8 @@ def postgres_temporary_schema(
207207
yield schema
208208
except Exception as e:
209209
if delete_on_throw:
210-
schema.delete(cascade=cascade)
210+
schema.delete(cascade=cascade, using=using)
211211

212212
raise e
213213

214-
schema.delete(cascade=cascade)
214+
schema.delete(cascade=cascade, using=using)

tests/test_schema.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
21
import freezegun
32
import pytest
43

54
from django.core.exceptions import SuspiciousOperation, ValidationError
6-
from django.db import (
7-
DEFAULT_DB_ALIAS,
8-
InternalError,
9-
ProgrammingError,
10-
connection,
11-
)
5+
from django.db import InternalError, ProgrammingError, connection
126
from psycopg2 import errorcodes
137

148
from psqlextra.error import extract_postgres_error
@@ -23,7 +17,6 @@ def _does_schema_exist(name: str) -> bool:
2317
def test_postgres_schema_create():
2418
schema = PostgresSchema.create("myschema")
2519
assert schema.name == "myschema"
26-
assert schema.using == DEFAULT_DB_ALIAS
2720

2821
assert _does_schema_exist(schema.name)
2922

0 commit comments

Comments
 (0)