Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: create storage without some attributes (#2358)

Athurg Gooth287f1beb9025
diff --git a/store/db/mysql/storage.go b/store/db/mysql/storage.go
index 1fd9cb9e5..36507ee65 100644
--- a/store/db/mysql/storage.go
+++ b/store/db/mysql/storage.go
@@ -8,8 +8,18 @@ import (
)
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
- stmt := "INSERT INTO `storage` (`name`, `type`, `config`) VALUES (?, ?, ?)"
- result, err := d.db.ExecContext(ctx, stmt, create.Name, create.Type, create.Config)
+ fields := []string{"`name`", "`type`", "`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 {
return nil, err
}
diff --git a/store/db/sqlite/storage.go b/store/db/sqlite/storage.go
index 705a4531b..5d4d009af 100644
--- a/store/db/sqlite/storage.go
+++ b/store/db/sqlite/storage.go
@@ -8,16 +8,18 @@ import (
)
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
- stmt := `
- INSERT INTO storage (
- name,
- type,
- config
- )
- VALUES (?, ?, ?)
- RETURNING id
- `
- if err := d.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
+ fields := []string{"`name`", "`type`", "`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, ", ") + ") RETURNING `id`"
+ if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
); err != nil {
return nil, err