Skip to content

Commit 9aacf04

Browse files
committed
**feat: add support for table and materialized view configuration in database generation**
- **SQLite**: - Introduced an optional `Tables` configuration to specify the tables to be included. Defaults to all tables if not set. - Updated the loop to handle the configured tables, improving flexibility in model generation. - **PostgreSQL**: - Added support for optional `Tables` and `MaterializedViews` configurations. If provided, these override the default query behavior. - Enhanced error handling for cases where queries return errors while fetching tables or materialized views. - **Configuration**: - Updated the configuration struct to include `Tables` and `MaterializedViews` fields. - Annotated sample configurations in comments for clear guidance on using the new options. This update provides fine-grained control over table and view inclusion during model generation, aligning with user-defined configurations.
1 parent e17b491 commit 9aacf04

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type (
2222
OutPath string
2323
OutPackagePath string
2424
ImportPackagePaths []string
25+
Tables *[]string
26+
MaterializedViews *[]string
2527
JsonTagOverridesByTable map[string]map[string]string
2628
ExtraFields map[string][]ExtraField
2729
TypeMap map[string]string
@@ -253,6 +255,12 @@ ImportPackagePaths = [
253255
"github.com/dan-sherwin/gormdb2struct/pgtypes",
254256
]
255257
258+
# Tables (optional. defaults to all tables)
259+
#Tables = ["foo", 'bar']
260+
261+
# Materialized Views (optional. defaults to all)
262+
#MaterializedViews = ["foo","bar"]
263+
256264
# TypeMap: database column type overrides (optional)
257265
[TypeMap]
258266
# "jsonb" = "datatypes.JSONMap"

postgresql.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,22 @@ func postgresToGorm(cfg ConversionConfig) {
8282
})
8383

8484
tables := []string{}
85-
err = db.Raw("select table_name from information_schema.tables where table_schema = 'public'").Scan(&tables).Error
86-
if err != nil {
87-
log.Fatal(err.Error())
85+
if cfg.Tables != nil {
86+
tables = *cfg.Tables
87+
} else {
88+
err = db.Raw("select table_name from information_schema.tables where table_schema = 'public'").Scan(&tables).Error
89+
if err != nil {
90+
log.Fatal(err.Error())
91+
}
8892
}
89-
9093
materializedViews := []string{}
91-
err = db.Raw("select matviewname from pg_matviews where schemaname='public'").Scan(&materializedViews).Error
92-
if err != nil {
93-
log.Fatal(err.Error())
94+
if cfg.MaterializedViews != nil {
95+
materializedViews = *cfg.MaterializedViews
96+
} else {
97+
err = db.Raw("select matviewname from pg_matviews where schemaname='public'").Scan(&materializedViews).Error
98+
if err != nil {
99+
log.Fatal(err.Error())
100+
}
94101
}
95102

96103
g.WithJSONTagNameStrategy(func(col string) (tag string) { return strcase.ToLowerCamel(col) })

sqlite.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ func sqliteToGorm(cfg ConversionConfig) {
5555
// Build models to allow extraFields and jsonTagOverrides like Postgres path
5656
modelsMap := map[string]any{}
5757
modelStructNames := []string{}
58-
for _, tableName := range sqlitetype.TableNames(db) {
58+
tables := []string{}
59+
if cfg.Tables != nil {
60+
tables = *cfg.Tables
61+
} else {
62+
tables = sqlitetype.TableNames(db)
63+
}
64+
65+
for _, tableName := range tables {
5966
model := g.GenerateModel(tableName)
6067
if ef, ok := cfg.ExtraFields[tableName]; ok {
6168
for _, ef := range ef {

0 commit comments

Comments
 (0)