Files
notifications-api/migrations/versions/0383_update_default_templates.py
Steven Reilly dc06b411ca Update default templates and add update command (#166)
This commit adds config_files for default data and, using that, creates a new way to update our default templates without needing to hardcode a migration.

---------

Co-authored-by: Ryan Ahearn <ryan.ahearn@gsa.gov>
2023-02-03 10:11:21 -05:00

56 lines
1.6 KiB
Python

"""
Revision ID: 0383_update_default_templates.py
Revises: 0382_remove_old_providers
Create Date: 2023-01-10 11:42:25.633265
"""
import json
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from flask import current_app
revision = '0383_update_default_templates.py'
down_revision = '0382_remove_old_providers'
def upgrade():
update = """
UPDATE {} SET name = '{}', template_type = '{}', content = '{}', subject = '{}'
WHERE id = '{}'
"""
with open(current_app.config['CONFIG_FILES'] + '/templates.json') as f:
data = json.load(f)
for d in data:
for table_name in 'templates', 'templates_history':
op.execute(
update.format(
table_name,
d['name'],
d['type'],
'\n'.join(d['content']),
d.get('subject'),
d['id']
)
)
# op.execute(
# """
# INSERT INTO template_redacted
# (
# template_id,
# redact_personalisation,
# updated_at,
# updated_by_id
# ) VALUES ( '{}', false, current_timestamp, '{}' )
# """.format(d['id'], current_app.config['NOTIFY_USER_ID'])
# )
def downgrade():
# with associated code changes, edits to templates should no longer be made via migration.
# instead, update the fixture and run the flask command to update.
pass