Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importing posgresql function make import fail #553

Open
pierresouchay opened this issue Apr 16, 2024 · 2 comments
Open

importing posgresql function make import fail #553

pierresouchay opened this issue Apr 16, 2024 · 2 comments

Comments

@pierresouchay
Copy link
Contributor

When parsing any function with BEGIN/END, parsing will fail:

Small example:

CREATE FUNCTION public.my_func_max_42() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
        BEGIN
          IF (SELECT SUM(mycounter::int) FROM mytable) > NEW.mycounter::int > 42
          THEN Raise Exception 'Sum must be lower than 42';
          END IF;
          RETURN NEW;
        END;
        $$;

=> This make the whole process fail, likely due to ; chars within the function

@pierresouchay
Copy link
Contributor Author

For now, my only workaround is to remove lots of stuffs from the SQL schema using the following script:

#!/usr/bin/env python3

import os
import re
import sys


"""
An ugly script to pre-process sql and to have an output suitable for sql2dbml
"""

FUNCTION_PATTERN = re.compile("^(CREATE|ALTER) FUNCTION")
VIEW_PATTERN = re.compile("^CREATE.* VIEW")
AS_PATTERN = re.compile("[\\s]*AS ([^\\s]+)")
JSON_ACCESSOR = re.compile(" ->> '([^']+)'")

searching_for = None
current_function = ""


def print_ignored_line(val: str) -> None:
    if "DEBUG" in os.environ:
        print(f"[IGNORED] {val}", file=sys.stderr)


with open(sys.argv[1], "r") as f:
    for line in f.readlines():
        if searching_for:
            current_function += line
            if line.strip().endswith(searching_for):
                searching_for = None
                print_ignored_line(current_function)
                current_function = ""
            elif AS_PATTERN.match(line):
                symbol = AS_PATTERN.match(line).group(1)
                searching_for = f"{symbol};"
        elif FUNCTION_PATTERN.match(line) or VIEW_PATTERN.match(line):
            current_function = line
            if not line.strip().endswith(";"):
                searching_for = ";"
            else:
                print_ignored_line(line)
        else:
            # sql2dbml does not like postgresql JSON Accessors
            # replace ->> with .
            print(JSON_ACCESSOR.sub(".\\1", line), end="")

@simonschmidt
Copy link

I hit the same issue, since I produce sql with pg_dump to begin with I was able to use --disable-dollar-quoting

pg_dump \
  --schema-only \
  --disable-dollar-quoting \
  "$DB_URL" |
  sql2dbml --postgres /dev/stdin

which partially makes the conversion works - as in it doesn't error but the functions aren't part of the output.

I'm not sure if they're expected to be, this is the first time I'm trying this tool out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants