-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JSONB instead of JSON for context object in SQLite. (#871)
Use `jsonb` type instead of `json` for metric `context` object.
- Loading branch information
Showing
14 changed files
with
424 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package types | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
// JSONB defined JSONB data type, need to implements driver.Valuer, sql.Scanner interface | ||
type JSONB json.RawMessage | ||
|
||
// Value return json value, implement driver.Valuer interface | ||
func (j JSONB) Value() (driver.Value, error) { | ||
if len(j) == 0 { | ||
return nil, nil | ||
} | ||
return string(j), nil | ||
} | ||
|
||
// Scan scan value into Jsonb, implements sql.Scanner interface | ||
func (j *JSONB) Scan(value interface{}) error { | ||
if value == nil { | ||
*j = JSONB("null") | ||
return nil | ||
} | ||
var bytes []byte | ||
switch v := value.(type) { | ||
case []byte: | ||
if len(v) > 0 { | ||
bytes = make([]byte, len(v)) | ||
copy(bytes, v) | ||
} | ||
case string: | ||
bytes = []byte(v) | ||
default: | ||
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value)) | ||
} | ||
|
||
result := json.RawMessage(bytes) | ||
*j = JSONB(result) | ||
return nil | ||
} | ||
|
||
// MarshalJSON to output non base64 encoded []byte | ||
func (j JSONB) MarshalJSON() ([]byte, error) { | ||
return json.RawMessage(j).MarshalJSON() | ||
} | ||
|
||
// UnmarshalJSON to deserialize []byte | ||
func (j *JSONB) UnmarshalJSON(b []byte) error { | ||
result := json.RawMessage{} | ||
err := result.UnmarshalJSON(b) | ||
*j = JSONB(result) | ||
return err | ||
} | ||
|
||
func (j JSONB) String() string { | ||
return string(j) | ||
} | ||
|
||
// GormDataType gorm common data type | ||
func (JSONB) GormDataType() string { | ||
return "json" | ||
} | ||
|
||
// GormDBDataType gorm db data type | ||
func (JSONB) GormDBDataType(db *gorm.DB, field *schema.Field) string { | ||
return "JSONB" | ||
} | ||
|
||
// GormValue gorm db actual value | ||
// nolint | ||
func (js JSONB) GormValue(ctx context.Context, db *gorm.DB) clause.Expr { | ||
if len(js) == 0 { | ||
return gorm.Expr("NULL") | ||
} | ||
|
||
data, _ := js.MarshalJSON() | ||
return gorm.Expr("?", string(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package v_0010 | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
const Version = "10d125c68d9a" | ||
|
||
func Migrate(db *gorm.DB) error { | ||
return db.Transaction(func(tx *gorm.DB) error { | ||
if err := tx.AutoMigrate(&Context{}); err != nil { | ||
return err | ||
} | ||
return tx.Model(&SchemaVersion{}). | ||
Where("1 = 1"). | ||
Update("Version", Version). | ||
Error | ||
}) | ||
} |
Oops, something went wrong.