mirror of https://github.com/usememos/memos.git
refactor(db): rename tables for clarity - resource→attachment, system_setting→instance_setting
This commit is contained in:
parent
01669685b4
commit
d326c71078
|
|
@ -1,11 +1,11 @@
|
|||
package email
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
|
|
@ -106,34 +106,22 @@ func TestSendAsyncConcurrent(t *testing.T) {
|
|||
FromEmail: "test@example.com",
|
||||
}
|
||||
|
||||
// Send multiple emails concurrently
|
||||
var wg sync.WaitGroup
|
||||
g := errgroup.Group{}
|
||||
count := 5
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
g.Go(func() error {
|
||||
message := &Message{
|
||||
To: []string{"recipient@example.com"},
|
||||
Subject: "Concurrent Test",
|
||||
Body: "Test body",
|
||||
}
|
||||
SendAsync(config, message)
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Should complete without deadlock
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
done <- true
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// Success
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("SendAsync calls did not complete in time")
|
||||
if err := g.Wait(); err != nil {
|
||||
t.Fatalf("SendAsync calls failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -256,7 +256,7 @@ func NewAttachmentSchema() Schema {
|
|||
Name: "filename",
|
||||
Kind: FieldKindScalar,
|
||||
Type: FieldTypeString,
|
||||
Column: Column{Table: "resource", Name: "filename"},
|
||||
Column: Column{Table: "attachment", Name: "filename"},
|
||||
SupportsContains: true,
|
||||
Expressions: map[DialectName]string{},
|
||||
},
|
||||
|
|
@ -264,14 +264,14 @@ func NewAttachmentSchema() Schema {
|
|||
Name: "mime_type",
|
||||
Kind: FieldKindScalar,
|
||||
Type: FieldTypeString,
|
||||
Column: Column{Table: "resource", Name: "type"},
|
||||
Column: Column{Table: "attachment", Name: "type"},
|
||||
Expressions: map[DialectName]string{},
|
||||
},
|
||||
"create_time": {
|
||||
Name: "create_time",
|
||||
Kind: FieldKindScalar,
|
||||
Type: FieldTypeTimestamp,
|
||||
Column: Column{Table: "resource", Name: "created_ts"},
|
||||
Column: Column{Table: "attachment", Name: "created_ts"},
|
||||
Expressions: map[DialectName]string{
|
||||
// MySQL stores created_ts as TIMESTAMP, needs conversion to epoch
|
||||
DialectMySQL: "UNIX_TIMESTAMP(%s)",
|
||||
|
|
@ -284,7 +284,7 @@ func NewAttachmentSchema() Schema {
|
|||
Name: "memo_id",
|
||||
Kind: FieldKindScalar,
|
||||
Type: FieldTypeInt,
|
||||
Column: Column{Table: "resource", Name: "memo_id"},
|
||||
Column: Column{Table: "attachment", Name: "memo_id"},
|
||||
Expressions: map[DialectName]string{},
|
||||
AllowedComparisonOps: map[ComparisonOperator]bool{
|
||||
CompareEq: true,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func (d *DB) CreateAttachment(ctx context.Context, create *store.Attachment) (*s
|
|||
}
|
||||
args := []any{create.UID, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID, create.MemoID, storageType, create.Reference, payloadString}
|
||||
|
||||
stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
|
||||
stmt := "INSERT INTO `attachment` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -50,38 +50,38 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "`resource`.`id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`id` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.UID; v != nil {
|
||||
where, args = append(where, "`resource`.`uid` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`uid` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "`resource`.`creator_id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`creator_id` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Filename; v != nil {
|
||||
where, args = append(where, "`resource`.`filename` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`filename` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.FilenameSearch; v != nil {
|
||||
where, args = append(where, "`resource`.`filename` LIKE ?"), append(args, "%"+*v+"%")
|
||||
where, args = append(where, "`attachment`.`filename` LIKE ?"), append(args, "%"+*v+"%")
|
||||
}
|
||||
if v := find.MemoID; v != nil {
|
||||
where, args = append(where, "`resource`.`memo_id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`memo_id` = ?"), append(args, *v)
|
||||
}
|
||||
if len(find.MemoIDList) > 0 {
|
||||
placeholders := make([]string, 0, len(find.MemoIDList))
|
||||
for range find.MemoIDList {
|
||||
placeholders = append(placeholders, "?")
|
||||
}
|
||||
where = append(where, "`resource`.`memo_id` IN ("+strings.Join(placeholders, ",")+")")
|
||||
where = append(where, "`attachment`.`memo_id` IN ("+strings.Join(placeholders, ",")+")")
|
||||
for _, id := range find.MemoIDList {
|
||||
args = append(args, id)
|
||||
}
|
||||
}
|
||||
if find.HasRelatedMemo {
|
||||
where = append(where, "`resource`.`memo_id` IS NOT NULL")
|
||||
where = append(where, "`attachment`.`memo_id` IS NOT NULL")
|
||||
}
|
||||
if find.StorageType != nil {
|
||||
where, args = append(where, "`resource`.`storage_type` = ?"), append(args, find.StorageType.String())
|
||||
where, args = append(where, "`attachment`.`storage_type` = ?"), append(args, find.StorageType.String())
|
||||
}
|
||||
|
||||
if len(find.Filters) > 0 {
|
||||
|
|
@ -95,26 +95,26 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
}
|
||||
|
||||
fields := []string{
|
||||
"`resource`.`id` AS `id`",
|
||||
"`resource`.`uid` AS `uid`",
|
||||
"`resource`.`filename` AS `filename`",
|
||||
"`resource`.`type` AS `type`",
|
||||
"`resource`.`size` AS `size`",
|
||||
"`resource`.`creator_id` AS `creator_id`",
|
||||
"UNIX_TIMESTAMP(`resource`.`created_ts`) AS `created_ts`",
|
||||
"UNIX_TIMESTAMP(`resource`.`updated_ts`) AS `updated_ts`",
|
||||
"`resource`.`memo_id` AS `memo_id`",
|
||||
"`resource`.`storage_type` AS `storage_type`",
|
||||
"`resource`.`reference` AS `reference`",
|
||||
"`resource`.`payload` AS `payload`",
|
||||
"`attachment`.`id` AS `id`",
|
||||
"`attachment`.`uid` AS `uid`",
|
||||
"`attachment`.`filename` AS `filename`",
|
||||
"`attachment`.`type` AS `type`",
|
||||
"`attachment`.`size` AS `size`",
|
||||
"`attachment`.`creator_id` AS `creator_id`",
|
||||
"UNIX_TIMESTAMP(`attachment`.`created_ts`) AS `created_ts`",
|
||||
"UNIX_TIMESTAMP(`attachment`.`updated_ts`) AS `updated_ts`",
|
||||
"`attachment`.`memo_id` AS `memo_id`",
|
||||
"`attachment`.`storage_type` AS `storage_type`",
|
||||
"`attachment`.`reference` AS `reference`",
|
||||
"`attachment`.`payload` AS `payload`",
|
||||
"CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`",
|
||||
}
|
||||
if find.GetBlob {
|
||||
fields = append(fields, "`resource`.`blob` AS `blob`")
|
||||
fields = append(fields, "`attachment`.`blob` AS `blob`")
|
||||
}
|
||||
|
||||
query := "SELECT " + strings.Join(fields, ", ") + " FROM `resource`" + " " +
|
||||
"LEFT JOIN `memo` ON `resource`.`memo_id` = `memo`.`id`" + " " +
|
||||
query := "SELECT " + strings.Join(fields, ", ") + " FROM `attachment`" + " " +
|
||||
"LEFT JOIN `memo` ON `attachment`.`memo_id` = `memo`.`id`" + " " +
|
||||
"WHERE " + strings.Join(where, " AND ") + " " +
|
||||
"ORDER BY `updated_ts` DESC"
|
||||
if find.Limit != nil {
|
||||
|
|
@ -216,7 +216,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
}
|
||||
|
||||
args = append(args, update.ID)
|
||||
stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
||||
stmt := "UPDATE `attachment` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -228,7 +228,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
}
|
||||
|
||||
func (d *DB) DeleteAttachment(ctx context.Context, delete *store.DeleteAttachment) error {
|
||||
stmt := "DELETE FROM `resource` WHERE `id` = ?"
|
||||
stmt := "DELETE FROM `attachment` WHERE `id` = ?"
|
||||
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) {
|
||||
stmt := "INSERT INTO `system_setting` (`name`, `value`, `description`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?, `description` = ?"
|
||||
stmt := "INSERT INTO `instance_setting` (`name`, `value`, `description`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?, `description` = ?"
|
||||
_, err := d.db.ExecContext(
|
||||
ctx,
|
||||
stmt,
|
||||
|
|
@ -31,7 +31,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
where, args = append(where, "`name` = ?"), append(args, find.Name)
|
||||
}
|
||||
|
||||
query := "SELECT `name`, `value`, `description` FROM `system_setting` WHERE " + strings.Join(where, " AND ")
|
||||
query := "SELECT `name`, `value`, `description` FROM `instance_setting` WHERE " + strings.Join(where, " AND ")
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -59,7 +59,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
}
|
||||
|
||||
func (d *DB) DeleteInstanceSetting(ctx context.Context, delete *store.DeleteInstanceSetting) error {
|
||||
stmt := "DELETE FROM `system_setting` WHERE `name` = ?"
|
||||
stmt := "DELETE FROM `instance_setting` WHERE `name` = ?"
|
||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ func (d *DB) CreateAttachment(ctx context.Context, create *store.Attachment) (*s
|
|||
}
|
||||
args := []any{create.UID, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID, create.MemoID, storageType, create.Reference, payloadString}
|
||||
|
||||
stmt := "INSERT INTO resource (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts"
|
||||
stmt := "INSERT INTO attachment (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -41,22 +41,22 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "resource.id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
where, args = append(where, "attachment.id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.UID; v != nil {
|
||||
where, args = append(where, "resource.uid = "+placeholder(len(args)+1)), append(args, *v)
|
||||
where, args = append(where, "attachment.uid = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "resource.creator_id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
where, args = append(where, "attachment.creator_id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.Filename; v != nil {
|
||||
where, args = append(where, "resource.filename = "+placeholder(len(args)+1)), append(args, *v)
|
||||
where, args = append(where, "attachment.filename = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if v := find.FilenameSearch; v != nil {
|
||||
where, args = append(where, "resource.filename LIKE "+placeholder(len(args)+1)), append(args, fmt.Sprintf("%%%s%%", *v))
|
||||
where, args = append(where, "attachment.filename LIKE "+placeholder(len(args)+1)), append(args, fmt.Sprintf("%%%s%%", *v))
|
||||
}
|
||||
if v := find.MemoID; v != nil {
|
||||
where, args = append(where, "resource.memo_id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
where, args = append(where, "attachment.memo_id = "+placeholder(len(args)+1)), append(args, *v)
|
||||
}
|
||||
if len(find.MemoIDList) > 0 {
|
||||
holders := make([]string, 0, len(find.MemoIDList))
|
||||
|
|
@ -64,13 +64,13 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
holders = append(holders, placeholder(len(args)+1))
|
||||
args = append(args, id)
|
||||
}
|
||||
where = append(where, "resource.memo_id IN ("+strings.Join(holders, ", ")+")")
|
||||
where = append(where, "attachment.memo_id IN ("+strings.Join(holders, ", ")+")")
|
||||
}
|
||||
if find.HasRelatedMemo {
|
||||
where = append(where, "resource.memo_id IS NOT NULL")
|
||||
where = append(where, "attachment.memo_id IS NOT NULL")
|
||||
}
|
||||
if v := find.StorageType; v != nil {
|
||||
where, args = append(where, "resource.storage_type = "+placeholder(len(args)+1)), append(args, v.String())
|
||||
where, args = append(where, "attachment.storage_type = "+placeholder(len(args)+1)), append(args, v.String())
|
||||
}
|
||||
|
||||
if len(find.Filters) > 0 {
|
||||
|
|
@ -84,31 +84,31 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
}
|
||||
|
||||
fields := []string{
|
||||
"resource.id AS id",
|
||||
"resource.uid AS uid",
|
||||
"resource.filename AS filename",
|
||||
"resource.type AS type",
|
||||
"resource.size AS size",
|
||||
"resource.creator_id AS creator_id",
|
||||
"resource.created_ts AS created_ts",
|
||||
"resource.updated_ts AS updated_ts",
|
||||
"resource.memo_id AS memo_id",
|
||||
"resource.storage_type AS storage_type",
|
||||
"resource.reference AS reference",
|
||||
"resource.payload AS payload",
|
||||
"attachment.id AS id",
|
||||
"attachment.uid AS uid",
|
||||
"attachment.filename AS filename",
|
||||
"attachment.type AS type",
|
||||
"attachment.size AS size",
|
||||
"attachment.creator_id AS creator_id",
|
||||
"attachment.created_ts AS created_ts",
|
||||
"attachment.updated_ts AS updated_ts",
|
||||
"attachment.memo_id AS memo_id",
|
||||
"attachment.storage_type AS storage_type",
|
||||
"attachment.reference AS reference",
|
||||
"attachment.payload AS payload",
|
||||
"CASE WHEN memo.uid IS NOT NULL THEN memo.uid ELSE NULL END AS memo_uid",
|
||||
}
|
||||
if find.GetBlob {
|
||||
fields = append(fields, "resource.blob AS blob")
|
||||
fields = append(fields, "attachment.blob AS blob")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
%s
|
||||
FROM resource
|
||||
LEFT JOIN memo ON resource.memo_id = memo.id
|
||||
FROM attachment
|
||||
LEFT JOIN memo ON attachment.memo_id = memo.id
|
||||
WHERE %s
|
||||
ORDER BY resource.updated_ts DESC
|
||||
ORDER BY attachment.updated_ts DESC
|
||||
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
|
||||
if find.Limit != nil {
|
||||
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
||||
|
|
@ -196,7 +196,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
set, args = append(set, "payload = "+placeholder(len(args)+1)), append(args, string(bytes))
|
||||
}
|
||||
|
||||
stmt := `UPDATE resource SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1)
|
||||
stmt := `UPDATE attachment SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1)
|
||||
args = append(args, update.ID)
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
|
|
@ -209,7 +209,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
}
|
||||
|
||||
func (d *DB) DeleteAttachment(ctx context.Context, delete *store.DeleteAttachment) error {
|
||||
stmt := `DELETE FROM resource WHERE id = $1`
|
||||
stmt := `DELETE FROM attachment WHERE id = $1`
|
||||
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO system_setting (
|
||||
INSERT INTO instance_setting (
|
||||
name, value, description
|
||||
)
|
||||
VALUES ($1, $2, $3)
|
||||
|
|
@ -36,7 +36,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
name,
|
||||
value,
|
||||
description
|
||||
FROM system_setting
|
||||
FROM instance_setting
|
||||
WHERE ` + strings.Join(where, " AND ")
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
|
|
@ -66,7 +66,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
}
|
||||
|
||||
func (d *DB) DeleteInstanceSetting(ctx context.Context, delete *store.DeleteInstanceSetting) error {
|
||||
stmt := `DELETE FROM system_setting WHERE name = $1`
|
||||
stmt := `DELETE FROM instance_setting WHERE name = $1`
|
||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func (d *DB) CreateAttachment(ctx context.Context, create *store.Attachment) (*s
|
|||
}
|
||||
args := []any{create.UID, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID, create.MemoID, storageType, create.Reference, payloadString}
|
||||
|
||||
stmt := "INSERT INTO `resource` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
|
||||
stmt := "INSERT INTO `attachment` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -43,38 +43,38 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "`resource`.`id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`id` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.UID; v != nil {
|
||||
where, args = append(where, "`resource`.`uid` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`uid` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "`resource`.`creator_id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`creator_id` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Filename; v != nil {
|
||||
where, args = append(where, "`resource`.`filename` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`filename` = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.FilenameSearch; v != nil {
|
||||
where, args = append(where, "`resource`.`filename` LIKE ?"), append(args, fmt.Sprintf("%%%s%%", *v))
|
||||
where, args = append(where, "`attachment`.`filename` LIKE ?"), append(args, fmt.Sprintf("%%%s%%", *v))
|
||||
}
|
||||
if v := find.MemoID; v != nil {
|
||||
where, args = append(where, "`resource`.`memo_id` = ?"), append(args, *v)
|
||||
where, args = append(where, "`attachment`.`memo_id` = ?"), append(args, *v)
|
||||
}
|
||||
if len(find.MemoIDList) > 0 {
|
||||
placeholders := make([]string, 0, len(find.MemoIDList))
|
||||
for range find.MemoIDList {
|
||||
placeholders = append(placeholders, "?")
|
||||
}
|
||||
where = append(where, "`resource`.`memo_id` IN ("+strings.Join(placeholders, ",")+")")
|
||||
where = append(where, "`attachment`.`memo_id` IN ("+strings.Join(placeholders, ",")+")")
|
||||
for _, id := range find.MemoIDList {
|
||||
args = append(args, id)
|
||||
}
|
||||
}
|
||||
if find.HasRelatedMemo {
|
||||
where = append(where, "`resource`.`memo_id` IS NOT NULL")
|
||||
where = append(where, "`attachment`.`memo_id` IS NOT NULL")
|
||||
}
|
||||
if find.StorageType != nil {
|
||||
where, args = append(where, "`resource`.`storage_type` = ?"), append(args, find.StorageType.String())
|
||||
where, args = append(where, "`attachment`.`storage_type` = ?"), append(args, find.StorageType.String())
|
||||
}
|
||||
|
||||
if len(find.Filters) > 0 {
|
||||
|
|
@ -88,28 +88,28 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
|
|||
}
|
||||
|
||||
fields := []string{
|
||||
"`resource`.`id` AS `id`",
|
||||
"`resource`.`uid` AS `uid`",
|
||||
"`resource`.`filename` AS `filename`",
|
||||
"`resource`.`type` AS `type`",
|
||||
"`resource`.`size` AS `size`",
|
||||
"`resource`.`creator_id` AS `creator_id`",
|
||||
"`resource`.`created_ts` AS `created_ts`",
|
||||
"`resource`.`updated_ts` AS `updated_ts`",
|
||||
"`resource`.`memo_id` AS `memo_id`",
|
||||
"`resource`.`storage_type` AS `storage_type`",
|
||||
"`resource`.`reference` AS `reference`",
|
||||
"`resource`.`payload` AS `payload`",
|
||||
"`attachment`.`id` AS `id`",
|
||||
"`attachment`.`uid` AS `uid`",
|
||||
"`attachment`.`filename` AS `filename`",
|
||||
"`attachment`.`type` AS `type`",
|
||||
"`attachment`.`size` AS `size`",
|
||||
"`attachment`.`creator_id` AS `creator_id`",
|
||||
"`attachment`.`created_ts` AS `created_ts`",
|
||||
"`attachment`.`updated_ts` AS `updated_ts`",
|
||||
"`attachment`.`memo_id` AS `memo_id`",
|
||||
"`attachment`.`storage_type` AS `storage_type`",
|
||||
"`attachment`.`reference` AS `reference`",
|
||||
"`attachment`.`payload` AS `payload`",
|
||||
"CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`",
|
||||
}
|
||||
if find.GetBlob {
|
||||
fields = append(fields, "`resource`.`blob` AS `blob`")
|
||||
fields = append(fields, "`attachment`.`blob` AS `blob`")
|
||||
}
|
||||
|
||||
query := "SELECT " + strings.Join(fields, ", ") + " FROM `resource`" + " " +
|
||||
"LEFT JOIN `memo` ON `resource`.`memo_id` = `memo`.`id`" + " " +
|
||||
query := "SELECT " + strings.Join(fields, ", ") + " FROM `attachment`" + " " +
|
||||
"LEFT JOIN `memo` ON `attachment`.`memo_id` = `memo`.`id`" + " " +
|
||||
"WHERE " + strings.Join(where, " AND ") + " " +
|
||||
"ORDER BY `resource`.`updated_ts` DESC"
|
||||
"ORDER BY `attachment`.`updated_ts` DESC"
|
||||
if find.Limit != nil {
|
||||
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
||||
if find.Offset != nil {
|
||||
|
|
@ -197,7 +197,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
}
|
||||
|
||||
args = append(args, update.ID)
|
||||
stmt := "UPDATE `resource` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
||||
stmt := "UPDATE `attachment` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to update attachment")
|
||||
|
|
@ -209,7 +209,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
|
|||
}
|
||||
|
||||
func (d *DB) DeleteAttachment(ctx context.Context, delete *store.DeleteAttachment) error {
|
||||
stmt := "DELETE FROM `resource` WHERE `id` = ?"
|
||||
stmt := "DELETE FROM `attachment` WHERE `id` = ?"
|
||||
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO system_setting (
|
||||
INSERT INTO instance_setting (
|
||||
name, value, description
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
|
|
@ -36,7 +36,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
name,
|
||||
value,
|
||||
description
|
||||
FROM system_setting
|
||||
FROM instance_setting
|
||||
WHERE ` + strings.Join(where, " AND ")
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
|
|
@ -66,7 +66,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
|
|||
}
|
||||
|
||||
func (d *DB) DeleteInstanceSetting(ctx context.Context, delete *store.DeleteInstanceSetting) error {
|
||||
stmt := "DELETE FROM system_setting WHERE name = ?"
|
||||
stmt := "DELETE FROM instance_setting WHERE name = ?"
|
||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
RENAME TABLE resource TO attachment;
|
||||
|
|
@ -0,0 +1 @@
|
|||
RENAME TABLE system_setting TO instance_setting;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
-- system_setting
|
||||
CREATE TABLE `system_setting` (
|
||||
-- instance_setting
|
||||
CREATE TABLE `instance_setting` (
|
||||
`name` VARCHAR(256) NOT NULL PRIMARY KEY,
|
||||
`value` LONGTEXT NOT NULL,
|
||||
`description` TEXT NOT NULL
|
||||
|
|
@ -58,8 +58,8 @@ CREATE TABLE `memo_relation` (
|
|||
UNIQUE(`memo_id`,`related_memo_id`,`type`)
|
||||
);
|
||||
|
||||
-- resource
|
||||
CREATE TABLE `resource` (
|
||||
-- attachment
|
||||
CREATE TABLE `attachment` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`uid` VARCHAR(256) NOT NULL UNIQUE,
|
||||
`creator_id` INT NOT NULL,
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE resource RENAME TO attachment;
|
||||
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE system_setting RENAME TO instance_setting;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
-- system_setting
|
||||
CREATE TABLE system_setting (
|
||||
-- instance_setting
|
||||
CREATE TABLE instance_setting (
|
||||
name TEXT NOT NULL PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
description TEXT NOT NULL
|
||||
|
|
@ -58,8 +58,8 @@ CREATE TABLE memo_relation (
|
|||
UNIQUE(memo_id, related_memo_id, type)
|
||||
);
|
||||
|
||||
-- resource
|
||||
CREATE TABLE resource (
|
||||
-- attachment
|
||||
CREATE TABLE attachment (
|
||||
id SERIAL PRIMARY KEY,
|
||||
uid TEXT NOT NULL UNIQUE,
|
||||
creator_id INTEGER NOT NULL,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE `resource` RENAME TO `attachment`;
|
||||
DROP INDEX IF EXISTS `idx_resource_creator_id`;
|
||||
CREATE INDEX `idx_attachment_creator_id` ON `attachment` (`creator_id`);
|
||||
DROP INDEX IF EXISTS `idx_resource_memo_id`;
|
||||
CREATE INDEX `idx_attachment_memo_id` ON `attachment` (`memo_id`);
|
||||
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE `system_setting` RENAME TO `instance_setting`;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
-- system_setting
|
||||
CREATE TABLE system_setting (
|
||||
-- instance_setting
|
||||
CREATE TABLE instance_setting (
|
||||
name TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
|
|
@ -63,8 +63,8 @@ CREATE TABLE memo_relation (
|
|||
UNIQUE(memo_id, related_memo_id, type)
|
||||
);
|
||||
|
||||
-- resource
|
||||
CREATE TABLE resource (
|
||||
-- attachment
|
||||
CREATE TABLE attachment (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
uid TEXT NOT NULL UNIQUE,
|
||||
creator_id INTEGER NOT NULL,
|
||||
|
|
@ -80,9 +80,9 @@ CREATE TABLE resource (
|
|||
payload TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
CREATE INDEX idx_resource_creator_id ON resource (creator_id);
|
||||
CREATE INDEX idx_attachment_creator_id ON attachment (creator_id);
|
||||
|
||||
CREATE INDEX idx_resource_memo_id ON resource (memo_id);
|
||||
CREATE INDEX idx_attachment_memo_id ON attachment (memo_id);
|
||||
|
||||
-- activity
|
||||
CREATE TABLE activity (
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
DELETE FROM system_setting;
|
||||
DELETE FROM user;
|
||||
DELETE FROM user_setting;
|
||||
DELETE FROM memo;
|
||||
DELETE FROM memo_organizer;
|
||||
DELETE FROM memo_relation;
|
||||
DELETE FROM resource;
|
||||
DELETE FROM activity;
|
||||
DELETE FROM idp;
|
||||
DELETE FROM inbox;
|
||||
DELETE FROM reaction;
|
||||
Loading…
Reference in New Issue