Skip to content

Commit b3ba03f

Browse files
ragokaniamralch
andauthored
Add table exclude option (#46)
* add table exclude option * refactor: namespace exclude under options.tables Move the table exclude list from options.exclude to options.tables.exclude so future table-level filters (e.g. include) can be added non-breakingly. --------- Co-authored-by: Svetlin Ralchev <iamralch@users.noreply.github.com>
1 parent f8670c4 commit b3ba03f

6 files changed

Lines changed: 107 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,19 @@ sql:
5959
- plugin: gen-queries
6060
out: "ent/query"
6161
options:
62+
tables:
63+
exclude:
64+
- "audit_logs"
65+
- "auth.sessions"
6266
queries:
6367
- "CopyUsers"
6468
- "GetUserWithPost"
6569
```
6670
71+
Use `options.tables.exclude` to skip generating query files for tables that
72+
should not be managed by sqlc. Entries may be table names (`audit_logs`) or
73+
schema-qualified table names (`auth.sessions`).
74+
6775
### Default queries (always generated)
6876

6977
Primary key CRUD operations, List queries (including FK-index-based list

internal/sqlc/config.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ type Codegen struct {
7171

7272
// CodegenOptions holds plugin-specific options for the gen-queries plugin.
7373
type CodegenOptions struct {
74-
Queries []string `yaml:"queries,omitempty"`
74+
Queries []string `yaml:"queries,omitempty"`
75+
Tables TableOptions `yaml:"tables,omitempty"`
76+
}
77+
78+
// TableOptions holds table-level filtering options for the gen-queries plugin.
79+
type TableOptions struct {
80+
Exclude []string `yaml:"exclude,omitempty"`
7581
}
7682

7783
// GetOptions returns the CodegenOptions for the gen-queries plugin.
@@ -95,3 +101,18 @@ func (s *SQL) GetQueriesSet() map[string]bool {
95101
}
96102
return querySet
97103
}
104+
105+
// GetExcludeSet returns a set of table names to skip during query generation.
106+
// Entries may be unqualified table names or schema-qualified names.
107+
func (s *SQL) GetExcludeSet() map[string]bool {
108+
opts := s.GetOptions()
109+
excludeSet := make(map[string]bool, len(opts.Tables.Exclude))
110+
for _, name := range opts.Tables.Exclude {
111+
excludeSet[name] = true
112+
}
113+
return excludeSet
114+
}
115+
116+
func tableExcluded(excludeSet map[string]bool, schema, table string) bool {
117+
return excludeSet[table] || excludeSet[schema+"."+table]
118+
}

internal/sqlc/config_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var _ = Describe("Config", func() {
2626
opts := config.SQL[0].GetOptions()
2727
Expect(opts.Queries).To(HaveLen(2))
2828
Expect(opts.Queries).To(ContainElements("CopyUsers", "GetUserByEmail"))
29+
Expect(opts.Tables.Exclude).To(ContainElement("posts"))
2930
})
3031

3132
When("the file does not exist", func() {
@@ -50,6 +51,7 @@ var _ = Describe("Config", func() {
5051
sql := sqlc.SQL{}
5152
opts := sql.GetOptions()
5253
Expect(opts.Queries).To(BeNil())
54+
Expect(opts.Tables.Exclude).To(BeNil())
5355
})
5456

5557
It("returns empty options when no matching plugin", func() {
@@ -60,6 +62,7 @@ var _ = Describe("Config", func() {
6062
}
6163
opts := sql.GetOptions()
6264
Expect(opts.Queries).To(BeNil())
65+
Expect(opts.Tables.Exclude).To(BeNil())
6366
})
6467

6568
It("returns options for the gen-queries plugin", func() {
@@ -70,13 +73,15 @@ var _ = Describe("Config", func() {
7073
Out: "ent/query",
7174
Options: sqlc.CodegenOptions{
7275
Queries: []string{"ListUsers", "CopyUsers"},
76+
Tables: sqlc.TableOptions{Exclude: []string{"audit_logs"}},
7377
},
7478
},
7579
},
7680
}
7781
opts := sql.GetOptions()
7882
Expect(opts.Queries).To(HaveLen(2))
7983
Expect(opts.Queries).To(ContainElements("ListUsers", "CopyUsers"))
84+
Expect(opts.Tables.Exclude).To(ContainElement("audit_logs"))
8085
})
8186
})
8287

@@ -142,4 +147,47 @@ var _ = Describe("Config", func() {
142147
Expect(exists).To(BeFalse())
143148
})
144149
})
150+
151+
Describe("SQL.GetExcludeSet", func() {
152+
It("returns an empty map when codegen is nil", func() {
153+
sql := sqlc.SQL{}
154+
excludeSet := sql.GetExcludeSet()
155+
Expect(excludeSet).NotTo(BeNil())
156+
Expect(excludeSet).To(BeEmpty())
157+
})
158+
159+
It("returns an empty map when exclude is empty", func() {
160+
sql := sqlc.SQL{
161+
Codegen: []sqlc.Codegen{
162+
{
163+
Plugin: "gen-queries",
164+
Out: "out",
165+
Options: sqlc.CodegenOptions{Tables: sqlc.TableOptions{Exclude: []string{}}},
166+
},
167+
},
168+
}
169+
excludeSet := sql.GetExcludeSet()
170+
Expect(excludeSet).NotTo(BeNil())
171+
Expect(excludeSet).To(BeEmpty())
172+
})
173+
174+
It("returns a map with excluded table names", func() {
175+
sql := sqlc.SQL{
176+
Codegen: []sqlc.Codegen{
177+
{
178+
Plugin: "gen-queries",
179+
Out: "out",
180+
Options: sqlc.CodegenOptions{
181+
Tables: sqlc.TableOptions{Exclude: []string{"users", "analytics.events"}},
182+
},
183+
},
184+
},
185+
}
186+
excludeSet := sql.GetExcludeSet()
187+
Expect(excludeSet).To(HaveLen(2))
188+
Expect(excludeSet["users"]).To(BeTrue())
189+
Expect(excludeSet["analytics.events"]).To(BeTrue())
190+
Expect(excludeSet["posts"]).To(BeFalse())
191+
})
192+
})
145193
})

internal/sqlc/config_test_exclude.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ sql:
1111
- plugin: gen-queries
1212
out: "ent/query"
1313
options:
14+
tables:
15+
exclude:
16+
- "posts"
1417
queries:
1518
- "CopyUsers"
1619
- "GetUserByEmail"

internal/sqlc/generator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ func (x *Generator) Generate() error {
162162
}
163163

164164
queries := config.GetQueriesSet()
165+
exclude := config.GetExcludeSet()
165166

166167
for _, schema := range x.Catalog.Schemas {
167168
for _, table := range schema.Tables {
169+
if tableExcluded(exclude, schema.Name, table.Name) {
170+
continue
171+
}
172+
168173
file, err := os.Create(
169174
filepath.Join(config.Queries,
170175
fmt.Sprintf("%s.sql", table.Name)),

internal/sqlc/generator_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ var _ = Describe("Generator", func() {
4949
}
5050
})
5151

52+
It("skips excluded tables", func() {
53+
dir := generator.Config.SQL[0].Queries
54+
generator.Config.SQL[0].Codegen = []sqlc.Codegen{
55+
{
56+
Plugin: "gen-queries",
57+
Out: dir,
58+
Options: sqlc.CodegenOptions{
59+
Tables: sqlc.TableOptions{Exclude: []string{"public.posts"}},
60+
Queries: []string{"ListPostsByTitle"},
61+
},
62+
},
63+
}
64+
65+
Expect(generator.Generate()).NotTo(HaveOccurred())
66+
67+
for _, config := range generator.Config.SQL {
68+
Expect(filepath.Join(config.Queries, "users.sql")).To(BeAnExistingFile())
69+
Expect(filepath.Join(config.Queries, "posts.sql")).NotTo(BeAnExistingFile())
70+
}
71+
})
72+
5273
It("generates valid SQL content with default PK queries", func() {
5374
err := generator.Generate()
5475
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)