如何使用模块 node-pg-migrate 为 postgresql 中的函数编写迁移脚本
How to write migration script for a function in postgresql using a module node-pg-migrate
我正在尝试使用 node-pg-migrate 模块编写一个迁移脚本,但无法成功,因为我遇到了以下错误。
Error: Can't get migration files: //dbMigration/migrations/1534615332847_new_test_function.js:11
},'DECLARE
^^^^^^^^^
SyntaxError: Invalid or unexpected token
我的迁移脚本如下:
exports.shorthands = undefined
exports.up = (pgm) => {
pgm.createFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],
{
return: 'json',
language: 'plpgsql',
replace: true
},'DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
')
}
exports.down = (pgm) => {
pgm.dropFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],{
ifExists : true,
cascade : false
})
}
& 下面是我实际的 postgresql 函数:
CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
RETURNS json
LANGUAGE plpgsql
AS $function$
DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
$function$
谁能给我一个用 node-pg-migrate 模块编写的函数的正确示例。我能够编写 create table 和其他脚本,但它给添加函数迁移带来了问题。提前致谢。
找到解决此问题的解决方法。 pgm 还提供了我们可以直接将原始 sql 查询添加到迁移文件中的方法。
找到下面的代码,如下所示:
exports.shorthands = undefined
exports.up = (pgm) => {
pgm.sql(`CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
RETURNS json
LANGUAGE plpgsql
AS $function$
DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
$function$`)
}
exports.down = (pgm) => {
pgm.sql(`DROP FUNCTION IF EXISTS public.get_customer_name(soldtocode text)`)
}
我正在尝试使用 node-pg-migrate 模块编写一个迁移脚本,但无法成功,因为我遇到了以下错误。
Error: Can't get migration files: //dbMigration/migrations/1534615332847_new_test_function.js:11
},'DECLARE
^^^^^^^^^
SyntaxError: Invalid or unexpected token
我的迁移脚本如下:
exports.shorthands = undefined
exports.up = (pgm) => {
pgm.createFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],
{
return: 'json',
language: 'plpgsql',
replace: true
},'DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
')
}
exports.down = (pgm) => {
pgm.dropFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],{
ifExists : true,
cascade : false
})
}
& 下面是我实际的 postgresql 函数:
CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
RETURNS json
LANGUAGE plpgsql
AS $function$
DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
$function$
谁能给我一个用 node-pg-migrate 模块编写的函数的正确示例。我能够编写 create table 和其他脚本,但它给添加函数迁移带来了问题。提前致谢。
找到解决此问题的解决方法。 pgm 还提供了我们可以直接将原始 sql 查询添加到迁移文件中的方法。
找到下面的代码,如下所示:
exports.shorthands = undefined
exports.up = (pgm) => {
pgm.sql(`CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
RETURNS json
LANGUAGE plpgsql
AS $function$
DECLARE
customer_list json;
final_response json;
begin
if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
else
SELECT array_to_json(array_agg(row_to_json(t))) FROM
(select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
from public.company_info ci
join public.company_oem_mapping com on ci.id = com.company_info_id
join mst_oem mo on mo.id = com.oem_id
and ci.supplier_location_id = soldtocode) t
INTO customer_list;
final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
end if;
RETURN final_response;
end
$function$`)
}
exports.down = (pgm) => {
pgm.sql(`DROP FUNCTION IF EXISTS public.get_customer_name(soldtocode text)`)
}