ALTER TABLE module_prefix_snapshot ADD COLUMN prefixes_json JSONB NOT NULL DEFAULT '[]';

UPDATE module_prefix_snapshot mps
SET prefixes_json = COALESCE((
    SELECT jsonb_agg(
        jsonb_build_object(
            'prefix', psr.prefix::text,
            'community_id', psr.community_id,
            'source', psr.source
        ) ORDER BY psr.ord
    )
    FROM module_prefix_snapshot_row psr
    WHERE psr.tenant_id = mps.tenant_id AND psr.module_id = mps.module_id
), '[]'::jsonb);

DROP TABLE IF EXISTS module_prefix_snapshot_row;
