refactor(db): rename tables for clarity - resource→attachment, system_setting→instance_setting

This commit is contained in:
Johnny 2026-01-06 23:36:42 +08:00
parent 01669685b4
commit d326c71078
18 changed files with 127 additions and 140 deletions

View File

@ -1,11 +1,11 @@
package email package email
import ( import (
"sync"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
) )
func TestSend(t *testing.T) { func TestSend(t *testing.T) {
@ -106,34 +106,22 @@ func TestSendAsyncConcurrent(t *testing.T) {
FromEmail: "test@example.com", FromEmail: "test@example.com",
} }
// Send multiple emails concurrently g := errgroup.Group{}
var wg sync.WaitGroup
count := 5 count := 5
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
wg.Add(1) g.Go(func() error {
go func() {
defer wg.Done()
message := &Message{ message := &Message{
To: []string{"recipient@example.com"}, To: []string{"recipient@example.com"},
Subject: "Concurrent Test", Subject: "Concurrent Test",
Body: "Test body", Body: "Test body",
} }
SendAsync(config, message) SendAsync(config, message)
}() return nil
})
} }
// Should complete without deadlock if err := g.Wait(); err != nil {
done := make(chan bool) t.Fatalf("SendAsync calls failed: %v", err)
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")
} }
} }

View File

