agendas: fix pg function set_ignore_reason on desk deletion (#66394)
This commit is contained in:
parent
6efe2dbaa2
commit
0e000fa430
|
@ -1,17 +1,4 @@
|
|||
import os
|
||||
|
||||
from django.db import migrations, transaction
|
||||
from django.db.utils import InternalError, OperationalError, ProgrammingError
|
||||
|
||||
with open(
|
||||
os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)),
|
||||
'..',
|
||||
'sql',
|
||||
'event_triggers_for_tstzrange_constraint.sql',
|
||||
)
|
||||
) as sql_file:
|
||||
sql_forwards = sql_file.read()
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
@ -20,26 +7,4 @@ class Migration(migrations.Migration):
|
|||
('agendas', '0127_remove_pricing_models'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunSQL(sql=sql_forwards, reverse_sql=migrations.RunSQL.noop),
|
||||
]
|
||||
|
||||
def _check_db(self, project_state, schema_editor):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
try:
|
||||
# will fail if extension does not exist and can not be created
|
||||
schema_editor.execute('CREATE EXTENSION IF NOT EXISTS btree_Gist SCHEMA public')
|
||||
except (OperationalError, ProgrammingError):
|
||||
# if no extension, do not create trigger
|
||||
return project_state
|
||||
except InternalError:
|
||||
return project_state
|
||||
|
||||
def apply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().apply(project_state, schema_editor, *args, **kwargs)
|
||||
|
||||
def unapply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().unapply(project_state, schema_editor, *args, **kwargs)
|
||||
operations = []
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
import os
|
||||
|
||||
from django.db import migrations, transaction
|
||||
from django.db.utils import InternalError, OperationalError, ProgrammingError
|
||||
|
||||
with open(
|
||||
os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)),
|
||||
'..',
|
||||
'sql',
|
||||
'event_triggers_for_tstzrange_constraint.sql',
|
||||
)
|
||||
) as sql_file:
|
||||
sql_triggers = sql_file.read()
|
||||
|
||||
sql_forwards = (
|
||||
"""
|
||||
-- Add technical columns
|
||||
ALTER TABLE agendas_event ADD COLUMN IF NOT EXISTS _end_datetime TIMESTAMP WITH TIME ZONE;
|
||||
ALTER TABLE agendas_event ADD COLUMN IF NOT EXISTS _ignore_reason CHARACTER VARYING(20);
|
||||
|
||||
%s
|
||||
|
||||
-- Init legacy
|
||||
UPDATE agendas_event SET _ignore_reason = 'history';
|
||||
UPDATE
|
||||
agendas_event
|
||||
SET
|
||||
_end_datetime = agendas_event.start_datetime + (agendas_meetingtype.duration ||' minutes')::interval
|
||||
FROM
|
||||
agendas_meetingtype
|
||||
WHERE
|
||||
agendas_meetingtype.id = agendas_event.meeting_type_id
|
||||
AND _end_datetime IS NULL;
|
||||
"""
|
||||
% sql_triggers
|
||||
)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('agendas', '0128_event_date_range_constraint'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunSQL(sql=sql_forwards, reverse_sql=migrations.RunSQL.noop),
|
||||
]
|
||||
|
||||
def _check_db(self, project_state, schema_editor):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
try:
|
||||
# will fail if extension does not exist and can not be created
|
||||
schema_editor.execute('CREATE EXTENSION IF NOT EXISTS btree_Gist SCHEMA public')
|
||||
except (OperationalError, ProgrammingError):
|
||||
# if no extension, do not create columns and triggers
|
||||
return project_state
|
||||
except InternalError:
|
||||
return project_state
|
||||
|
||||
def apply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().apply(project_state, schema_editor, *args, **kwargs)
|
||||
|
||||
def unapply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().unapply(project_state, schema_editor, *args, **kwargs)
|
|
@ -0,0 +1,42 @@
|
|||
from django.db import migrations, transaction
|
||||
from django.db.utils import InternalError, OperationalError, ProgrammingError
|
||||
|
||||
sql_forwards = """
|
||||
ALTER TABLE agendas_event
|
||||
DROP CONSTRAINT IF EXISTS tstzrange_constraint;
|
||||
ALTER TABLE agendas_event
|
||||
ADD CONSTRAINT tstzrange_constraint
|
||||
EXCLUDE USING GIST(desk_id WITH =, tstzrange(start_datetime, _end_datetime) WITH &&)
|
||||
WHERE (_ignore_reason IS NULL AND _end_datetime IS NOT NULL and desk_id IS NOT NULL);
|
||||
"""
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('agendas', '0129_event_date_range_constraint'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunSQL(sql=sql_forwards, reverse_sql=migrations.RunSQL.noop),
|
||||
]
|
||||
|
||||
def _check_db(self, project_state, schema_editor):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
try:
|
||||
# will fail if extension does not exist and can not be created
|
||||
schema_editor.execute('CREATE EXTENSION IF NOT EXISTS btree_Gist SCHEMA public')
|
||||
except (OperationalError, ProgrammingError):
|
||||
# if no extension, columns and triggers does not exist and constraint can not be added
|
||||
return project_state
|
||||
except InternalError:
|
||||
return project_state
|
||||
|
||||
def apply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().apply(project_state, schema_editor, *args, **kwargs)
|
||||
|
||||
def unapply(self, project_state, schema_editor, *args, **kwargs):
|
||||
result = self._check_db(project_state, schema_editor)
|
||||
return result or super().unapply(project_state, schema_editor, *args, **kwargs)
|
Loading…
Reference in New Issue