fix: create storage without some attributes (#2358)

pull/2363/head
Athurg Gooth 2 years ago committed by GitHub
parent 7680be1a2f
commit 287f1beb90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,8 +8,18 @@ import (
) )
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) { func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
stmt := "INSERT INTO `storage` (`name`, `type`, `config`) VALUES (?, ?, ?)" fields := []string{"`name`", "`type`", "`config`"}
result, err := d.db.ExecContext(ctx, stmt, create.Name, create.Type, create.Config) placeholder := []string{"?", "?", "?"}
args := []any{create.Name, create.Type, create.Config}
if create.ID != 0 {
fields = append(fields, "`id`")
placeholder = append(placeholder, "?")
args = append(args, create.ID)
}
stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -8,16 +8,18 @@ import (
) )
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) { func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
stmt := ` fields := []string{"`name`", "`type`", "`config`"}
INSERT INTO storage ( placeholder := []string{"?", "?", "?"}
name, args := []any{create.Name, create.Type, create.Config}
type,
config if create.ID != 0 {
) fields = append(fields, "`id`")
VALUES (?, ?, ?) placeholder = append(placeholder, "?")
RETURNING id args = append(args, create.ID)
` }
if err := d.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
stmt := "INSERT INTO `storage` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID, &create.ID,
); err != nil { ); err != nil {
return nil, err return nil, err

Loading…
Cancel
Save