Skip to content

Commit f43114a

Browse files
committed
Merge branch 'dev'
2 parents 0116414 + b656a98 commit f43114a

File tree

17 files changed

+301
-75
lines changed

17 files changed

+301
-75
lines changed

CHANGELOG.md

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
44
and this project adheres to [Semantic
55
Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
- Add types.DecimalContext to control the context with which new decimals
10+
are created. This is important if your application uses a special context
11+
with more precision or requires the Go operating mode for example.
12+
- Add WhereNotIn/AndNotIn/OrNotIn query mods to help solve a bug
13+
- Add alias struct case type (uses the columns alias) (thanks @Darkclainer)
14+
- Add ability to type replace on tables (thanks @stephenafamo)
15+
16+
### Changed
17+
18+
- Change the way column cache keys are created and looked up, improving memory
19+
performance of sqlboiler's caching layer (thanks @zikaeroh)
20+
21+
### Fixed
22+
23+
- Fix the psql driver to correctly ignore generated columns (thanks @chochihim)
24+
- Fix an issue with mariadb ssl-mode when running generated tests
25+
(thanks @tooolbox)
26+
- Fix $1 placeholder in mysql DeleteAll() when using soft delete
27+
(thanks @mfzy602)
28+
- Fix boilingcore tests to use current module via replace instead of the
29+
published v4 module
30+
731
## [v4.1.2] - 2020-05-18
832

933
### Fixed

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ Flags:
444444
--no-tests Disable generated go test files
445445
-o, --output string The name of the folder to output to (default "models")
446446
-p, --pkgname string The name you wish to assign to your generated package (default "models")
447-
--struct-tag-casing string Decides the casing for go structure tag names. camel, title or snake (default snake) (default "snake")
447+
--struct-tag-casing string Decides the casing for go structure tag names. camel, title, alias or snake (default "snake")
448448
-t, --tag strings Struct tags to be included on your models in addition to json, yaml, toml
449449
--tag-ignore strings List of column names that should have tags values set to '-' (ignored during parsing)
450450
--templates strings A templates directory, overrides the bindata'd template folders in sqlboiler
@@ -639,10 +639,17 @@ The way to accomplish this is through the config file.
639639
[[types]]
640640
# The match is a drivers.Column struct, and matches on almost all fields.
641641
# Notable exception for the unique bool. Matches are done
642-
# with "logical and" meaning it must match all specified matchers. Boolean values
643-
# are only checked if all the string specifiers match first, and they
644-
# must always match.
642+
# with "logical and" meaning it must match all specified matchers.
643+
# Boolean values are only checked if all the string specifiers match first,
644+
# and they must always match.
645+
#
645646
# Not shown here: db_type is the database type and a very useful matcher
647+
# We can also whitelist tables for this replace by adding to the types.match:
648+
# tables = ['users', 'videos']
649+
#
650+
# Note there is precedence for types.match, more specific things should appear
651+
# further down in the config as once a matching rule is found it is executed
652+
# immediately.
646653
[types.match]
647654
type = "null.String"
648655
nullable = true

boilingcore/boilingcore.go

+20
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ func (s *State) processTypeReplacements() error {
420420
for i := range s.Tables {
421421
t := s.Tables[i]
422422

423+
if !shouldReplaceInTable(t, r) {
424+
continue
425+
}
426+
423427
for j := range t.Columns {
424428
c := t.Columns[j]
425429
if matchColumn(c, r.Match) {
@@ -521,6 +525,22 @@ func columnMerge(dst, src drivers.Column) drivers.Column {
521525
return ret
522526
}
523527

528+
// shouldReplaceInTable checks if tables were specified in types.match in the config.
529+
// If tables were set, it checks if the given table is among the specified tables.
530+
func shouldReplaceInTable(t drivers.Table, r TypeReplace) bool {
531+
if len(r.Tables) == 0 {
532+
return true
533+
}
534+
535+
for _, replaceInTable := range r.Tables {
536+
if replaceInTable == t.Name {
537+
return true
538+
}
539+
}
540+
541+
return false
542+
}
543+
524544
// initOutFolders creates the folders that will hold the generated output.
525545
func (s *State) initOutFolders(lazyTemplates []lazyTemplate) error {
526546
if s.Config.Wipe {

boilingcore/boilingcore_test.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,23 @@ func TestNew(t *testing.T) {
6565

6666
buf := &bytes.Buffer{}
6767

68-
cmd := exec.Command("go", "mod", "init", "github.com/volatiletech/sqlboiler-test")
68+
cmd := exec.Command("go", "env", "GOMOD")
69+
goModFilePath, err := cmd.Output()
70+
if err != nil {
71+
t.Fatalf("go env GOMOD cmd execution failed: %s", err)
72+
}
73+
74+
cmd = exec.Command("go", "mod", "init", "github.com/volatiletech/sqlboiler-test")
75+
cmd.Dir = state.Config.OutFolder
76+
cmd.Stderr = buf
77+
78+
if err = cmd.Run(); err != nil {
79+
t.Errorf("go mod init cmd execution failed: %s", err)
80+
outputCompileErrors(buf, state.Config.OutFolder)
81+
fmt.Println()
82+
}
83+
84+
cmd = exec.Command("go", "mod", "edit", fmt.Sprintf("-replace=github.com/volatiletech/sqlboiler/v4=%s", filepath.Dir(string(goModFilePath))))
6985
cmd.Dir = state.Config.OutFolder
7086
cmd.Stderr = buf
7187

@@ -192,6 +208,18 @@ func TestProcessTypeReplacements(t *testing.T) {
192208
},
193209
},
194210
},
211+
{
212+
Name: "named_table",
213+
Columns: []drivers.Column{
214+
{
215+
Name: "id",
216+
Type: "int",
217+
DBType: "serial",
218+
Default: "some db nonsense",
219+
Nullable: false,
220+
},
221+
},
222+
},
195223
}
196224

197225
s.Config.TypeReplaces = []TypeReplace{
@@ -206,6 +234,18 @@ func TestProcessTypeReplacements(t *testing.T) {
206234
ThirdParty: []string{`"rock.com/excellent"`},
207235
},
208236
},
237+
{
238+
Tables: []string{"named_table"},
239+
Match: drivers.Column{
240+
DBType: "serial",
241+
},
242+
Replace: drivers.Column{
243+
Type: "excellent.NamedType",
244+
},
245+
Imports: importers.Set{
246+
ThirdParty: []string{`"rock.com/excellent-name"`},
247+
},
248+
},
209249
{
210250
Match: drivers.Column{
211251
Type: "null.String",
@@ -255,4 +295,11 @@ func TestProcessTypeReplacements(t *testing.T) {
255295
if i := s.Config.Imports.BasedOnType["big.Int"].Standard[0]; i != `"math/big"` {
256296
t.Error("imports were not adjusted")
257297
}
298+
299+
if typ := s.Tables[1].Columns[0].Type; typ != "excellent.NamedType" {
300+
t.Error("type was wrong:", typ)
301+
}
302+
if i := s.Config.Imports.BasedOnType["excellent.NamedType"].ThirdParty[0]; i != `"rock.com/excellent-name"` {
303+
t.Error("imports were not adjusted")
304+
}
258305
}

boilingcore/config.go

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Config struct {
4545

4646
// TypeReplace replaces a column type with something else
4747
type TypeReplace struct {
48+
Tables []string `toml:"tables,omitempty" json:"tables,omitempty"`
4849
Match drivers.Column `toml:"match,omitempty" json:"match,omitempty"`
4950
Replace drivers.Column `toml:"replace,omitempty" json:"replace,omitempty"`
5051
Imports importers.Set `toml:"imports,omitempty" json:"imports,omitempty"`
@@ -194,6 +195,8 @@ func ConvertTypeReplace(i interface{}) []TypeReplace {
194195
replace.Match = columnFromInterface(replaceIntf["match"])
195196
replace.Replace = columnFromInterface(replaceIntf["replace"])
196197

198+
replace.Tables = tablesOfTypeReplace(replaceIntf["match"])
199+
197200
if imps := replaceIntf["imports"]; imps != nil {
198201
imps = cast.ToStringMap(imps)
199202
var err error
@@ -209,6 +212,17 @@ func ConvertTypeReplace(i interface{}) []TypeReplace {
209212
return replaces
210213
}
211214

215+
func tablesOfTypeReplace(i interface{}) []string {
216+
tables := []string{}
217+
218+
m := cast.ToStringMap(i)
219+
if s := m["tables"]; s != nil {
220+
tables = cast.ToStringSlice(s)
221+
}
222+
223+
return tables
224+
}
225+
212226
func columnFromInterface(i interface{}) (col drivers.Column) {
213227
m := cast.ToStringMap(i)
214228
if s := m["name"]; s != nil {

boilingcore/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package boilingcore
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/volatiletech/sqlboiler/v4/drivers"
@@ -186,6 +187,7 @@ func TestConvertTypeReplace(t *testing.T) {
186187
"udt_name": "d",
187188
"full_db_type": "e",
188189
"arr_type": "f",
190+
"tables": []string{"g", "h"},
189191
"auto_generated": true,
190192
"nullable": true,
191193
}
@@ -248,4 +250,7 @@ func TestConvertTypeReplace(t *testing.T) {
248250
if got := r.Imports.ThirdParty[0]; got != "github.com/abc" {
249251
t.Error("standard import wrong:", got)
250252
}
253+
if got := r.Tables; !reflect.DeepEqual(r.Tables, []string{"g", "h"}) {
254+
t.Error("tables in types.match wrong:", got)
255+
}
251256
}

0 commit comments

Comments
 (0)