1
0
Fork 0
forked from forgejo/forgejo
This commit is contained in:
techknowlogick 2021-02-28 18:08:33 -05:00 committed by GitHub
parent 030646eea4
commit 47f6a4ec3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
947 changed files with 26119 additions and 7062 deletions

View file

@ -1,5 +1,13 @@
# Changelog
## v3.5.0 - 2021-01-11
- Fix insert of JSON values on PostgreSQL when using `binary_parameters=yes` in
the connection string
([#83](https://github.com/go-testfixtures/testfixtures/issues/83), [#84](https://github.com/go-testfixtures/testfixtures/pull/84), [lib/pq#528](https://github.com/lib/pq/issues/528)).
- Officially support binary columns through hexadecimal strings
([#48](https://github.com/go-testfixtures/testfixtures/issues/48), [#82](https://github.com/go-testfixtures/testfixtures/pull/82)).
## v3.4.1 - 2020-10-19
- Fix for Microsoft SQL Server databases with views

View file

@ -90,6 +90,13 @@ databases.
post: "..."
```
Binary columns can be represented as hexadecimal strings (should start with `0x`):
```yaml
- id: 1
binary_column: 0x1234567890abcdef
```
If you need to write raw SQL, probably to call a function, prefix the value
of the column with `RAW=`:
@ -129,7 +136,7 @@ func TestMain(m *testing.M) {
...
}
fixtures, err := testfixtures.New(
fixtures, err = testfixtures.New(
testfixtures.Database(db), // You database connection
testfixtures.Dialect("postgres"), // Available: "postgresql", "timescaledb", "mysql", "mariadb", "sqlite" and "sqlserver"
testfixtures.Directory("testdata/fixtures"), // the directory containing the YAML files
@ -497,6 +504,7 @@ unit test database code without having to connect to a real database
- [dbcleaner][dbcleaner] - Clean database for testing, inspired by
database_cleaner for Ruby
[doc]: https://pkg.go.dev/github.com/go-testfixtures/testfixtures/v3?tab=doc
[railstests]: http://guides.rubyonrails.org/testing.html#the-test-database
[gotxdb]: https://github.com/DATA-DOG/go-txdb
[gosqlmock]: https://github.com/DATA-DOG/go-sqlmock

View file

@ -0,0 +1,14 @@
package testfixtures
import (
"encoding/hex"
"fmt"
"strings"
)
func (l *Loader) tryHexStringToBytes(s string) ([]byte, error) {
if !strings.HasPrefix(s, "0x") {
return nil, fmt.Errorf("not a hexadecimal string, must be prefix 0x")
}
return hex.DecodeString(strings.TrimPrefix(s, "0x"))
}

View file

@ -2,6 +2,7 @@ package testfixtures
import (
"database/sql"
"encoding/hex"
"fmt"
"os"
"path/filepath"
@ -160,6 +161,7 @@ func convertValue(value interface{}) interface{} {
if utf8.Valid(v) {
return string(v)
}
return "0x" + hex.EncodeToString(value.([]byte))
}
return value
}

View file

@ -3,6 +3,7 @@ package testfixtures // import "github.com/go-testfixtures/testfixtures/v3"
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
@ -496,12 +497,18 @@ func (l *Loader) buildInsertSQL(f *fixtureFile, record map[interface{}]interface
sqlValues = append(sqlValues, strings.TrimPrefix(v, "RAW="))
continue
}
if t, err := l.tryStrToDate(v); err == nil {
if b, err := l.tryHexStringToBytes(v); err == nil {
value = b
} else if t, err := l.tryStrToDate(v); err == nil {
value = t
}
case []interface{}, map[interface{}]interface{}:
value = recursiveToJSON(v)
var bytes []byte
bytes, err = json.Marshal(recursiveToJSON(v))
if err != nil {
return
}
value = string(bytes)
}
switch l.helper.paramType() {