@ -256,7 +256,7 @@ func NewAttachmentSchema() Schema {
Name: "filename", Name: "filename",
Kind: FieldKindScalar, Kind: FieldKindScalar,
Type: FieldTypeString, Type: FieldTypeString,
Column: Column{Table: "resource", Name: "filename"}, Column: Column{Table: "attachment", Name: "filename"},
SupportsContains: true, SupportsContains: true,
Expressions: map[DialectName]string{}, Expressions: map[DialectName]string{},
}, },
@ -264,14 +264,14 @@ func NewAttachmentSchema() Schema {
Name: "mime_type", Name: "mime_type",
Kind: FieldKindScalar, Kind: FieldKindScalar,
Type: FieldTypeString, Type: FieldTypeString,
Column: Column{Table: "resource", Name: "type"}, Column: Column{Table: "attachment", Name: "type"},
Expressions: map[DialectName]string{}, Expressions: map[DialectName]string{},
}, },
"create_time": { "create_time": {
Name: "create_time", Name: "create_time",
Kind: FieldKindScalar, Kind: FieldKindScalar,
Type: FieldTypeTimestamp, Type: FieldTypeTimestamp,
Column: Column{Table: "resource", Name: "created_ts"}, Column: Column{Table: "attachment", Name: "created_ts"},
Expressions: map[DialectName]string{ Expressions: map[DialectName]string{
// MySQL stores created_ts as TIMESTAMP, needs conversion to epoch // MySQL stores created_ts as TIMESTAMP, needs conversion to epoch
DialectMySQL: "UNIX_TIMESTAMP(%s)", DialectMySQL: "UNIX_TIMESTAMP(%s)",
@ -284,7 +284,7 @@ func NewAttachmentSchema() Schema {
Name: "memo_id", Name: "memo_id",
Kind: FieldKindScalar, Kind: FieldKindScalar,
Type: FieldTypeInt, Type: FieldTypeInt,
Column: Column{Table: "resource", Name: "memo_id"}, Column: Column{Table: "attachment", Name: "memo_id"},
Expressions: map[DialectName]string{}, Expressions: map[DialectName]string{},
AllowedComparisonOps: map[ComparisonOperator]bool{ AllowedComparisonOps: map[ComparisonOperator]bool{
CompareEq: true, CompareEq: true,

View File

@ -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} 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...) result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { if err != nil {
return nil, err return nil, err
@ -50,38 +50,38 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
where, args := []string{"1 = 1"}, []any{} where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil { 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 { 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 { 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 { 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 { 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 { 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 { if len(find.MemoIDList) > 0 {
placeholders := make([]string, 0, len(find.MemoIDList)) placeholders := make([]string, 0, len(find.MemoIDList))
for range find.MemoIDList { for range find.MemoIDList {
placeholders = append(placeholders, "?") 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 { for _, id := range find.MemoIDList {
args = append(args, id) args = append(args, id)
} }
} }
if find.HasRelatedMemo { 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 { 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 { if len(find.Filters) > 0 {
@ -95,26 +95,26 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
} }
fields := []string{ fields := []string{
"`resource`.`id` AS `id`", "`attachment`.`id` AS `id`",
"`resource`.`uid` AS `uid`", "`attachment`.`uid` AS `uid`",
"`resource`.`filename` AS `filename`", "`attachment`.`filename` AS `filename`",
"`resource`.`type` AS `type`", "`attachment`.`type` AS `type`",
"`resource`.`size` AS `size`", "`attachment`.`size` AS `size`",
"`resource`.`creator_id` AS `creator_id`", "`attachment`.`creator_id` AS `creator_id`",
"UNIX_TIMESTAMP(`resource`.`created_ts`) AS `created_ts`", "UNIX_TIMESTAMP(`attachment`.`created_ts`) AS `created_ts`",
"UNIX_TIMESTAMP(`resource`.`updated_ts`) AS `updated_ts`", "UNIX_TIMESTAMP(`attachment`.`updated_ts`) AS `updated_ts`",
"`resource`.`memo_id` AS `memo_id`", "`attachment`.`memo_id` AS `memo_id`",
"`resource`.`storage_type` AS `storage_type`", "`attachment`.`storage_type` AS `storage_type`",
"`resource`.`reference` AS `reference`", "`attachment`.`reference` AS `reference`",
"`resource`.`payload` AS `payload`", "`attachment`.`payload` AS `payload`",
"CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`", "CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`",
} }
if find.GetBlob { if find.GetBlob {
fields = append(fields, "`resource`.`blob` AS `blob`") fields = append(fields, "`attachment`.`blob` AS `blob`")
} }
query := "SELECT " + strings.Join(fields, ", ") + " FROM `resource`" + " " + query := "SELECT " + strings.Join(fields, ", ") + " FROM `attachment`" + " " +
"LEFT JOIN `memo` ON `resource`.`memo_id` = `memo`.`id`" + " " + "LEFT JOIN `memo` ON `attachment`.`memo_id` = `memo`.`id`" + " " +
"WHERE " + strings.Join(where, " AND ") + " " + "WHERE " + strings.Join(where, " AND ") + " " +
"ORDER BY `updated_ts` DESC" "ORDER BY `updated_ts` DESC"
if find.Limit != nil { if find.Limit != nil {
@ -216,7 +216,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
} }
args = append(args, update.ID) 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...) result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { if err != nil {
return err 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 { 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) result, err := d.db.ExecContext(ctx, stmt, delete.ID)
if err != nil { if err != nil {
return err return err

View File

@ -8,7 +8,7 @@ import (
) )
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { 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( _, err := d.db.ExecContext(
ctx, ctx,
stmt, stmt,
@ -31,7 +31,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
where, args = append(where, "`name` = ?"), append(args, find.Name) 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...) rows, err := d.db.QueryContext(ctx, query, args...)
if err != nil { if err != nil {
return nil, err 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 { 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) _, err := d.db.ExecContext(ctx, stmt, delete.Name)
return err return err
} }

View File

@ -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} 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 { if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
return nil, err return nil, err
} }
@ -41,22 +41,22 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
where, args := []string{"1 = 1"}, []any{} where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil { 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 { 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 { 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 { 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 { 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 { 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 { if len(find.MemoIDList) > 0 {
holders := make([]string, 0, len(find.MemoIDList)) 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)) holders = append(holders, placeholder(len(args)+1))
args = append(args, id) 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 { 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 { 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 { if len(find.Filters) > 0 {
@ -84,31 +84,31 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
} }
fields := []string{ fields := []string{
"resource.id AS id", "attachment.id AS id",
"resource.uid AS uid", "attachment.uid AS uid",
"resource.filename AS filename", "attachment.filename AS filename",
"resource.type AS type", "attachment.type AS type",
"resource.size AS size", "attachment.size AS size",
"resource.creator_id AS creator_id", "attachment.creator_id AS creator_id",
"resource.created_ts AS created_ts", "attachment.created_ts AS created_ts",
"resource.updated_ts AS updated_ts", "attachment.updated_ts AS updated_ts",
"resource.memo_id AS memo_id", "attachment.memo_id AS memo_id",
"resource.storage_type AS storage_type", "attachment.storage_type AS storage_type",
"resource.reference AS reference", "attachment.reference AS reference",
"resource.payload AS payload", "attachment.payload AS payload",
"CASE WHEN memo.uid IS NOT NULL THEN memo.uid ELSE NULL END AS memo_uid", "CASE WHEN memo.uid IS NOT NULL THEN memo.uid ELSE NULL END AS memo_uid",
} }
if find.GetBlob { if find.GetBlob {
fields = append(fields, "resource.blob AS blob") fields = append(fields, "attachment.blob AS blob")
} }
query := fmt.Sprintf(` query := fmt.Sprintf(`
SELECT SELECT
%s %s
FROM resource FROM attachment
LEFT JOIN memo ON resource.memo_id = memo.id LEFT JOIN memo ON attachment.memo_id = memo.id
WHERE %s WHERE %s
ORDER BY resource.updated_ts DESC ORDER BY attachment.updated_ts DESC
`, strings.Join(fields, ", "), strings.Join(where, " AND ")) `, strings.Join(fields, ", "), strings.Join(where, " AND "))
if find.Limit != nil { if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit) 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)) 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) args = append(args, update.ID)
result, err := d.db.ExecContext(ctx, stmt, args...) result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { 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 { 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) result, err := d.db.ExecContext(ctx, stmt, delete.ID)
if err != nil { if err != nil {
return err return err

View File

@ -9,7 +9,7 @@ import (
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) {
stmt := ` stmt := `
INSERT INTO system_setting ( INSERT INTO instance_setting (
name, value, description name, value, description
) )
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
@ -36,7 +36,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
name, name,
value, value,
description description
FROM system_setting FROM instance_setting
WHERE ` + strings.Join(where, " AND ") WHERE ` + strings.Join(where, " AND ")
rows, err := d.db.QueryContext(ctx, query, args...) 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 { 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) _, err := d.db.ExecContext(ctx, stmt, delete.Name)
return err return err
} }

View File

@ -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} 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 { if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
return nil, err return nil, err
} }
@ -43,38 +43,38 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
where, args := []string{"1 = 1"}, []any{} where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil { 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 { 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 { 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 { 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 { 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 { 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 { if len(find.MemoIDList) > 0 {
placeholders := make([]string, 0, len(find.MemoIDList)) placeholders := make([]string, 0, len(find.MemoIDList))
for range find.MemoIDList { for range find.MemoIDList {
placeholders = append(placeholders, "?") 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 { for _, id := range find.MemoIDList {
args = append(args, id) args = append(args, id)
} }
} }
if find.HasRelatedMemo { 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 { 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 { if len(find.Filters) > 0 {
@ -88,28 +88,28 @@ func (d *DB) ListAttachments(ctx context.Context, find *store.FindAttachment) ([
} }
fields := []string{ fields := []string{
"`resource`.`id` AS `id`", "`attachment`.`id` AS `id`",
"`resource`.`uid` AS `uid`", "`attachment`.`uid` AS `uid`",
"`resource`.`filename` AS `filename`", "`attachment`.`filename` AS `filename`",
"`resource`.`type` AS `type`", "`attachment`.`type` AS `type`",
"`resource`.`size` AS `size`", "`attachment`.`size` AS `size`",
"`resource`.`creator_id` AS `creator_id`", "`attachment`.`creator_id` AS `creator_id`",
"`resource`.`created_ts` AS `created_ts`", "`attachment`.`created_ts` AS `created_ts`",
"`resource`.`updated_ts` AS `updated_ts`", "`attachment`.`updated_ts` AS `updated_ts`",
"`resource`.`memo_id` AS `memo_id`", "`attachment`.`memo_id` AS `memo_id`",
"`resource`.`storage_type` AS `storage_type`", "`attachment`.`storage_type` AS `storage_type`",
"`resource`.`reference` AS `reference`", "`attachment`.`reference` AS `reference`",
"`resource`.`payload` AS `payload`", "`attachment`.`payload` AS `payload`",
"CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`", "CASE WHEN `memo`.`uid` IS NOT NULL THEN `memo`.`uid` ELSE NULL END AS `memo_uid`",
} }
if find.GetBlob { if find.GetBlob {
fields = append(fields, "`resource`.`blob` AS `blob`") fields = append(fields, "`attachment`.`blob` AS `blob`")
} }
query := "SELECT " + strings.Join(fields, ", ") + " FROM `resource`" + " " + query := "SELECT " + strings.Join(fields, ", ") + " FROM `attachment`" + " " +
"LEFT JOIN `memo` ON `resource`.`memo_id` = `memo`.`id`" + " " + "LEFT JOIN `memo` ON `attachment`.`memo_id` = `memo`.`id`" + " " +
"WHERE " + strings.Join(where, " AND ") + " " + "WHERE " + strings.Join(where, " AND ") + " " +
"ORDER BY `resource`.`updated_ts` DESC" "ORDER BY `attachment`.`updated_ts` DESC"
if find.Limit != nil { if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit) query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
if find.Offset != nil { if find.Offset != nil {
@ -197,7 +197,7 @@ func (d *DB) UpdateAttachment(ctx context.Context, update *store.UpdateAttachmen
} }
args = append(args, update.ID) 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...) result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to update attachment") 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 { 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) result, err := d.db.ExecContext(ctx, stmt, delete.ID)
if err != nil { if err != nil {
return err return err

View File

@ -9,7 +9,7 @@ import (
func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) {
stmt := ` stmt := `
INSERT INTO system_setting ( INSERT INTO instance_setting (
name, value, description name, value, description
) )
VALUES (?, ?, ?) VALUES (?, ?, ?)
@ -36,7 +36,7 @@ func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceS
name, name,
value, value,
description description
FROM system_setting FROM instance_setting
WHERE ` + strings.Join(where, " AND ") WHERE ` + strings.Join(where, " AND ")
rows, err := d.db.QueryContext(ctx, query, args...) 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 { 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) _, err := d.db.ExecContext(ctx, stmt, delete.Name)
return err return err
} }

View File

@ -0,0 +1 @@
RENAME TABLE resource TO attachment;

View File

@ -0,0 +1 @@
RENAME TABLE system_setting TO instance_setting;

View File

@ -1,5 +1,5 @@
-- system_setting -- instance_setting
CREATE TABLE `system_setting` ( CREATE TABLE `instance_setting` (
`name` VARCHAR(256) NOT NULL PRIMARY KEY, `name` VARCHAR(256) NOT NULL PRIMARY KEY,
`value` LONGTEXT NOT NULL, `value` LONGTEXT NOT NULL,
`description` TEXT NOT NULL `description` TEXT NOT NULL
@ -58,8 +58,8 @@ CREATE TABLE `memo_relation` (
UNIQUE(`memo_id`,`related_memo_id`,`type`) UNIQUE(`memo_id`,`related_memo_id`,`type`)
); );
-- resource -- attachment
CREATE TABLE `resource` ( CREATE TABLE `attachment` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`uid` VARCHAR(256) NOT NULL UNIQUE, `uid` VARCHAR(256) NOT NULL UNIQUE,
`creator_id` INT NOT NULL, `creator_id` INT NOT NULL,

View File

@ -0,0 +1 @@
ALTER TABLE resource RENAME TO attachment;

View File

@ -0,0 +1 @@
ALTER TABLE system_setting RENAME TO instance_setting;

View File

@ -1,5 +1,5 @@
-- system_setting -- instance_setting
CREATE TABLE system_setting ( CREATE TABLE instance_setting (
name TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL PRIMARY KEY,
value TEXT NOT NULL, value TEXT NOT NULL,
description TEXT NOT NULL description TEXT NOT NULL
@ -58,8 +58,8 @@ CREATE TABLE memo_relation (
UNIQUE(memo_id, related_memo_id, type) UNIQUE(memo_id, related_memo_id, type)
); );
-- resource -- attachment
CREATE TABLE resource ( CREATE TABLE attachment (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
uid TEXT NOT NULL UNIQUE, uid TEXT NOT NULL UNIQUE,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,

View File

@ -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`);

View File

@ -0,0 +1 @@
ALTER TABLE `system_setting` RENAME TO `instance_setting`;

View File

@ -1,5 +1,5 @@
-- system_setting -- instance_setting
CREATE TABLE system_setting ( CREATE TABLE instance_setting (
name TEXT NOT NULL, name TEXT NOT NULL,
value TEXT NOT NULL, value TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '',
@ -63,8 +63,8 @@ CREATE TABLE memo_relation (
UNIQUE(memo_id, related_memo_id, type) UNIQUE(memo_id, related_memo_id, type)
); );
-- resource -- attachment
CREATE TABLE resource ( CREATE TABLE attachment (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT NOT NULL UNIQUE, uid TEXT NOT NULL UNIQUE,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
@ -80,9 +80,9 @@ CREATE TABLE resource (
payload TEXT NOT NULL DEFAULT '{}' 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 -- activity
CREATE TABLE activity ( CREATE TABLE activity (

View File

@ -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;