Skip to content
Open
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
5 changes: 5 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ The `gen` mapping supports the following keys:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `omit_unused_structs`:
- If `true`, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to `false`.
- `omit_catalog_schema`:
- If `true`, sqlc won't generate structs for tables and enums in the `pg_catalog` and `information_schema` catalog schemas. Set to `false` to generate models from these schemas. Defaults to `true`.
- `output_batch_file_name`:
- Customize the name of the batch file. Defaults to `batch.go`.
- `output_db_file_name`:
Expand Down Expand Up @@ -396,6 +398,7 @@ packages:
build_tags: "some_tag"
json_tags_case_style: "camel"
omit_unused_structs: false
omit_catalog_schema: true
output_batch_file_name: "batch.go"
output_db_file_name: "db.go"
output_models_file_name: "models.go"
Expand Down Expand Up @@ -458,6 +461,8 @@ Each mapping in the `packages` collection has the following keys:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `omit_unused_structs`:
- If `true`, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to `false`.
- `omit_catalog_schema`:
- If `true`, sqlc won't generate structs for tables and enums in the `pg_catalog` and `information_schema` catalog schemas. Set to `false` to generate models from these schemas. Defaults to `true`.
- `output_batch_file_name`:
- Customize the name of the batch file. Defaults to `batch.go`.
- `output_db_file_name`:
Expand Down
11 changes: 11 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Options struct {
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
OmitCatalogSchema *bool `json:"omit_catalog_schema,omitempty" yaml:"omit_catalog_schema"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`

Expand Down Expand Up @@ -185,6 +186,16 @@ func (o *Options) ModelsEmitEnabled() bool {
return *o.OutputModelsEmit
}

// OmitCatalogSchemaEnabled reports whether the pg_catalog and
// information_schema catalog schemas should be excluded when generating
// models. Defaults to true when the option is unset.
func (o *Options) OmitCatalogSchemaEnabled() bool {
if o.OmitCatalogSchema == nil {
return true
}
return *o.OmitCatalogSchema
}

// ModelsImportAlias is the fixed Go import alias used for the models
// package in query files. Using a constant alias keeps the type qualifier
// consistent regardless of how the user names the actual package.
Expand Down
4 changes: 2 additions & 2 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
var enums []Enum
for _, schema := range req.Catalog.Schemas {
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
if options.OmitCatalogSchemaEnabled() && (schema.Name == "pg_catalog" || schema.Name == "information_schema") {
continue
}
for _, enum := range schema.Enums {
Expand Down Expand Up @@ -63,7 +63,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
var structs []Struct
for _, schema := range req.Catalog.Schemas {
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
if options.OmitCatalogSchemaEnabled() && (schema.Name == "pg_catalog" || schema.Name == "information_schema") {
continue
}
for _, table := range schema.Tables {
Expand Down
65 changes: 65 additions & 0 deletions internal/codegen/golang/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,75 @@ package golang
import (
"testing"

"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/metadata"
"github.com/sqlc-dev/sqlc/internal/plugin"
)

func boolPtr(b bool) *bool { return &b }

func catalogSchemaRequest() *plugin.GenerateRequest {
col := func(name, typ string) *plugin.Column {
return &plugin.Column{Name: name, Type: &plugin.Identifier{Name: typ}}
}
table := func(schema, name string, cols ...*plugin.Column) *plugin.Schema {
return &plugin.Schema{
Name: schema,
Tables: []*plugin.Table{
{Rel: &plugin.Identifier{Schema: schema, Name: name}, Columns: cols},
},
}
}
return &plugin.GenerateRequest{
Settings: &plugin.Settings{Engine: "postgresql"},
Catalog: &plugin.Catalog{
DefaultSchema: "public",
Schemas: []*plugin.Schema{
table("pg_catalog", "pg_class", col("oid", "oid")),
table("information_schema", "tables", col("table_name", "text")),
table("public", "users", col("id", "int4")),
},
},
}
}

func TestBuildStructs_OmitCatalogSchema(t *testing.T) {
req := catalogSchemaRequest()

t.Run("unset (default) skips pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{})
for _, s := range structs {
if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") {
t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name)
}
}
})

t.Run("omit=true skips pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(true)})
for _, s := range structs {
if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") {
t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name)
}
}
})

t.Run("omit=false includes pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(false)})
schemas := make(map[string]bool)
for _, s := range structs {
if s.Table != nil {
schemas[s.Table.Schema] = true
}
}
for _, want := range []string{"pg_catalog", "information_schema", "public"} {
if !schemas[want] {
t.Errorf("expected structs for schema %q, got none", want)
}
}
})
}

func TestPutOutColumns_ForZeroColumns(t *testing.T) {
tests := []struct {
cmd string
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type v1PackageSettings struct {
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
OmitCatalogSchema *bool `json:"omit_catalog_schema,omitempty" yaml:"omit_catalog_schema"`
Rules []string `json:"rules" yaml:"rules"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
}
Expand Down Expand Up @@ -177,6 +178,7 @@ func (c *V1GenerateSettings) Translate() Config {
QueryParameterLimit: pkg.QueryParameterLimit,
OmitSqlcVersion: pkg.OmitSqlcVersion,
OmitUnusedStructs: pkg.OmitUnusedStructs,
OmitCatalogSchema: pkg.OmitCatalogSchema,
BuildTags: pkg.BuildTags,
},
},
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_one.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@
"omit_unused_structs": {
"type": "boolean"
},
"omit_catalog_schema": {
"type": "boolean"
},
"rules": {
"type": "array",
"items": {
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@
},
"omit_unused_structs": {
"type": "boolean"
},
"omit_catalog_schema": {
"type": "boolean"
}
},
"json": {
Expand Down

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,5 @@
-- name: GetTimezones :many
SELECT * FROM pg_catalog.pg_timezone_names;

-- name: GetTables :many
SELECT table_name::text FROM information_schema.tables;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"engine": "postgresql",
"schema": "schema.sql",
"queries": "query.sql",
"omit_catalog_schema": false,
"omit_unused_structs": true
}
]
}
Loading