Skip to content

Commit

Permalink
v6.0.2 (#704)
Browse files Browse the repository at this point in the history
* fix: improve model ID field customization (#604)

Updates places where `"id"` was hardcoded instead of using `model.IDField()`.

* Ensure uninitialized map is initialized when unmarshaling json
Add tests for this scenario

* exclude migration_table_name from connection string

* add test for OptionsString

* Add support for pointer FKs when preloading a belongs_to association (#602)

* feat: support context-aware tablenames (#614)

This patch adds a feature which enables pop to pass down the connection context to the model's TableName() function by implementing TableName(ctx context.Context) string. The context can be used to dynamically generate tablenames which can be important for prefixed or generic tables and other use cases.

* Bump pg deps (#616)

* Reset to development

* bumping pgx and pgconn versions

Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* Latest from master (#620)

* Latest from development (#617)

* fix: improve model ID field customization (#604)

Updates places where `"id"` was hardcoded instead of using `model.IDField()`.

* Ensure uninitialized map is initialized when unmarshaling json
Add tests for this scenario

* exclude migration_table_name from connection string

* add test for OptionsString

* Add support for pointer FKs when preloading a belongs_to association (#602)

* feat: support context-aware tablenames (#614)

This patch adds a feature which enables pop to pass down the connection context to the model's TableName() function by implementing TableName(ctx context.Context) string. The context can be used to dynamically generate tablenames which can be important for prefixed or generic tables and other use cases.

* Bump pg deps (#616)

* Reset to development

* bumping pgx and pgconn versions

Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* adding goreleaser syntaz (#619)

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* Resolve issues in UPDATE and DELETE when using schemas (#618)

* Resolve MySQL issues and improve test migrations
* Bump CockroachDB to maintained and supported versions
Version 2.1 has reached EoL in 2019

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Use `PaginatorPageKey` and `PaginatorPerPageKey` variables (#615)

* update pagination_test

* Pass Time structure into timestamp update functions. (#625)

Closes #624

* Allow nullable JSONB and resolve MySQL regression (#639)

* Allow passing args to `Order` (#630)

* Added connection maximum idle time configuration (#635)

This PR add the possibility to configure the connection maximum idle time (https://golang.org/pkg/database/sql/#DB.SetConnMaxIdleTime).

Closes #632

BREAKING CHANGE: Requires Go 1.15 from now on.

* Bump sqlite to 3.35.4 / 1.14.7 (#642)

* Update pg, pgx, sqlx (#643)

- `jackc/pgx` to  version `v4.11.0`.
- `jmoiron/sqlx` to version`v1.3.3`
- `lib/pq` to version`v1.10.1`

* Fix Inner has many associations when passing on multiple arguments (#633)

* Fix Inner has many associations when passing on multiple arguments for inner fields

* Fix broken tests

* clean up extractFieldAndInnerFields function

Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

* Remove many to many TX condition for EagerPreload (#645)

* Remove the need to use Tx when loading many to many associations

* replace TX access to create a new tx.Store.Transaction() object

* Added fix/tests for has_many with pointer foreign key (#647)

Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

* Export WhereID, Alias, WhereNamedID (#637)

This patch export some model convenience functions which are useful when constructing queries outside of pop: custom updates, deletes, inserts, ...

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

* fix: log model values everywhere (#656)

Some SQL logs were missing the values as argument. This adds all places

* Add delete to query builder (#658)

This allows writing delete queries without knowing the exact primary key or for composite keys.
`Destroy` only allows to delete by primary key, but there are many cases where you want to delete multiple rows or by some other query than the ID.

See #29

* Sort down migrations (#657)

Basically, just reversing the up migration order does not work, as that puts "all" migrations before specific ones. Therefore, I added implemented the proper `Less` function for down migrations explicitly.

Related #533

Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>

* Preserve eager information when validating models (#664) (#665)

Co-authored-by: Karl Haas <karl.haas@coditects.com>

* Migrate from packr to fs (#667)

* Updating Pgx (#660)

* adding goreleaser syntaz

* updating pgx now really

* Migrate from packr to fs

* Migrate to v6

* Use old build tags

* Update error handling

* Fix error after rebase

* Fix error handling

* Fix filenames for embed Go 1.16 usage

Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

* Task merging master (#669)

* v5.3.4 (#644)

* fix: improve model ID field customization (#604)

Updates places where `"id"` was hardcoded instead of using `model.IDField()`.

* Ensure uninitialized map is initialized when unmarshaling json
Add tests for this scenario

* exclude migration_table_name from connection string

* add test for OptionsString

* Add support for pointer FKs when preloading a belongs_to association (#602)

* feat: support context-aware tablenames (#614)

This patch adds a feature which enables pop to pass down the connection context to the model's TableName() function by implementing TableName(ctx context.Context) string. The context can be used to dynamically generate tablenames which can be important for prefixed or generic tables and other use cases.

* Bump pg deps (#616)

* Reset to development

* bumping pgx and pgconn versions

Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* Latest from master (#620)

* Latest from development (#617)

* fix: improve model ID field customization (#604)

Updates places where `"id"` was hardcoded instead of using `model.IDField()`.

* Ensure uninitialized map is initialized when unmarshaling json
Add tests for this scenario

* exclude migration_table_name from connection string

* add test for OptionsString

* Add support for pointer FKs when preloading a belongs_to association (#602)

* feat: support context-aware tablenames (#614)

This patch adds a feature which enables pop to pass down the connection context to the model's TableName() function by implementing TableName(ctx context.Context) string. The context can be used to dynamically generate tablenames which can be important for prefixed or generic tables and other use cases.

* Bump pg deps (#616)

* Reset to development

* bumping pgx and pgconn versions

Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* adding goreleaser syntaz (#619)

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>

* Resolve issues in UPDATE and DELETE when using schemas (#618)

* Resolve MySQL issues and improve test migrations
* Bump CockroachDB to maintained and supported versions
Version 2.1 has reached EoL in 2019

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Use `PaginatorPageKey` and `PaginatorPerPageKey` variables (#615)

* update pagination_test

* Pass Time structure into timestamp update functions. (#625)

Closes #624

* Allow nullable JSONB and resolve MySQL regression (#639)

* Allow passing args to `Order` (#630)

* Added connection maximum idle time configuration (#635)

This PR add the possibility to configure the connection maximum idle time (https://golang.org/pkg/database/sql/#DB.SetConnMaxIdleTime).

Closes #632

BREAKING CHANGE: Requires Go 1.15 from now on.

* Bump sqlite to 3.35.4 / 1.14.7 (#642)

* Update pg, pgx, sqlx (#643)

- `jackc/pgx` to  version `v4.11.0`.
- `jmoiron/sqlx` to version`v1.3.3`
- `lib/pq` to version`v1.10.1`

* Fix Inner has many associations when passing on multiple arguments (#633)

* Fix Inner has many associations when passing on multiple arguments for inner fields

* Fix broken tests

* clean up extractFieldAndInnerFields function

Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

* Remove many to many TX condition for EagerPreload (#645)

* Remove the need to use Tx when loading many to many associations

* replace TX access to create a new tx.Store.Transaction() object

* Added fix/tests for has_many with pointer foreign key (#647)

Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>
Co-authored-by: Brian Buchholz <4773480+bhb603@users.noreply.github.com>
Co-authored-by: Mike Pontillo <mpontillo@users.noreply.github.com>
Co-authored-by: Benjamin Blattberg <ben.blattberg@objectrocket.com>
Co-authored-by: Jonathan Duck <Duckbrain30@gmail.com>
Co-authored-by: Arthur Knoepflin <arthur.knoepflin@epitech.eu>

* Updating Pgx (#660)

* adding goreleaser syntaz

* updating pgx now really

* tidying

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>
Co-authored-by: Brian Buchholz <4773480+bhb603@users.noreply.github.com>
Co-authored-by: Mike Pontillo <mpontillo@users.noreply.github.com>
Co-authored-by: Benjamin Blattberg <ben.blattberg@objectrocket.com>
Co-authored-by: Jonathan Duck <Duckbrain30@gmail.com>
Co-authored-by: Arthur Knoepflin <arthur.knoepflin@epitech.eu>

* Replace removed  command

The  command has been removed from the CLI. This patch introduces a new mechanism to reliably dump the SQL schema for CockroachDB.

* Resolve `EagerPreload` panic caused for pointer references

Resolves a panic where `EagerPreload` tried to set `reflect.Struct` for a `reflect.Pointer` on 1.. associations.

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Resolve `EagerPreload` panic caused by NullUUID

Resolves a panic where `EagerPreload` was trying to set UUID into NullUUID.

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Support pointers in n+1 `Eager` loading

Resolves an issue where n+1 eager associations would error with a double pointer in `associations.ForStruct`.

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Improve error message of associations.ForStruct

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Add test cases for `IsZeroOfUnderlyingType`

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Resolve an obscure bug where empty structs got loaded for NULL foreign keys

Closes #139

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Resolve association regression in finders.go

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Use dedicated migrations for preloading regression test

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Fix code regression

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Fix test code regressions

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Fix sql migration order

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

* Resolve order issue in test

* Ignore order when testing for nil values in test

* Pass Context during exec in create. (#688)

* feat: support embedded struct fields (#691)

* test: use `T.TempDir` to create temporary test directory

This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* task: adding next version number

Co-authored-by: Patrik <zepatrik@users.noreply.github.com>
Co-authored-by: Michael Montgomery <mmontg1@gmail.com>
Co-authored-by: kyrozetera <jasonhale.w@gmail.com>
Co-authored-by: Reggie Riser <4960757+reggieriser@users.noreply.github.com>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
Co-authored-by: Stanislas Michalak <stanislas.michalak@gmail.com>
Co-authored-by: Larry M Jordan <larrymoralesjordan@gmail.com>
Co-authored-by: Brian Buchholz <4773480+bhb603@users.noreply.github.com>
Co-authored-by: Mike Pontillo <mpontillo@users.noreply.github.com>
Co-authored-by: Benjamin Blattberg <ben.blattberg@objectrocket.com>
Co-authored-by: Jonathan Duck <Duckbrain30@gmail.com>
Co-authored-by: Arthur Knoepflin <arthur.knoepflin@epitech.eu>
Co-authored-by: karlhaas <haaskarl81@gmail.com>
Co-authored-by: Karl Haas <karl.haas@coditects.com>
Co-authored-by: Matthias Fasching <fasching.matthias@gmail.com>
Co-authored-by: Martin Eigenbrodt <martin.eigenbrodt@googlemail.com>
Co-authored-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
18 people committed Apr 17, 2022
1 parent 0f98703 commit c29c06c
Show file tree
Hide file tree
Showing 26 changed files with 422 additions and 71 deletions.
4 changes: 4 additions & 0 deletions associations/association.go
Expand Up @@ -160,5 +160,9 @@ func fieldIsNil(f reflect.Value) bool {

// IsZeroOfUnderlyingType will check if the value of anything is the equal to the Zero value of that type.
func IsZeroOfUnderlyingType(x interface{}) bool {
if x == nil {
return true
}

return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}
37 changes: 37 additions & 0 deletions associations/association_test.go
@@ -0,0 +1,37 @@
package associations

import (
"database/sql"
"fmt"
"github.com/gobuffalo/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_IsZeroOfUnderlyingType(t *testing.T) {
for k, tc := range []struct {
in interface{}
zero bool
}{
{in: nil, zero: true},
{in: 0, zero: true},
{in: 1, zero: false},
{in: false, zero: true},
{in: "", zero: true},
{in: interface{}(nil), zero: true},
{in: uuid.NullUUID{}, zero: true},
{in: uuid.UUID{}, zero: true},
{in: uuid.NullUUID{Valid: true}, zero: false},
{in: nulls.Int{}, zero: true},
{in: nulls.String{}, zero: true},
{in: nulls.Bool{}, zero: true},
{in: nulls.Float64{}, zero: true},
{in: sql.NullString{}, zero: true},
{in: sql.NullString{Valid: true}, zero: false},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
assert.EqualValues(t, tc.zero, IsZeroOfUnderlyingType(tc.in))
})
}
}
13 changes: 11 additions & 2 deletions associations/associations_for_struct.go
@@ -1,7 +1,6 @@
package associations

import (
"errors"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -30,7 +29,7 @@ var associationBuilders = map[string]associationBuilder{}
func ForStruct(s interface{}, fields ...string) (Associations, error) {
t, v := getModelDefinition(s)
if t.Kind() != reflect.Struct {
return nil, errors.New("could not get struct associations: not a struct")
return nil, fmt.Errorf("could not get struct associations: not a struct but %T", s)
}
fields = trimFields(fields)
associations := Associations{}
Expand Down Expand Up @@ -73,6 +72,16 @@ func ForStruct(s interface{}, fields ...string) (Associations, error) {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)

// inline embedded field
if f.Anonymous {
innerAssociations, err := ForStruct(v.Field(i).Interface(), fields...)
if err != nil {
return nil, err
}
associations = append(associations, innerAssociations...)
continue
}

// ignores those fields not included in fields list.
if len(fields) > 0 && fieldIgnoredIn(fields, f.Name) {
continue
Expand Down
21 changes: 14 additions & 7 deletions associations/belongs_to_association.go
Expand Up @@ -128,15 +128,22 @@ func (b *belongsToAssociation) BeforeInterface() interface{} {

func (b *belongsToAssociation) BeforeSetup() error {
ownerID := reflect.Indirect(reflect.ValueOf(b.ownerModel.Interface())).FieldByName("ID")
if b.ownerID.CanSet() {
if n := nulls.New(b.ownerID.Interface()); n != nil {
b.ownerID.Set(reflect.ValueOf(n.Parse(ownerID.Interface())))
} else if b.ownerID.Kind() == reflect.Ptr {
b.ownerID.Set(ownerID.Addr())
toSet := b.ownerID
switch b.ownerID.Type().Name() {
case "NullUUID":
b.ownerID.FieldByName("Valid").Set(reflect.ValueOf(true))
toSet = b.ownerID.FieldByName("UUID")
}

if toSet.CanSet() {
if n := nulls.New(toSet.Interface()); n != nil {
toSet.Set(reflect.ValueOf(n.Parse(ownerID.Interface())))
} else if toSet.Kind() == reflect.Ptr {
toSet.Set(ownerID.Addr())
} else {
b.ownerID.Set(ownerID)
toSet.Set(ownerID)
}
return nil
}
return fmt.Errorf("could not set '%s' to '%s'", ownerID, b.ownerID)
return fmt.Errorf("could not set '%s' to '%s'", ownerID, toSet)
}
19 changes: 19 additions & 0 deletions associations/belongs_to_association_test.go
Expand Up @@ -22,6 +22,11 @@ type barBelongsTo struct {
Foo fooBelongsTo `belongs_to:"foo"`
}

type barBelongsToNullable struct {
FooID uuid.NullUUID `db:"foo_id"`
Foo *fooBelongsTo `belongs_to:"foo"`
}

func Test_Belongs_To_Association(t *testing.T) {
a := require.New(t)

Expand Down Expand Up @@ -50,3 +55,17 @@ func Test_Belongs_To_Association(t *testing.T) {
a.Equal(nil, before[index].BeforeInterface())
}
}

func Test_Belongs_To_Nullable_Association(t *testing.T) {
a := require.New(t)
id, _ := uuid.NewV1()

bar := barBelongsToNullable{Foo: &fooBelongsTo{id}}
as, err := associations.ForStruct(&bar, "Foo")
a.NoError(err)

before := as.AssociationsBeforeCreatable()
for index := range before {
a.Equal(nil, before[index].BeforeSetup())
}
}
50 changes: 32 additions & 18 deletions columns/columns_for_struct.go
Expand Up @@ -31,33 +31,47 @@ func ForStructWithAlias(s interface{}, tableName, tableAlias, idField string) (c
}
}

fieldCount := st.NumField()
// recursive functions to also find and add embedded struct fields
var findColumns func(st reflect.Type)
findColumns = func(t reflect.Type) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}

for i := 0; i < fieldCount; i++ {
field := st.Field(i)
fc := t.NumField()
for i := 0; i < fc; i++ {
field := t.Field(i)

popTags := TagsFor(field)
tag := popTags.Find("db")
if field.Anonymous {
findColumns(field.Type)
continue
}

if !tag.Ignored() && !tag.Empty() {
col := tag.Value
popTags := TagsFor(field)
tag := popTags.Find("db")

// add writable or readable.
tag := popTags.Find("rw")
if !tag.Empty() {
col = col + "," + tag.Value
}
if !tag.Ignored() && !tag.Empty() {
col := tag.Value

cs := columns.Add(col)
// add writable or readable.
tag := popTags.Find("rw")
if !tag.Empty() {
col = col + "," + tag.Value
}

// add select clause.
tag = popTags.Find("select")
if !tag.Empty() {
c := cs[0]
c.SetSelectSQL(tag.Value)
cs := columns.Add(col)

// add select clause.
tag = popTags.Find("select")
if !tag.Empty() {
c := cs[0]
c.SetSelectSQL(tag.Value)
}
}
}
}

findColumns(st)

return columns
}
35 changes: 33 additions & 2 deletions dialect_cockroach.go
@@ -1,11 +1,13 @@
package pop

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -200,13 +202,42 @@ func (p *cockroach) FizzTranslator() fizz.Translator {
}

func (p *cockroach) DumpSchema(w io.Writer) error {
cmd := exec.Command("cockroach", "dump", p.Details().Database, "--dump-mode=schema")
cmd := exec.Command("cockroach", "sql", "-e", "SHOW CREATE ALL TABLES", "-d", p.Details().Database, "--format", "raw")

c := p.ConnectionDetails
if defaults.String(c.Options["sslmode"], "disable") == "disable" || strings.Contains(c.RawOptions, "sslmode=disable") {
cmd.Args = append(cmd.Args, "--insecure")
}
return genericDumpSchema(p.Details(), cmd, w)
return cockroachDumpSchema(p.Details(), cmd, w)
}

func cockroachDumpSchema(deets *ConnectionDetails, cmd *exec.Cmd, w io.Writer) error {
log(logging.SQL, strings.Join(cmd.Args, " "))

var bb bytes.Buffer

cmd.Stdout = &bb
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
return err
}

// --format raw returns comments prefixed with # which is invalid, so we make it a valid SQL comment.
result := regexp.MustCompile("(?m)^#").ReplaceAll(bb.Bytes(), []byte("-- #"))

if _, err := w.Write(result); err != nil {
return err
}

x := bytes.TrimSpace(result)
if len(x) == 0 {
return fmt.Errorf("unable to dump schema for %s", deets.Database)
}

log(logging.Info, "dumped schema for %s", deets.Database)
return nil
}

func (p *cockroach) LoadSchema(r io.Reader) error {
Expand Down
2 changes: 1 addition & 1 deletion dialect_common.go
Expand Up @@ -85,7 +85,7 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable)
if err != nil {
return err
}
_, err = stmt.Exec(model.Value)
_, err = stmt.ExecContext(model.ctx, model.Value)
if err != nil {
if closeErr := stmt.Close(); closeErr != nil {
return fmt.Errorf("failed to close prepared statement: %s: %w", closeErr, err)
Expand Down
3 changes: 2 additions & 1 deletion dialect_sqlite.go
Expand Up @@ -169,10 +169,11 @@ func (m *sqlite) CreateDB() error {
if err != nil {
return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
}
_, err = os.Create(m.ConnectionDetails.Database)
f, err := os.Create(m.ConnectionDetails.Database)
if err != nil {
return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
}
_ = f.Close()

log(logging.Info, "created database '%s'", m.ConnectionDetails.Database)
return nil
Expand Down
10 changes: 2 additions & 8 deletions dialect_sqlite_test.go
Expand Up @@ -4,8 +4,6 @@ package pop

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -133,10 +131,8 @@ func Test_ConnectionDetails_Finalize_SQLite_OverrideOptions_Synonym_Path(t *test

func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {
r := require.New(t)
d, err := ioutil.TempDir("", "")
r.NoError(err)
d := t.TempDir()
p := filepath.Join(d, "testdb.sqlite")
defer os.RemoveAll(p)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
Expand All @@ -148,10 +144,8 @@ func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {

func TestSqlite_CreateDB(t *testing.T) {
r := require.New(t)
dir, err := ioutil.TempDir("", "")
r.NoError(err)
dir := t.TempDir()
p := filepath.Join(dir, "testdb.sqlite")
defer os.RemoveAll(p)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
Expand Down
27 changes: 27 additions & 0 deletions executors_test.go
Expand Up @@ -533,6 +533,33 @@ func Test_Create_Non_PK_ID(t *testing.T) {
})
}

func Test_Embedded_Struct(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)

entry := &EmbeddingStruct{
InnerStruct: InnerStruct{},
AdditionalField: "I am also important!",
}
r.NoError(tx.Create(entry))

var actual EmbeddingStruct
r.NoError(tx.Find(&actual, entry.ID))
r.Equal(entry.AdditionalField, actual.AdditionalField)

entry.AdditionalField = entry.AdditionalField + " updated"
r.NoError(tx.Update(entry))

r.NoError(tx.Find(&actual, entry.ID))
r.Equal(entry.AdditionalField, actual.AdditionalField)

r.NoError(tx.Destroy(entry))
})
}

func Test_Eager_Create_Has_Many(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
Expand Down
9 changes: 8 additions & 1 deletion finders.go
Expand Up @@ -282,7 +282,14 @@ func (q *Query) eagerDefaultAssociations(model interface{}) error {
v = reflect.Indirect(reflect.ValueOf(model)).FieldByName(inner.Name)
innerQuery := Q(query.Connection)
innerQuery.eagerFields = inner.Fields
err = innerQuery.eagerAssociations(v.Addr().Interface())

switch v.Kind() {
case reflect.Ptr:
err = innerQuery.eagerAssociations(v.Interface())
default:
err = innerQuery.eagerAssociations(v.Addr().Interface())
}

if err != nil {
return err
}
Expand Down

0 comments on commit c29c06c

Please sign in to comment.