""" Contains upgrade tasks that are executed when the application is being
upgraded on the server. See :class:`onegov.core.upgrade.upgrade_task`.
"""
from __future__ import annotations
from depot.io.utils import FileIntent
from io import BytesIO
from onegov.core.crypto import random_token
from onegov.core.orm.types import JSON, UUID
from onegov.core.upgrade import upgrade_task
from onegov.core.utils import dictionary_to_binary
from onegov.core.utils import normalize_for_url
from onegov.form import FormDefinitionCollection
from onegov.form import FormFile
from onegov.form import FormSubmission
from sqlalchemy import Column, Integer, Text
from sqlalchemy.engine.reflection import Inspector
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.core.upgrade import UpgradeContext
@upgrade_task('Enable external form submissions')
@upgrade_task('Set payment method for existing forms')
@upgrade_task('Migrate form submission files to onegov.file')
@upgrade_task('Add payment_method to definitions and submissions')
[docs]
def add_payment_method_to_definitions_and_submissions(
context: UpgradeContext
) -> None:
context.add_column_with_defaults(
table='forms',
column=Column('payment_method', Text, nullable=False),
default=lambda form: form.content.get('payment_method', 'manual')
)
context.add_column_with_defaults(
table='submissions',
column=Column('payment_method', Text, nullable=False),
default=lambda submission: (
submission.form
and submission.form.content.get('payment_method', 'manual')
or 'manual'
)
)
for form in context.records_per_table('forms'):
form.content.pop('payment_method', None)
@upgrade_task('Add meta dictionary to submissions')
@upgrade_task('Add group/order to form definitions')
@upgrade_task('Add registration window columns')
[docs]
def add_registration_window_columns(context: UpgradeContext) -> None:
context.operations.add_column(
'submissions',
Column('claimed', Integer, nullable=True)
)
context.operations.add_column(
'submissions',
Column('registration_window_id', UUID, nullable=True)
)
context.add_column_with_defaults(
table='submissions',
column=Column('spots', Integer, nullable=False),
default=0
)
@upgrade_task('Make form polymorphic type non-nullable')
@upgrade_task('Add title to submission windows')
[docs]
def add_title_to_submission_windows(context: UpgradeContext) -> None:
if not context.has_column('submission_windows', 'title'):
context.add_column_with_defaults(
'submission_windows',
Column(
'title',
Text,
nullable=True
),
default=None
)
@upgrade_task('Remove no overlapping submission windows constraint')
[docs]
def remove_no_overlapping_submission_windows_constraint(
context: UpgradeContext
) -> None:
if not context.has_table('submission_windows'):
return
inspector = Inspector(context.operations_connection)
# Check unique constraints
unique_constraints = inspector.get_unique_constraints('submission_windows')
# Check check constraints
check_constraints = inspector.get_check_constraints('submission_windows')
constraint_exists = any(
const['name'] == 'no_overlapping_submission_windows'
for const in unique_constraints + check_constraints
)
if constraint_exists:
context.operations.drop_constraint(
'no_overlapping_submission_windows',
'submission_windows'
)
@upgrade_task('Remove state column form survey submissions')
[docs]
def remove_state_from_survey_submissions(context: UpgradeContext) -> None:
if context.has_table('survey_submissions'):
context.operations.drop_column('survey_submissions', 'state')