Skip to content

Commit 63ba2c6

Browse files
committed
feat(mysql): allow generating models from information_schema tables
1 parent ecec179 commit 63ba2c6

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

internal/codegen/golang/opts/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Options struct {
4949
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
5050
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
5151
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
52+
OmitCatalogSchema *bool `json:"omit_catalog_schema,omitempty" yaml:"omit_catalog_schema"`
5253
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
5354
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`
5455

@@ -137,6 +138,11 @@ func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
137138
*options.Initialisms = []string{"id"}
138139
}
139140

141+
if options.OmitCatalogSchema == nil {
142+
options.OmitCatalogSchema = new(bool)
143+
*options.OmitCatalogSchema = true
144+
}
145+
140146
options.InitialismsMap = map[string]struct{}{}
141147
for _, initial := range *options.Initialisms {
142148
options.InitialismsMap[initial] = struct{}{}

internal/codegen/golang/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
1717
var enums []Enum
1818
for _, schema := range req.Catalog.Schemas {
19-
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
19+
if options.OmitCatalogSchema != nil && *options.OmitCatalogSchema && (schema.Name == "pg_catalog" || schema.Name == "information_schema") {
2020
continue
2121
}
2222
for _, enum := range schema.Enums {
@@ -63,7 +63,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
6363
func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
6464
var structs []Struct
6565
for _, schema := range req.Catalog.Schemas {
66-
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
66+
if options.OmitCatalogSchema != nil && *options.OmitCatalogSchema && (schema.Name == "pg_catalog" || schema.Name == "information_schema") {
6767
continue
6868
}
6969
for _, table := range schema.Tables {

internal/codegen/golang/result_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,66 @@ package golang
33
import (
44
"testing"
55

6+
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
67
"github.com/sqlc-dev/sqlc/internal/metadata"
78
"github.com/sqlc-dev/sqlc/internal/plugin"
89
)
910

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("omit=true skips pg_catalog and information_schema", func(t *testing.T) {
42+
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(true)})
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=false includes pg_catalog and information_schema", func(t *testing.T) {
51+
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(false)})
52+
schemas := make(map[string]bool)
53+
for _, s := range structs {
54+
if s.Table != nil {
55+
schemas[s.Table.Schema] = true
56+
}
57+
}
58+
for _, want := range []string{"pg_catalog", "information_schema", "public"} {
59+
if !schemas[want] {
60+
t.Errorf("expected structs for schema %q, got none", want)
61+
}
62+
}
63+
})
64+
}
65+
1066
func TestPutOutColumns_ForZeroColumns(t *testing.T) {
1167
tests := []struct {
1268
cmd string

0 commit comments

Comments
 (0)