Skip to content
Merged
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
19 changes: 10 additions & 9 deletions generator/template/file_templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions generator/template/model_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type TableModelField struct {
Name string
Type Type
Tags []string
Skip bool
}

// DefaultTableModelField returns default TableModelField implementation
Expand All @@ -173,6 +174,7 @@ func DefaultTableModelField(columnMetaData metadata.Column) TableModelField {
Name: dbidentifier.ToGoIdentifier(columnMetaData.Name),
Type: getType(columnMetaData),
Tags: tags,
Skip: false,
}
}

Expand Down
11 changes: 11 additions & 0 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ func processTableSQLBuilder(fileTypes, dirPath string,
return insertedRowAlias(dialect)
},
"golangComment": formatGolangComment,
"columnList": func(columns []metadata.Column) string {
names := []string{}
for _, col := range columns {
bc := tableSQLBuilder.Column(col)
if bc.Skip {
continue
}
names = append(names, bc.Name+"Column")
}
return strings.Join(names, ", ")
},
})
if err != nil {
return fmt.Errorf("failed to generate table sql builder type %s: %w", tableSQLBuilder.TypeName, err)
Expand Down
1 change: 1 addition & 0 deletions generator/template/sql_builder_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (tb TableSQLBuilder) UseColumn(columnsFunc func(column metadata.Column) Tab

// TableSQLBuilderColumn is template for table sql builder column
type TableSQLBuilderColumn struct {
Skip bool
Name string
Type string
}
Expand Down
49 changes: 49 additions & 0 deletions tests/postgres/generator_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,55 @@ func TestGeneratorTemplate_SQLBuilder_ChangeColumnTypes(t *testing.T) {
require.Contains(t, actor, "ActorID postgres.ColumnString")
}

func TestGeneratorTemplate_Model_SQLBuilder_SkipColumn(t *testing.T) {
err := postgres.Generate(
tempTestDir,
dbConnection,
template.Default(postgres2.Dialect).
UseSchema(func(schemaMetaData metadata.Schema) template.Schema {
return template.DefaultSchema(schemaMetaData).
UseSQLBuilder(template.DefaultSQLBuilder().
UseTable(func(table metadata.Table) template.TableSQLBuilder {
return template.DefaultTableSQLBuilder(table).
UseColumn(func(column metadata.Column) template.TableSQLBuilderColumn {
defaultColumn := template.DefaultTableSQLBuilderColumn(column)

if defaultColumn.Name == "FirstName" {
defaultColumn.Skip = true
}

return defaultColumn
})
}),
).
UseModel(template.DefaultModel().
UseTable(func(table metadata.Table) template.TableModel {
return template.DefaultTableModel(table).
UseField(func(column metadata.Column) template.TableModelField {
defaultColumn := template.DefaultTableModelField(column)

if defaultColumn.Name == "FirstName" {
defaultColumn.Skip = true
}

return defaultColumn
})
}),
)
}),
)

require.Nil(t, err)

actorSql := file2.Exists(t, defaultActorSQLBuilderFilePath)
require.NotContains(t, actorSql, "FirstName")
require.Contains(t, actorSql, "ActorID")

actorModel := file2.Exists(t, defaultActorModelFilePath)
require.NotContains(t, actorModel, "FirstName")
require.Contains(t, actorModel, "ActorID")
}

func TestRenameEnumValueName(t *testing.T) {
err := postgres.Generate(
tempTestDir,
Expand Down
Loading