Skip to content

Commit

Permalink
Move using parameter for PostgresSchema to individual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios committed Apr 10, 2023
1 parent 57d95b1 commit 4d727a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
18 changes: 9 additions & 9 deletions psqlextra/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ class PostgresSchema:
NAME_MAX_LENGTH = 63

name: str
using: str

default: "PostgresSchema"

def __init__(self, name: str, *, using: str = DEFAULT_DB_ALIAS) -> None:
def __init__(self, name: str) -> None:
self.name = name
self.using = using

@classmethod
def create(
Expand Down Expand Up @@ -51,7 +49,7 @@ def create(
with connections[using].schema_editor() as schema_editor:
schema_editor.create_schema(name)

return cls(name, using=using)
return cls(name)

@classmethod
def create_time_based(
Expand Down Expand Up @@ -116,7 +114,7 @@ def delete_and_create(
"""

with transaction.atomic(using=using):
cls(name, using=using).delete(cascade=cascade)
cls(name).delete(cascade=cascade, using=using)
return cls.create(name, using=using)

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

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

with connections[self.using].schema_editor() as schema_editor:
with connections[using].schema_editor() as schema_editor:
schema_editor.delete_schema(self.name, cascade=cascade)

@classmethod
Expand Down Expand Up @@ -207,8 +207,8 @@ def postgres_temporary_schema(
yield schema
except Exception as e:
if delete_on_throw:
schema.delete(cascade=cascade)
schema.delete(cascade=cascade, using=using)

raise e

schema.delete(cascade=cascade)
schema.delete(cascade=cascade, using=using)
9 changes: 1 addition & 8 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@

import freezegun
import pytest

from django.core.exceptions import SuspiciousOperation, ValidationError
from django.db import (
DEFAULT_DB_ALIAS,
InternalError,
ProgrammingError,
connection,
)
from django.db import InternalError, ProgrammingError, connection
from psycopg2 import errorcodes

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

assert _does_schema_exist(schema.name)

Expand Down

0 comments on commit 4d727a0

Please sign in to comment.