如何使用模块node-pg-migrate在Postgresql中为函数编写迁移脚本

时间:2018-08-18 18:46:58

标签: node.js postgresql pg dbmigrate

我正试图使用​​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模块编写的函数的正确示例。我能够编写创建表和其他脚本,但是它给添加功能迁移带来了问题。预先感谢。

1 个答案:

答案 0 :(得分:0)

找到了解决此问题的解决方法。 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)`)
}