"""jsonb_agg_uniq_by_first_item Revision ID: 4eb679fb4fd0 Revises: ad2ed41fef92 Create Date: 2019-05-22 07:26:13.962095 """ # revision identifiers, used by Alembic. revision = '4eb679fb4fd0' down_revision = 'ad2ed41fef92' branch_labels = None depends_on = None from alembic import op import sqlalchemy as sa def upgrade(): op.execute(''' create function f_array_append_uniq_first_item(jsonb, jsonb) returns jsonb immutable language sql as $$ SELECT CASE WHEN $1 is null then jsonb_build_array($2) WHEN (($1->-1)->0) <> ($2->0) THEN $1 || jsonb_build_array($2) ELSE $1 END $$; create aggregate jsonb_agg_uniq_by_first_item(jsonb) ( sfunc = f_array_append_uniq_first_item, stype = jsonb ); ''') def downgrade(): op.execute(''' drop aggregate jsonb_agg_uniq_by_first_item(jsonb); drop function f_array_append_uniq_first_item(jsonb, jsonb); ''')