database/postgres: fix nil time errors
We need to use sql.NullTime, otherwise we get errors like these: sql: Scan error on column index 7, name "downstream_interacted_at": unsupported Scan, storing driver.Value type <nil> into type *time.Time
This commit is contained in:
parent
478bd2077b
commit
251af0d9d3
@ -248,3 +248,10 @@ func toNullString(s string) sql.NullString {
|
|||||||
Valid: s != "",
|
Valid: s != "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toNullTime(t time.Time) sql.NullTime {
|
||||||
|
return sql.NullTime{
|
||||||
|
Time: t,
|
||||||
|
Valid: !t.IsZero(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -318,12 +318,14 @@ func (db *PostgresDB) ListUsers(ctx context.Context) ([]User, error) {
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var user User
|
var user User
|
||||||
var password, nick, realname sql.NullString
|
var password, nick, realname sql.NullString
|
||||||
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin, &nick, &realname, &user.Enabled, &user.DownstreamInteractedAt); err != nil {
|
var downstreamInteractedAt sql.NullTime
|
||||||
|
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin, &nick, &realname, &user.Enabled, &downstreamInteractedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user.Password = password.String
|
user.Password = password.String
|
||||||
user.Nick = nick.String
|
user.Nick = nick.String
|
||||||
user.Realname = realname.String
|
user.Realname = realname.String
|
||||||
|
user.DownstreamInteractedAt = downstreamInteractedAt.Time
|
||||||
users = append(users, user)
|
users = append(users, user)
|
||||||
}
|
}
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
@ -340,17 +342,19 @@ func (db *PostgresDB) GetUser(ctx context.Context, username string) (*User, erro
|
|||||||
user := &User{Username: username}
|
user := &User{Username: username}
|
||||||
|
|
||||||
var password, nick, realname sql.NullString
|
var password, nick, realname sql.NullString
|
||||||
|
var downstreamInteractedAt sql.NullTime
|
||||||
row := db.db.QueryRowContext(ctx,
|
row := db.db.QueryRowContext(ctx,
|
||||||
`SELECT id, password, admin, nick, realname, enabled, downstream_interacted_at
|
`SELECT id, password, admin, nick, realname, enabled, downstream_interacted_at
|
||||||
FROM "User"
|
FROM "User"
|
||||||
WHERE username = $1`,
|
WHERE username = $1`,
|
||||||
username)
|
username)
|
||||||
if err := row.Scan(&user.ID, &password, &user.Admin, &nick, &realname, &user.Enabled, &user.DownstreamInteractedAt); err != nil {
|
if err := row.Scan(&user.ID, &password, &user.Admin, &nick, &realname, &user.Enabled, &downstreamInteractedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user.Password = password.String
|
user.Password = password.String
|
||||||
user.Nick = nick.String
|
user.Nick = nick.String
|
||||||
user.Realname = realname.String
|
user.Realname = realname.String
|
||||||
|
user.DownstreamInteractedAt = downstreamInteractedAt.Time
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,6 +392,7 @@ func (db *PostgresDB) StoreUser(ctx context.Context, user *User) error {
|
|||||||
password := toNullString(user.Password)
|
password := toNullString(user.Password)
|
||||||
nick := toNullString(user.Nick)
|
nick := toNullString(user.Nick)
|
||||||
realname := toNullString(user.Realname)
|
realname := toNullString(user.Realname)
|
||||||
|
downstreamInteractedAt := toNullTime(user.DownstreamInteractedAt)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if user.ID == 0 {
|
if user.ID == 0 {
|
||||||
@ -397,7 +402,7 @@ func (db *PostgresDB) StoreUser(ctx context.Context, user *User) error {
|
|||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
RETURNING id`,
|
RETURNING id`,
|
||||||
user.Username, password, user.Admin, nick, realname, user.Enabled,
|
user.Username, password, user.Admin, nick, realname, user.Enabled,
|
||||||
user.DownstreamInteractedAt).Scan(&user.ID)
|
downstreamInteractedAt).Scan(&user.ID)
|
||||||
} else {
|
} else {
|
||||||
_, err = db.db.ExecContext(ctx, `
|
_, err = db.db.ExecContext(ctx, `
|
||||||
UPDATE "User"
|
UPDATE "User"
|
||||||
@ -405,7 +410,7 @@ func (db *PostgresDB) StoreUser(ctx context.Context, user *User) error {
|
|||||||
enabled = $5, downstream_interacted_at = $6
|
enabled = $5, downstream_interacted_at = $6
|
||||||
WHERE id = $7`,
|
WHERE id = $7`,
|
||||||
password, user.Admin, nick, realname, user.Enabled,
|
password, user.Admin, nick, realname, user.Enabled,
|
||||||
user.DownstreamInteractedAt, user.ID)
|
downstreamInteractedAt, user.ID)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user