Skip to content

chore(migrations): Removed vendored code from SafePostgresDatabaseSchemaEditor. #90457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: danf/migrations-remove-allow-run-sql
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions src/sentry/db/postgres/schema.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
from contextlib import contextmanager

from django.db.backends.ddl_references import Statement
from django.db.backends.postgresql.schema import (
DatabaseSchemaEditor as PostgresDatabaseSchemaEditor,
)
from django.db.models import Field
from django.db.models.base import ModelBase
from django_zero_downtime_migrations.backends.postgres.schema import (
DUMMY_SQL,
DatabaseSchemaEditorMixin,
MultiStatementSQL,
PGLock,
Unsafe,
UnsafeOperationException,
)
Expand Down Expand Up @@ -103,73 +97,6 @@ def remove_field(self, model, field, is_safe=False):
)
super(DatabaseSchemaEditorMixin, self).remove_field(model, field)

def execute(self, sql, params=()):
if sql is DUMMY_SQL:
return
statements = []
if isinstance(sql, MultiStatementSQL):
statements.extend(sql)
elif isinstance(sql, Statement) and isinstance(sql.template, MultiStatementSQL):
statements.extend(Statement(s, **sql.parts) for s in sql.template)
else:
statements.append(sql)
for statement in statements:
idempotent_condition = None
if isinstance(statement, PGLock):
use_timeouts = statement.use_timeouts
disable_statement_timeout = statement.disable_statement_timeout
idempotent_condition = statement.idempotent_condition
statement = statement.sql
elif isinstance(statement, Statement) and isinstance(statement.template, PGLock):
use_timeouts = statement.template.use_timeouts
disable_statement_timeout = statement.template.disable_statement_timeout
if statement.template.idempotent_condition is not None:
idempotent_condition = statement.template.idempotent_condition % statement.parts
statement = Statement(statement.template.sql, **statement.parts)
else:
use_timeouts = False
disable_statement_timeout = False

if not self._skip_applied(idempotent_condition):
if use_timeouts:
with self._set_operation_timeout(self.STATEMENT_TIMEOUT, self.LOCK_TIMEOUT):
PostgresDatabaseSchemaEditor.execute(self, statement, params)
elif disable_statement_timeout and self.FLEXIBLE_STATEMENT_TIMEOUT:
with self._set_operation_timeout(self.ZERO_TIMEOUT):
PostgresDatabaseSchemaEditor.execute(self, statement, params)
else:
PostgresDatabaseSchemaEditor.execute(self, statement, params)

@contextmanager
def _set_operation_timeout(self, statement_timeout=None, lock_timeout=None):
if self.collect_sql:
previous_statement_timeout = self.ZERO_TIMEOUT
previous_lock_timeout = self.ZERO_TIMEOUT
else:
with self.connection.cursor() as cursor:
cursor.execute(self._sql_get_statement_timeout)
(previous_statement_timeout,) = cursor.fetchone()
cursor.execute(self._sql_get_lock_timeout)
(previous_lock_timeout,) = cursor.fetchone()
if statement_timeout is not None:
PostgresDatabaseSchemaEditor.execute(
self, self._sql_set_statement_timeout % {"statement_timeout": statement_timeout}
)
if lock_timeout is not None:
PostgresDatabaseSchemaEditor.execute(
self, self._sql_set_lock_timeout % {"lock_timeout": lock_timeout}
)
yield
if statement_timeout is not None:
PostgresDatabaseSchemaEditor.execute(
self,
self._sql_set_statement_timeout % {"statement_timeout": previous_statement_timeout},
)
if lock_timeout is not None:
PostgresDatabaseSchemaEditor.execute(
self, self._sql_set_lock_timeout % {"lock_timeout": previous_lock_timeout}
)


class DatabaseSchemaEditorProxy:
"""
Expand Down
Loading