use custom type marshalling via database/sql interface
* remove cruft from sqlite package * update sqlboiler.toml * fix default value in migration * add default tests
This commit is contained in:
parent
77b1f5268a
commit
418f1d6606
|
@ -12,7 +12,7 @@ CREATE TABLE config (
|
|||
-- to have something to update, we insert the first and only row at id 0
|
||||
INSERT INTO config (id, privacyMode) VALUES (
|
||||
0, -- the constant id we will query
|
||||
1 -- community is the default mode unless overridden
|
||||
2 -- community is the default mode unless overridden
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/friendsofgo/errors"
|
||||
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
"github.com/volatiletech/sqlboiler/v4/queries"
|
||||
"github.com/volatiletech/sqlboiler/v4/queries/qm"
|
||||
|
@ -22,8 +23,8 @@ import (
|
|||
|
||||
// Config is an object representing the database table.
|
||||
type Config struct {
|
||||
ID int64 `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
PrivacyMode int64 `boil:"privacyMode" json:"privacyMode" toml:"privacyMode" yaml:"privacyMode"`
|
||||
ID int64 `boil:"id" json:"id" toml:"id" yaml:"id"`
|
||||
PrivacyMode roomdb.PrivacyMode `boil:"privacyMode" json:"privacyMode" toml:"privacyMode" yaml:"privacyMode"`
|
||||
|
||||
R *configR `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
L configL `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
|
@ -39,12 +40,33 @@ var ConfigColumns = struct {
|
|||
|
||||
// Generated where
|
||||
|
||||
type whereHelperroomdb_PrivacyMode struct{ field string }
|
||||
|
||||
func (w whereHelperroomdb_PrivacyMode) EQ(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.EQ, x)
|
||||
}
|
||||
func (w whereHelperroomdb_PrivacyMode) NEQ(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.NEQ, x)
|
||||
}
|
||||
func (w whereHelperroomdb_PrivacyMode) LT(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.LT, x)
|
||||
}
|
||||
func (w whereHelperroomdb_PrivacyMode) LTE(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.LTE, x)
|
||||
}
|
||||
func (w whereHelperroomdb_PrivacyMode) GT(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GT, x)
|
||||
}
|
||||
func (w whereHelperroomdb_PrivacyMode) GTE(x roomdb.PrivacyMode) qm.QueryMod {
|
||||
return qmhelper.Where(w.field, qmhelper.GTE, x)
|
||||
}
|
||||
|
||||
var ConfigWhere = struct {
|
||||
ID whereHelperint64
|
||||
PrivacyMode whereHelperint64
|
||||
PrivacyMode whereHelperroomdb_PrivacyMode
|
||||
}{
|
||||
ID: whereHelperint64{field: "\"config\".\"id\""},
|
||||
PrivacyMode: whereHelperint64{field: "\"config\".\"privacyMode\""},
|
||||
PrivacyMode: whereHelperroomdb_PrivacyMode{field: "\"config\".\"privacyMode\""},
|
||||
}
|
||||
|
||||
// ConfigRels is where relationship names are stored.
|
||||
|
|
|
@ -30,14 +30,7 @@ func (c Config) GetPrivacyMode(ctx context.Context) (roomdb.PrivacyMode, error)
|
|||
return roomdb.ModeUnknown, err
|
||||
}
|
||||
|
||||
// use a type conversion to tell compiler the returned value is a roomdb.PrivacyMode
|
||||
pm := (roomdb.PrivacyMode)(config.PrivacyMode)
|
||||
err = pm.IsValid()
|
||||
if err != nil {
|
||||
return roomdb.ModeUnknown, err
|
||||
}
|
||||
|
||||
return pm, nil
|
||||
return config.PrivacyMode, nil
|
||||
}
|
||||
|
||||
func (c Config) SetPrivacyMode(ctx context.Context, pm roomdb.PrivacyMode) error {
|
||||
|
@ -56,7 +49,7 @@ func (c Config) SetPrivacyMode(ctx context.Context, pm roomdb.PrivacyMode) error
|
|||
}
|
||||
|
||||
// set the new privacy mode
|
||||
config.PrivacyMode = int64(pm)
|
||||
config.PrivacyMode = pm
|
||||
// issue update stmt
|
||||
rowsAffected, err := config.Update(ctx, tx, boil.Infer())
|
||||
if err != nil {
|
||||
|
|
|
@ -26,14 +26,19 @@ func TestRoomConfig(t *testing.T) {
|
|||
db, err := Open(tr)
|
||||
r.NoError(err)
|
||||
|
||||
// make sure we have the expected default
|
||||
pm, err := db.Config.GetPrivacyMode(ctx)
|
||||
r.NoError(err)
|
||||
r.Equal(pm, roomdb.ModeCommunity, "privacy mode was unknown: %s", pm)
|
||||
|
||||
// test setting a valid privacy mode
|
||||
err = db.Config.SetPrivacyMode(ctx, roomdb.ModeCommunity)
|
||||
err = db.Config.SetPrivacyMode(ctx, roomdb.ModeRestricted)
|
||||
r.NoError(err)
|
||||
|
||||
// make sure the mode was set correctly by getting it
|
||||
pm, err := db.Config.GetPrivacyMode(ctx)
|
||||
pm, err = db.Config.GetPrivacyMode(ctx)
|
||||
r.NoError(err)
|
||||
r.Equal(pm, roomdb.ModeCommunity, "privacy mode was unknown")
|
||||
r.Equal(pm, roomdb.ModeRestricted, "privacy mode was unknown")
|
||||
|
||||
// test setting an invalid privacy mode
|
||||
err = db.Config.SetPrivacyMode(ctx, 1337)
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
[sqlite3]
|
||||
# go test in the admindb/sqlite package will create this
|
||||
# go test in the roomdb/sqlite package will create this
|
||||
dbname = "testrun/TestSchema/roomdb"
|
||||
blacklist = ["gorp_migrations"]
|
||||
|
||||
|
||||
# marshal pub_key strings ala @asdjjasd as feed references.
|
||||
[[types]]
|
||||
[types.match]
|
||||
|
@ -17,3 +16,17 @@ blacklist = ["gorp_migrations"]
|
|||
|
||||
[types.imports]
|
||||
third_party = ['"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"']
|
||||
|
||||
# convert from database-stored integers to the type roomdb.RoomConfig
|
||||
[[types]]
|
||||
[types.match]
|
||||
name = "privacyMode"
|
||||
tables = ['config']
|
||||
type = "int64"
|
||||
nullable = false
|
||||
|
||||
[types.replace]
|
||||
type = "roomdb.PrivacyMode"
|
||||
|
||||
[types.imports]
|
||||
third_party = ['"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"']
|
||||
|
|
|
@ -68,6 +68,32 @@ const (
|
|||
ModeRestricted
|
||||
)
|
||||
|
||||
// Implements the SQL marshaling interfaces (Scanner for Scan & Valuer for Value) for PrivacyMode
|
||||
|
||||
// Scan implements https://pkg.go.dev/database/sql#Scanner to read integers into a privacy mode
|
||||
func (pm *PrivacyMode) Scan(src interface{}) error {
|
||||
dbValue, ok := src.(int64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type: %T", src)
|
||||
}
|
||||
|
||||
privacyMode := PrivacyMode(dbValue)
|
||||
|
||||
err := privacyMode.IsValid()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*pm = privacyMode
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value returns privacy mode references as int64 to the database.
|
||||
// https://pkg.go.dev/database/sql/driver#Valuer
|
||||
func (pm PrivacyMode) Value() (driver.Value, error) {
|
||||
return driver.Value(int64(pm)), nil
|
||||
}
|
||||
|
||||
//go:generate go run golang.org/x/tools/cmd/stringer -type=Role
|
||||
|
||||
// Role describes the authorization level of an internal user (or member).
|
||||
|
|
Loading…
Reference in New Issue