How do I fix 'ERROR: function “xt.js_init” does not exist' when upgrading to xTuple ERP v4.x?

This is a problem with certain combinations of xTuple ERP upgrade packages and plv8. The solution is to run the following SQL script before upgrading:

-- xtuple/lib/orm/source/create_xt_schema.sql
do $$
declare
count integer;
query text;
begin /* Only create the schema if it hasn't been created already */
perform *
from information_schema.schemata
where schema_name = 'xt';

get diagnostics count = row_count;

if (count > 0) then
return;
end if;

query = 'create schema xt;';
execute query;

query = 'grant all on schema xt to group xtrole;';
execute query;
end; $$ language 'plpgsql';

-- xtuple/lib/orm/source/create_plv8.sql
-- dummy function avoids forward reference bug with some plv8 versions
CREATE OR REPLACE FUNCTION xt.js_init(debug BOOLEAN DEFAULT false, initialize BOOLEAN DEFAULT false) RETURNS VOID AS $$ BEGIN RETURN; END; $$ LANGUAGE plpgsql;

CREATE EXTENSION IF NOT EXISTS plv8;