tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
808 stars 91 forks source link

Unable to execute plpgsql functions as a part of a migration (unterminated dollar-quoted string at or near "$$) #234

Open ShipraShalini opened 2 years ago

ShipraShalini commented 2 years ago

I am trying to split a table into two and there's a need to transform the data before moving it. I am using a few plpgsql functions to achieve that. But aerich upgrade fails with the following error:

...

File "/Users/shipra/code/.virtualenvs/venv/lib/python3.9/site-packages/asyncpg/connection.py", line 318, in execute return await self._protocol.query(query, timeout) File "asyncpg/protocol/protocol.pyx", line 338, in query asyncpg.exceptions.PostgresSyntaxError: unterminated dollar-quoted string at or near "$$ -- Convert camelCase to snakecase. BEGIN RETURN trim(both '' from lower(regexp_replace(keyname, '([A-Z])','\1', 'g')))"

During handling of the above exception, another exception occurred:

...

File "/Users/shipra/code/.virtualenvs/venv/lib/python3.9/site-packages/tortoise/backends/asyncpg/client.py", line 38, in translateexceptions raise OperationalError(exc) tortoise.exceptions.OperationalError: unterminated dollar-quoted string at or near "$$ -- Convert camelCase to snakecase. BEGIN RETURN trim(both '' from lower(regexp_replace(keyname, '([A-Z])','\1', 'g')))"

The function mentioned in the traceback:

CREATE OR REPLACE FUNCTION camel_to_snake(key_name TEXT)  RETURNS TEXT AS $$
-- Convert camelCase to snake_case.
BEGIN
    RETURN trim(both '_' from lower(regexp_replace(key_name, '([A-Z])','_\1', 'g')));
END;
$$ LANGUAGE plpgsql;
alexf-bond commented 1 year ago

Any updates on this? Currently having to apply our database level functions manually out of band from aerich upgrade..