rubenv / sql-migrate

SQL schema migration tool for Go.
MIT License
3.19k stars 273 forks source link

the following migration fails #114

Closed miladz68 closed 6 years ago

miladz68 commented 6 years ago

when running the following sql command I get an error

CREATE OR REPLACE FUNCTION create_constraint_if_not_exists (t_name text, c_name text, constraint_sql text)
  RETURNS void
AS
$BODY$
  begin
    -- Look for our constraint
    if not exists (select constraint_name
                   from information_schema.constraint_column_usage
                   where table_name = t_name  and constraint_name = c_name) then
        execute 'ALTER TABLE ' || t_name || ' ADD CONSTRAINT ' || c_name || ' ' || constraint_sql;
    end if;
end;
$BODY$
LANGUAGE plpgsql VOLATILE;

the error is

sql-migrate up 1
Migration failed: pq: unterminated dollar-quoted string at or near "$BODY$
  begin
    -- Look for our constraint
    if not exists (select constraint_name
                   from information_schema.constraint_column_usage
                   where table_name = t_name  and constraint_name = c_name) then
        execute 'ALTER TABLE ' || t_name || ' ADD CONSTRAINT ' || c_name || ' ' || constraint_sql; 
end if; 
end;
" handling 20180126120940-PREFEDINED_FUNCTIONS.sql

This is a parsing error when there are too many semicologns in the function body. Chaning the sql command to the following fixes the error.

CREATE OR REPLACE FUNCTION create_constraint_if_not_exists (t_name text, c_name text, constraint_sql text)
  RETURNS void
AS
$BODY$
  begin
    -- Look for our constraint
    if not exists (select constraint_name
                   from information_schema.constraint_column_usage
                   where table_name = t_name  and constraint_name = c_name) then
        execute 'ALTER TABLE ' || t_name || ' ADD CONSTRAINT ' || c_name || ' ' || constraint_sql;end if;end;$BODY$LANGUAGE plpgsql VOLATILE;
rubenv commented 6 years ago

Check StatementBegin and StatementEnd: https://github.com/rubenv/sql-migrate#writing-migrations