"""jsonb_agg_uniq Revision ID: a24e03d2a7ec Revises: 6f872738dab7 Create Date: 2018-06-14 20:48:06.950616 """ # revision identifiers, used by Alembic. revision = 'a24e03d2a7ec' down_revision = '6f872738dab7' branch_labels = None depends_on = None from alembic import op import sqlalchemy as sa def upgrade(): op.execute(""" CREATE OR REPLACE FUNCTION f_array_append_uniq (jsonb, jsonb) RETURNS jsonb AS $func$ SELECT CASE WHEN $1 is null then jsonb_build_array($2) WHEN ($1->-1) <> $2 THEN $1 || jsonb_build_array($2) ELSE $1 END $func$ LANGUAGE sql IMMUTABLE; """) op.execute(""" CREATE AGGREGATE jsonb_agg_uniq(jsonb) ( SFUNC = f_array_append_uniq , STYPE = jsonb ); """) def downgrade(): op.execute('''DROP AGGREGATE jsonb_agg_uniq(anyelement)''') op.execute('''drop FUNCTION f_array_append_uniq (jsonb, anyelement)''')