CREATE OR REPLACE FUNCTION microsharding_migration_before(
  dst_schema text = 'public' -- text, because it may not yet exist
) RETURNS SETOF text
LANGUAGE plpgsql
SET search_path FROM CURRENT
AS $$
BEGIN
  RETURN QUERY SELECT *
    FROM microsharding_debug_views_drop(dst_schema);
  -- It's important to also drop the foreign schemas, in this sole transaction.
  -- This is because PG holds a lot of locks when dropping the foreign schemas
  -- AND THEN creating them from scratch in microsharding_migration_after(): it
  -- throws an error "out of shared memory ... You might need to increase
  -- max_locks_per_transaction".
  PERFORM microsharding_debug_fdw_drop('debug_fdw');
END;
$$;

COMMENT ON FUNCTION microsharding_migration_before(text)
  IS 'Can be run before all other migration DDL statements to remove '
     'cross-shard debug views and foreign schemas from the database.';
