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

Foreign constraints' name should be quoted as others constraints' name #18

Open
fljdin opened this issue Jul 15, 2022 · 4 comments
Open

Comments

@fljdin
Copy link
Contributor

fljdin commented Jul 15, 2022

Hi,

In deparser, foreign constraints' name should be quoted as others constraints' name.

SELECT deparser.expression(ast.alter_table_stmt(
        v_relation := ast_helpers.range_var(
                v_schemaname := 'public',
                v_relname := 't1'
        ),
        v_relkind := 'OBJECT_TABLE',
        v_cmds := to_jsonb(ARRAY[
                ast.alter_table_cmd(
                        v_subtype := 'AT_AddConstraint',
                        v_def := ast.constraint(
                                v_conname := 'my-foreign-key',
                                v_contype := 'CONSTR_FOREIGN',
                                v_pktable := ast_helpers.range_var(
                                        v_schemaname := 'public',
                                        v_relname := 'ref_t1'
                                ),
                                v_pk_attrs := to_jsonb(ARRAY[
                                        ast.string('id')
                                ]),
                                v_fk_attrs := to_jsonb(ARRAY[
                                        ast.string('id')
                                ])
                        )
                )
        ])
));
                                              expression                                              
------------------------------------------------------------------------------------------------------
 ALTER TABLE public.t1 ADD CONSTRAINT "my-foreign-key" FOREIGN KEY (id) REFERENCES public.ref_t1 (id)
(1 row)

However, libpg_query seems to ignore this rule (static void deparseConstraint) and tests are not successful in kitchen-sink.test.js on sqlfromparser step.

expect(cleanLines(sql)).toMatchSnapshot();
expect(cleanLines(sqlfromparser)).toMatchSnapshot(); /* <-- HERE */
Summary of all failing tests
 FAIL  test/__tests__/kitchen-sink.test.js (8.552 s)

  ● kitchen sink › alter
    expect(received).toMatchSnapshot()
    Snapshot name: `kitchen sink alter 2`

    - Snapshot  - 1
    + Received  + 1
    - ALTER TABLE scha.foo ADD CONSTRAINT "MyConstraintKey" FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
    + ALTER TABLE scha.foo ADD CONSTRAINT MyConstraintKey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );

Should I open a new issue in pgsql-parser and/or libpg_query?

Regards

@pyramation
Copy link
Collaborator

nice find! I think this may be an issue for pgsql-parser since we're not yet using the deparser from libpg_query

@pyramation
Copy link
Collaborator

So if I understand it correctly, you were able to fix it with #19, however, the tests are broken?

@fljdin
Copy link
Contributor Author

fljdin commented Jul 17, 2022

That’s right, all tests are included but pgsql-parser need to be updated.

@pyramation
Copy link
Collaborator

So a lot of this repository was originally built using pgsql-parser as the test case, so we should definitely get pgsql-parser updated.

The structure of the repos is very similar! The pgsql-parser is likely easier to edit than this one. Let's make an issue there if it doesn't support the names yet.

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