Skip to content

fix(postgresql): Support renaming and dropping materialized views #3728

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: postgresql alter materialized view is not registered to statements
  • Loading branch information
Pulung Ragil committed Nov 28, 2024
commit 02a80e563cd97f40c9fb51d26c472982b143bd3e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/3371
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: GetAuthorMv :one
SELECT * FROM authors_mv
WHERE id = $1 LIMIT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE MATERIALIZED VIEW authors_mv AS (
SELECT * FROM authors
);

CREATE MATERIALIZED VIEW authors_mv_new AS (
SELECT * FROM authors
);

ALTER MATERIALIZED VIEW authors_mv RENAME TO authors_mv_old;
ALTER MATERIALIZED VIEW authors_mv_new RENAME TO authors_mv;

DROP MATERIALIZED VIEW authors_mv_old;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v4"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: GetAuthorMv :one
SELECT * FROM authors_mv
WHERE id = $1 LIMIT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE MATERIALIZED VIEW authors_mv AS (
SELECT * FROM authors
);

CREATE MATERIALIZED VIEW authors_mv_new AS (
SELECT * FROM authors
);

ALTER MATERIALIZED VIEW authors_mv RENAME TO authors_mv_old;
ALTER MATERIALIZED VIEW authors_mv_new RENAME TO authors_mv;

DROP MATERIALIZED VIEW authors_mv_old;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
2 changes: 1 addition & 1 deletion internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func translate(node *nodes.Node) (ast.Node, error) {
MissingOk: n.MissingOk,
}, nil

case nodes.ObjectType_OBJECT_TABLE:
case nodes.ObjectType_OBJECT_TABLE, nodes.ObjectType_OBJECT_MATVIEW, nodes.ObjectType_OBJECT_VIEW:
rel := parseRelationFromRangeVar(n.Relation)
return &ast.RenameTableStmt{
Table: rel.TableName(),
Expand Down
Loading