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

Support select * for postgres parser #423

Merged
merged 1 commit into from
Jul 20, 2023

Conversation

hokaccha
Copy link
Collaborator

This PR resolves the error in #397. However, there is a problem as follows.

# schema.sql
CREATE TABLE public.test_table (
  "col1" numeric,
  "col2" numeric
);
CREATE VIEW public.test_view AS SELECT t.* FROM test_table t;
$ go run ./cmd/psqldef -h localhost -U postgres sandbox < schema.sql
-- Apply --
CREATE TABLE public.test_table (
  "col1" numeric,
  "col2" numeric
);
CREATE VIEW public.test_view AS SELECT t.* FROM test_table t;
# Run the same command again
$ go run ./cmd/psqldef -h localhost -U postgres sandbox < schema.sql
-- Apply --
CREATE OR REPLACE VIEW "public"."test_view" AS select t.* from test_table as t;

No matter how many times psqldef is executed, CREATE OR REPLACE VIEW is executed. It seems that the cause is that when select * is specified, the query is saved as a query with expanded columns.

$ psql -h localhost -U postgres sandbox -c "SELECT definition FROM pg_views WHERE viewname = 'test_view'"
      definition
-----------------------
  SELECT t.col1,      +
     t.col2           +
    FROM test_table t;
(1 row)

Therefore, it becomes as follows:

  • currentDDL: SELECT t.col1, t.col2
  • desiredDDL: SELECT t.*

As a result, the comparison fails. This seems to be the same not only for postgres but also for mysql. I have confirmed that the same phenomenon occurs with mysqldef.

To solve this problem, we need a process to expand * when parsing desiredDDL. This is not impossible, but it seems to take time. Another option is to have sqldef output an error that it does not support * and exit until this problem is solved.

@k0kubun Which do you think is better?

@k0kubun
Copy link
Collaborator

k0kubun commented Jul 20, 2023

To solve this problem, we need a process to expand * when parsing desiredDDL. This is not impossible, but it seems to take time. Another option is to have sqldef output an error that it does not support * and exit until this problem is solved.

For the short term, it makes sense to give up comparison and assume nothing has changed when one side has select *. Eventually, we should compare them at an AST level, normalizing * using the table schema in the previous DDLs. But let's take a workaround for now.

And, thanks for fixing the parser. This change itself is innocent, so let me just merge this.

@k0kubun k0kubun merged commit 81cbc53 into sqldef:master Jul 20, 2023
@hokaccha hokaccha deleted the fix-view-select branch July 21, 2023 00:26
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

Successfully merging this pull request may close these issues.

2 participants