diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/issue.md b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/issue.md new file mode 100644 index 0000000000..c51bd7f6d4 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/issue.md @@ -0,0 +1 @@ +https://github.com/sqlc-dev/sqlc/issues/1519 diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/db.go b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/db.go new file mode 100644 index 0000000000..8a010ccc48 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/models.go new file mode 100644 index 0000000000..5a759a4401 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/models.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type ComputedTablesSomething struct { + ID pgtype.Int4 + EventType string + CreatedAt pgtype.Timestamptz +} + +type Event struct { + ID pgtype.Int4 + EventType string + CreatedAt pgtype.Timestamptz +} diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/query.sql.go new file mode 100644 index 0000000000..3d0ab335c3 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/go/query.sql.go @@ -0,0 +1,36 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 +// source: query.sql + +package querytest + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const someQuery = `-- name: SomeQuery :many +select id from "computed_tables"."something" +` + +func (q *Queries) SomeQuery(ctx context.Context) ([]pgtype.Int4, error) { + rows, err := q.db.Query(ctx, someQuery) + if err != nil { + return nil, err + } + defer rows.Close() + var items []pgtype.Int4 + for rows.Next() { + var id pgtype.Int4 + if err := rows.Scan(&id); err != nil { + return nil, err + } + items = append(items, id) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/query.sql b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/query.sql new file mode 100644 index 0000000000..24dafea7ed --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/query.sql @@ -0,0 +1,2 @@ +-- name: SomeQuery :many +select id from "computed_tables"."something"; diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/schema.sql b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/schema.sql new file mode 100644 index 0000000000..2b788e0976 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/schema.sql @@ -0,0 +1,13 @@ +create table events ( + id int, + event_type text not null, + created_at timestamptz +); + +CREATE MATERIALIZED VIEW something AS +select * from events +where event_type = 'sale' +order by created_at desc; + +create schema computed_tables; +alter materialized view something set schema computed_tables; diff --git a/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/sqlc.yaml b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/sqlc.yaml new file mode 100644 index 0000000000..5dc63e3f91 --- /dev/null +++ b/internal/endtoend/testdata/ddl_alter_materialized_views_set_schema/postgresql/pgx/sqlc.yaml @@ -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" diff --git a/internal/engine/postgresql/parse.go b/internal/engine/postgresql/parse.go index c1ac83381c..49ec981484 100644 --- a/internal/engine/postgresql/parse.go +++ b/internal/engine/postgresql/parse.go @@ -236,7 +236,7 @@ func translate(node *nodes.Node) (ast.Node, error) { n := inner.AlterObjectSchemaStmt switch n.ObjectType { - case nodes.ObjectType_OBJECT_TABLE: + case nodes.ObjectType_OBJECT_TABLE, nodes.ObjectType_OBJECT_VIEW, nodes.ObjectType_OBJECT_MATVIEW: rel := parseRelationFromRangeVar(n.Relation) return &ast.AlterTableSetSchemaStmt{ Table: rel.TableName(),