| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ecec179 commit e74ee0b
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -179,6 +179,8 @@ The `gen` mapping supports the following keys: | |||
| 179 | 179 | - `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`. | |
| 180 | 180 | - `omit_unused_structs`: | |
| 181 | 181 | - If `true`, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to `false`. | |
| 182 | + - `omit_catalog_schema`: | ||
| 183 | + - 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`. | ||
| 182 | 184 | - `output_batch_file_name`: | |
| 183 | 185 | - Customize the name of the batch file. Defaults to `batch.go`. | |
| 184 | 186 | - `output_db_file_name`: | |
@@ -396,6 +398,7 @@ packages: | |||
| 396 | 398 | build_tags: "some_tag" | |
| 397 | 399 | json_tags_case_style: "camel" | |
| 398 | 400 | omit_unused_structs: false | |
| 401 | + omit_catalog_schema: true | ||
| 399 | 402 | output_batch_file_name: "batch.go" | |
| 400 | 403 | output_db_file_name: "db.go" | |
| 401 | 404 | output_models_file_name: "models.go" | |
@@ -458,6 +461,8 @@ Each mapping in the `packages` collection has the following keys: | |||
| 458 | 461 | - `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`. | |
| 459 | 462 | - `omit_unused_structs`: | |
| 460 | 463 | - If `true`, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to `false`. | |
| 464 | + - `omit_catalog_schema`: | ||
| 465 | + - 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`. | ||
| 461 | 466 | - `output_batch_file_name`: | |
| 462 | 467 | - Customize the name of the batch file. Defaults to `batch.go`. | |
| 463 | 468 | - `output_db_file_name`: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,7 @@ type Options struct { | |||
| 49 | 49 | QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"` | |
| 50 | 50 | OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"` | |
| 51 | 51 | OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"` | |
| 52 | + OmitCatalogSchema *bool `json:"omit_catalog_schema,omitempty" yaml:"omit_catalog_schema"` | ||
| 52 | 53 | BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"` | |
| 53 | 54 | Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"` | |
| 54 | 55 | ||
@@ -185,6 +186,16 @@ func (o *Options) ModelsEmitEnabled() bool { | |||
| 185 | 186 | return *o.OutputModelsEmit | |
| 186 | 187 | } | |
| 187 | 188 | ||
| 189 | + // OmitCatalogSchemaEnabled reports whether the pg_catalog and | ||
| 190 | + // information_schema catalog schemas should be excluded when generating | ||
| 191 | + // models. Defaults to true when the option is unset. | ||
| 192 | + func (o *Options) OmitCatalogSchemaEnabled() bool { | ||
| 193 | + if o.OmitCatalogSchema == nil { | ||
| 194 | + return true | ||
| 195 | + } | ||
| 196 | + return *o.OmitCatalogSchema | ||
| 197 | + } | ||
| 198 | + | ||
| 188 | 199 | // ModelsImportAlias is the fixed Go import alias used for the models | |
| 189 | 200 | // package in query files. Using a constant alias keeps the type qualifier | |
| 190 | 201 | // consistent regardless of how the user names the actual package. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ import ( | |||
| 16 | 16 | func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum { | |
| 17 | 17 | var enums []Enum | |
| 18 | 18 | for _, schema := range req.Catalog.Schemas { | |
| 19 | - if schema.Name == "pg_catalog" || schema.Name == "information_schema" { | ||
| 19 | + if options.OmitCatalogSchemaEnabled() && (schema.Name == "pg_catalog" || schema.Name == "information_schema") { | ||
| 20 | 20 | continue | |
| 21 | 21 | } | |
| 22 | 22 | for _, enum := range schema.Enums { | |
@@ -63,7 +63,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum { | |||
| 63 | 63 | func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct { | |
| 64 | 64 | var structs []Struct | |
| 65 | 65 | for _, schema := range req.Catalog.Schemas { | |
| 66 | - if schema.Name == "pg_catalog" || schema.Name == "information_schema" { | ||
| 66 | + if options.OmitCatalogSchemaEnabled() && (schema.Name == "pg_catalog" || schema.Name == "information_schema") { | ||
| 67 | 67 | continue | |
| 68 | 68 | } | |
| 69 | 69 | for _, table := range schema.Tables { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,10 +3,75 @@ package golang | |||
| 3 | 3 | import ( | |
| 4 | 4 | "testing" | |
| 5 | 5 | ||
| 6 | + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" | ||
| 6 | 7 | "github.com/sqlc-dev/sqlc/internal/metadata" | |
| 7 | 8 | "github.com/sqlc-dev/sqlc/internal/plugin" | |
| 8 | 9 | ) | |
| 9 | 10 | ||
| 11 | + func boolPtr(b bool) *bool { return &b } | ||
| 12 | + | ||
| 13 | + func catalogSchemaRequest() *plugin.GenerateRequest { | ||
| 14 | + col := func(name, typ string) *plugin.Column { | ||
| 15 | + return &plugin.Column{Name: name, Type: &plugin.Identifier{Name: typ}} | ||
| 16 | + } | ||
| 17 | + table := func(schema, name string, cols ...*plugin.Column) *plugin.Schema { | ||
| 18 | + return &plugin.Schema{ | ||
| 19 | + Name: schema, | ||
| 20 | + Tables: []*plugin.Table{ | ||
| 21 | + {Rel: &plugin.Identifier{Schema: schema, Name: name}, Columns: cols}, | ||
| 22 | + }, | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + return &plugin.GenerateRequest{ | ||
| 26 | + Settings: &plugin.Settings{Engine: "postgresql"}, | ||
| 27 | + Catalog: &plugin.Catalog{ | ||
| 28 | + DefaultSchema: "public", | ||
| 29 | + Schemas: []*plugin.Schema{ | ||
| 30 | + table("pg_catalog", "pg_class", col("oid", "oid")), | ||
| 31 | + table("information_schema", "tables", col("table_name", "text")), | ||
| 32 | + table("public", "users", col("id", "int4")), | ||
| 33 | + }, | ||
| 34 | + }, | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + func TestBuildStructs_OmitCatalogSchema(t *testing.T) { | ||
| 39 | + req := catalogSchemaRequest() | ||
| 40 | + | ||
| 41 | + t.Run("unset (default) skips pg_catalog and information_schema", func(t *testing.T) { | ||
| 42 | + structs := buildStructs(req, &opts.Options{}) | ||
| 43 | + for _, s := range structs { | ||
| 44 | + if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") { | ||
| 45 | + t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name) | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + }) | ||
| 49 | + | ||
| 50 | + t.Run("omit=true skips pg_catalog and information_schema", func(t *testing.T) { | ||
| 51 | + structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(true)}) | ||
| 52 | + for _, s := range structs { | ||
| 53 | + if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") { | ||
| 54 | + t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name) | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + }) | ||
| 58 | + | ||
| 59 | + t.Run("omit=false includes pg_catalog and information_schema", func(t *testing.T) { | ||
| 60 | + structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(false)}) | ||
| 61 | + schemas := make(map[string]bool) | ||
| 62 | + for _, s := range structs { | ||
| 63 | + if s.Table != nil { | ||
| 64 | + schemas[s.Table.Schema] = true | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + for _, want := range []string{"pg_catalog", "information_schema", "public"} { | ||
| 68 | + if !schemas[want] { | ||
| 69 | + t.Errorf("expected structs for schema %q, got none", want) | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + }) | ||
| 73 | + } | ||
| 74 | + | ||
| 10 | 75 | func TestPutOutColumns_ForZeroColumns(t *testing.T) { | |
| 11 | 76 | tests := []struct { | |
| 12 | 77 | cmd string | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,7 @@ type v1PackageSettings struct { | |||
| 62 | 62 | QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"` | |
| 63 | 63 | OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"` | |
| 64 | 64 | OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"` | |
| 65 | + OmitCatalogSchema *bool `json:"omit_catalog_schema,omitempty" yaml:"omit_catalog_schema"` | ||
| 65 | 66 | Rules []string `json:"rules" yaml:"rules"` | |
| 66 | 67 | BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"` | |
| 67 | 68 | } | |
@@ -177,6 +178,7 @@ func (c *V1GenerateSettings) Translate() Config { | |||
| 177 | 178 | QueryParameterLimit: pkg.QueryParameterLimit, | |
| 178 | 179 | OmitSqlcVersion: pkg.OmitSqlcVersion, | |
| 179 | 180 | OmitUnusedStructs: pkg.OmitUnusedStructs, | |
| 181 | + OmitCatalogSchema: pkg.OmitCatalogSchema, | ||
| 180 | 182 | BuildTags: pkg.BuildTags, | |
| 181 | 183 | }, | |
| 182 | 184 | }, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -263,6 +263,9 @@ | |||
| 263 | 263 | "omit_unused_structs": { | |
| 264 | 264 | "type": "boolean" | |
| 265 | 265 | }, | |
| 266 | + "omit_catalog_schema": { | ||
| 267 | + "type": "boolean" | ||
| 268 | + }, | ||
| 266 | 269 | "rules": { | |
| 267 | 270 | "type": "array", | |
| 268 | 271 | "items": { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -280,6 +280,9 @@ | |||
| 280 | 280 | }, | |
| 281 | 281 | "omit_unused_structs": { | |
| 282 | 282 | "type": "boolean" | |
| 283 | + }, | ||
| 284 | + "omit_catalog_schema": { | ||
| 285 | + "type": "boolean" | ||
| 283 | 286 | } | |
| 284 | 287 | }, | |
| 285 | 288 | "json": { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments