"""enable_performance_metrics_fields Revision ID: 9429dd875ef5 Revises: e71c2b434237 Create Date: 2021-03-23 12:23:07.635247 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. from sqlalchemy.dialects.postgresql import insert revision = '9429dd875ef5' down_revision = '710bb10e6e8d' branch_labels = None depends_on = None def get_metrics_field_table(bind): meta = sa.MetaData(bind=bind) meta.reflect() return meta.tables["MetricsField"] def get_metrics_objective_table(bind): meta = sa.MetaData(bind=bind) meta.reflect() return meta.tables["ObjectiveMetricsField"] def get_format_id(bind, name): return bind.execute(f''' SELECT id FROM "MetricsFieldFormat" WHERE name = '{name}' ''').fetchone()[0] def upgrade(): bind = op.get_bind() metrics_field_table = get_metrics_field_table(bind) changes = { 60: { "title": "Comments", "total_name": 'Total Comments', "description": "Sum of comments, defined by each platform." } } create = { 68:{ "name": "Custom Conversions", "type_id": 2, "format_id": 6, "title": "Custom Conversions", "total_name": "Total Conversions", "description": "Sum of custom conversions across all platforms." }, 69:{ "name": "Cost Per Custom Conversion", "type_id": 2, "format_id": 1, "title": "Custom Conversion", "subtitle": "Cost Per", "total_name": "Total Cost Per Custom Conversion", "description": "Cost per custom conversion across all platforms." }, 70:{ "name": "Page Likes", "type_id": 2, "format_id": 6, "title": "Page Likes", "total_name": "Total Page Likes", "description": "Sum of Page Likes (Facebook Only)." }, 71:{ "name": "Unique Conversions", "type_id": 2, "format_id": 6, "title": "Conversion", "subtitle": "Unique", "total_name": "Total Unique Conversions", "description": "Sum of unique conversions across all platforms. Value may differ from other ad platforms because Decibel calculates this value by summing unique people each day instead of filtering for unique people across the entire date range." }, } for field_id, values in changes.items(): op.execute( metrics_field_table.update().where(metrics_field_table.c.id == field_id).values(**values) ) for field_id, values in create.items(): op.execute( insert(metrics_field_table).values( id=field_id, name=values.get('name', None), type_id=values.get('type_id', None), format_id=values.get('format_id', None), title=values.get('title', None), subtitle=values.get('subtitle', None), total_name=values.get('total_name', None), description=values.get('description', None), ).on_conflict_do_nothing() )