chore: adjust store test for mysql

This commit is contained in:
Steven
2023-09-29 09:15:54 +08:00
parent 255254eb69
commit 87ddeb2c79
13 changed files with 359 additions and 163 deletions

View File

@@ -3,7 +3,6 @@ package mysql
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
@@ -56,7 +55,7 @@ func (d *Driver) CreateIdentityProvider(ctx context.Context, create *store.Ident
func (d *Driver) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil {
where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
where, args = append(where, "id = ?"), append(args, *v)
}
rows, err := d.db.QueryContext(ctx, `
@@ -150,39 +149,22 @@ func (d *Driver) UpdateIdentityProvider(ctx context.Context, update *store.Updat
UPDATE idp
SET ` + strings.Join(set, ", ") + `
WHERE id = ?
RETURNING id, name, type, identifier_filter, config
`
_, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil {
return nil, err
}
var identityProvider store.IdentityProvider
var identityProviderConfig string
stmt = `SELECT id, name, type, identifier_filter, config FROM idp WHERE id = ?`
if err := d.db.QueryRowContext(ctx, stmt, update.ID).Scan(
&identityProvider.ID,
&identityProvider.Name,
&identityProvider.Type,
&identityProvider.IdentifierFilter,
&identityProviderConfig,
); err != nil {
identityProvider, err := d.GetIdentityProvider(ctx, &store.FindIdentityProvider{
ID: &update.ID,
})
if err != nil {
return nil, err
}
if identityProvider.Type == store.IdentityProviderOAuth2Type {
oauth2Config := &store.IdentityProviderOAuth2Config{}
if err := json.Unmarshal([]byte(identityProviderConfig), oauth2Config); err != nil {
return nil, err
}
identityProvider.Config = &store.IdentityProviderConfig{
OAuth2Config: oauth2Config,
}
} else {
return nil, errors.Errorf("unsupported idp type %s", string(identityProvider.Type))
if identityProvider == nil {
return nil, errors.Errorf("idp %d not found", update.ID)
}
return &identityProvider, nil
return identityProvider, nil
}
func (d *Driver) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {

View File

@@ -32,37 +32,19 @@ func (d *Driver) CreateMemo(ctx context.Context, create *store.Memo) (*store.Mem
return nil, err
}
id, err := result.LastInsertId()
rawID, err := result.LastInsertId()
if err != nil {
return nil, err
}
var memo store.Memo
stmt = `
SELECT
id,
creator_id,
content,
visibility,
UNIX_TIMESTAMP(created_ts),
UNIX_TIMESTAMP(updated_ts),
row_status
FROM memo
WHERE id = ?
`
if err := d.db.QueryRowContext(ctx, stmt, id).Scan(
&memo.ID,
&memo.CreatorID,
&memo.Content,
&memo.Visibility,
&memo.UpdatedTs,
&memo.CreatedTs,
&memo.RowStatus,
); err != nil {
id := int32(rawID)
memo, err := d.GetMemo(ctx, &store.FindMemo{ID: &id})
if err != nil {
return nil, err
}
return &memo, nil
if memo == nil {
return nil, errors.Errorf("failed to create memo")
}
return memo, nil
}
func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
@@ -211,6 +193,19 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.
return list, nil
}
func (d *Driver) GetMemo(ctx context.Context, find *store.FindMemo) (*store.Memo, error) {
list, err := d.ListMemos(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
memo := list[0]
return memo, nil
}
func (d *Driver) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
set, args := []string{}, []any{}
if v := update.CreatedTs; v != nil {

View File

@@ -1,109 +1,34 @@
-- activity
CREATE TABLE IF NOT EXISTS `activity` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type` varchar(255) NOT NULL DEFAULT '',
`level` varchar(255) NOT NULL DEFAULT 'INFO',
`payload` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `activity_chk_1` CHECK ((`level` in (_utf8mb4'INFO',_utf8mb4'WARN',_utf8mb4'ERROR')))
);
-- idp
CREATE TABLE IF NOT EXISTS `idp` (
`id` int NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`type` text NOT NULL,
`identifier_filter` varchar(256) NOT NULL DEFAULT '',
`config` text NOT NULL,
PRIMARY KEY (`id`)
);
-- memo
CREATE TABLE IF NOT EXISTS `memo` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_status` varchar(255) NOT NULL DEFAULT 'NORMAL',
`content` text NOT NULL,
`visibility` varchar(255) NOT NULL DEFAULT 'PRIVATE',
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `visibility` (`visibility`),
CONSTRAINT `memo_chk_1` CHECK ((`row_status` in (_utf8mb4'NORMAL',_utf8mb4'ARCHIVED'))),
CONSTRAINT `memo_chk_2` CHECK ((`visibility` in (_utf8mb4'PUBLIC',_utf8mb4'PROTECTED',_utf8mb4'PRIVATE')))
);
-- memo_organizer
CREATE TABLE IF NOT EXISTS `memo_organizer` (
`memo_id` int NOT NULL,
`user_id` int NOT NULL,
`pinned` int NOT NULL DEFAULT '0',
UNIQUE KEY `memo_id` (`memo_id`,`user_id`),
CONSTRAINT `memo_organizer_chk_1` CHECK ((`pinned` in (0,1)))
);
-- memo_relation
CREATE TABLE IF NOT EXISTS `memo_relation` (
`memo_id` int NOT NULL,
`related_memo_id` int NOT NULL,
`type` varchar(256) NOT NULL,
UNIQUE KEY `memo_id` (`memo_id`,`related_memo_id`,`type`)
);
-- drop all tables first
DROP TABLE IF EXISTS `migration_history`;
DROP TABLE IF EXISTS `system_setting`;
DROP TABLE IF EXISTS `user`;
DROP TABLE IF EXISTS `user_setting`;
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `memo_organizer`;
DROP TABLE IF EXISTS `memo_relation`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `tag`;
DROP TABLE IF EXISTS `activity`;
DROP TABLE IF EXISTS `storage`;
DROP TABLE IF EXISTS `idp`;
-- migration_history
CREATE TABLE IF NOT EXISTS `migration_history` (
CREATE TABLE `migration_history` (
`version` varchar(255) NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`version`)
);
-- resource
CREATE TABLE IF NOT EXISTS `resource` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`filename` text NOT NULL,
`blob` blob,
`external_link` text NOT NULL,
`type` varchar(255) NOT NULL DEFAULT '',
`size` int NOT NULL DEFAULT '0',
`internal_path` varchar(255) NOT NULL DEFAULT '',
`memo_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `memo_id` (`memo_id`)
);
-- storage
CREATE TABLE IF NOT EXISTS `storage` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`type` varchar(256) NOT NULL,
`config` text NOT NULL,
PRIMARY KEY (`id`)
);
-- system_setting
CREATE TABLE IF NOT EXISTS `system_setting` (
CREATE TABLE `system_setting` (
`name` varchar(255) NOT NULL,
`value` text NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`name`)
);
-- tag
CREATE TABLE IF NOT EXISTS `tag` (
`name` varchar(255) NOT NULL,
`creator_id` int NOT NULL,
UNIQUE KEY `name` (`name`,`creator_id`)
);
-- user
CREATE TABLE IF NOT EXISTS `user` (
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -121,11 +46,98 @@ CREATE TABLE IF NOT EXISTS `user` (
);
-- user_setting
CREATE TABLE IF NOT EXISTS `user_setting` (
CREATE TABLE `user_setting` (
`user_id` int NOT NULL,
`key` varchar(255) NOT NULL,
`value` text NOT NULL,
UNIQUE KEY `user_id` (`user_id`,`key`)
);
-- memo
CREATE TABLE `memo` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_status` varchar(255) NOT NULL DEFAULT 'NORMAL',
`content` text NOT NULL,
`visibility` varchar(255) NOT NULL DEFAULT 'PRIVATE',
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `visibility` (`visibility`),
CONSTRAINT `memo_chk_1` CHECK ((`row_status` in (_utf8mb4'NORMAL',_utf8mb4'ARCHIVED'))),
CONSTRAINT `memo_chk_2` CHECK ((`visibility` in (_utf8mb4'PUBLIC',_utf8mb4'PROTECTED',_utf8mb4'PRIVATE')))
);
-- memo_organizer
CREATE TABLE `memo_organizer` (
`memo_id` int NOT NULL,
`user_id` int NOT NULL,
`pinned` int NOT NULL DEFAULT '0',
UNIQUE KEY `memo_id` (`memo_id`,`user_id`),
CONSTRAINT `memo_organizer_chk_1` CHECK ((`pinned` in (0,1)))
);
-- memo_relation
CREATE TABLE `memo_relation` (
`memo_id` int NOT NULL,
`related_memo_id` int NOT NULL,
`type` varchar(256) NOT NULL,
UNIQUE KEY `memo_id` (`memo_id`,`related_memo_id`,`type`)
);
-- resource
CREATE TABLE `resource` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`filename` text NOT NULL,
`blob` blob,
`external_link` text NOT NULL,
`type` varchar(255) NOT NULL DEFAULT '',
`size` int NOT NULL DEFAULT '0',
`internal_path` varchar(255) NOT NULL DEFAULT '',
`memo_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `memo_id` (`memo_id`)
);
-- tag
CREATE TABLE `tag` (
`name` varchar(255) NOT NULL,
`creator_id` int NOT NULL,
UNIQUE KEY `name` (`name`,`creator_id`)
);
-- activity
CREATE TABLE `activity` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type` varchar(255) NOT NULL DEFAULT '',
`level` varchar(255) NOT NULL DEFAULT 'INFO',
`payload` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `activity_chk_1` CHECK ((`level` in (_utf8mb4'INFO',_utf8mb4'WARN',_utf8mb4'ERROR')))
);
-- storage
CREATE TABLE `storage` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`type` varchar(256) NOT NULL,
`config` text NOT NULL,
PRIMARY KEY (`id`)
);
-- idp
CREATE TABLE `idp` (
`id` int NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`type` text NOT NULL,
`identifier_filter` varchar(256) NOT NULL DEFAULT '',
`config` text NOT NULL,
PRIMARY KEY (`id`)
);

View File

@@ -0,0 +1,143 @@
-- drop all tables first
DROP TABLE IF EXISTS `migration_history`;
DROP TABLE IF EXISTS `system_setting`;
DROP TABLE IF EXISTS `user`;
DROP TABLE IF EXISTS `user_setting`;
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `memo_organizer`;
DROP TABLE IF EXISTS `memo_relation`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `tag`;
DROP TABLE IF EXISTS `activity`;
DROP TABLE IF EXISTS `storage`;
DROP TABLE IF EXISTS `idp`;
-- migration_history
CREATE TABLE `migration_history` (
`version` varchar(255) NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`version`)
);
-- system_setting
CREATE TABLE `system_setting` (
`name` varchar(255) NOT NULL,
`value` text NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`name`)
);
-- user
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_status` varchar(255) NOT NULL DEFAULT 'NORMAL',
`username` varchar(255) NOT NULL,
`role` varchar(255) NOT NULL DEFAULT 'USER',
`email` varchar(255) NOT NULL DEFAULT '',
`nickname` varchar(255) NOT NULL DEFAULT '',
`password_hash` varchar(255) NOT NULL,
`avatar_url` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
CONSTRAINT `user_chk_1` CHECK ((`row_status` in (_utf8mb4'NORMAL',_utf8mb4'ARCHIVED'))),
CONSTRAINT `user_chk_2` CHECK ((`role` in (_utf8mb4'HOST',_utf8mb4'ADMIN',_utf8mb4'USER')))
);
-- user_setting
CREATE TABLE `user_setting` (
`user_id` int NOT NULL,
`key` varchar(255) NOT NULL,
`value` text NOT NULL,
UNIQUE KEY `user_id` (`user_id`,`key`)
);
-- memo
CREATE TABLE `memo` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_status` varchar(255) NOT NULL DEFAULT 'NORMAL',
`content` text NOT NULL,
`visibility` varchar(255) NOT NULL DEFAULT 'PRIVATE',
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `visibility` (`visibility`),
CONSTRAINT `memo_chk_1` CHECK ((`row_status` in (_utf8mb4'NORMAL',_utf8mb4'ARCHIVED'))),
CONSTRAINT `memo_chk_2` CHECK ((`visibility` in (_utf8mb4'PUBLIC',_utf8mb4'PROTECTED',_utf8mb4'PRIVATE')))
);
-- memo_organizer
CREATE TABLE `memo_organizer` (
`memo_id` int NOT NULL,
`user_id` int NOT NULL,
`pinned` int NOT NULL DEFAULT '0',
UNIQUE KEY `memo_id` (`memo_id`,`user_id`),
CONSTRAINT `memo_organizer_chk_1` CHECK ((`pinned` in (0,1)))
);
-- memo_relation
CREATE TABLE `memo_relation` (
`memo_id` int NOT NULL,
`related_memo_id` int NOT NULL,
`type` varchar(256) NOT NULL,
UNIQUE KEY `memo_id` (`memo_id`,`related_memo_id`,`type`)
);
-- resource
CREATE TABLE `resource` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`filename` text NOT NULL,
`blob` blob,
`external_link` text NOT NULL,
`type` varchar(255) NOT NULL DEFAULT '',
`size` int NOT NULL DEFAULT '0',
`internal_path` varchar(255) NOT NULL DEFAULT '',
`memo_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `creator_id` (`creator_id`),
KEY `memo_id` (`memo_id`)
);
-- tag
CREATE TABLE `tag` (
`name` varchar(255) NOT NULL,
`creator_id` int NOT NULL,
UNIQUE KEY `name` (`name`,`creator_id`)
);
-- activity
CREATE TABLE `activity` (
`id` int NOT NULL AUTO_INCREMENT,
`creator_id` int NOT NULL,
`created_ts` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type` varchar(255) NOT NULL DEFAULT '',
`level` varchar(255) NOT NULL DEFAULT 'INFO',
`payload` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `activity_chk_1` CHECK ((`level` in (_utf8mb4'INFO',_utf8mb4'WARN',_utf8mb4'ERROR')))
);
-- storage
CREATE TABLE `storage` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`type` varchar(256) NOT NULL,
`config` text NOT NULL,
PRIMARY KEY (`id`)
);
-- idp
CREATE TABLE `idp` (
`id` int NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`type` text NOT NULL,
`identifier_filter` varchar(256) NOT NULL DEFAULT '',
`config` text NOT NULL,
PRIMARY KEY (`id`)
);

View File

@@ -18,13 +18,17 @@ type Driver struct {
func NewDriver(profile *profile.Profile) (store.Driver, error) {
db, err := sql.Open("mysql", profile.DSN)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to open db: %s", profile.DSN)
}
driver := Driver{db: db, profile: profile}
return &driver, nil
}
func (d *Driver) GetDB() *sql.DB {
return d.db
}
func (d *Driver) Vacuum(ctx context.Context) error {
tx, err := d.db.BeginTx(ctx, nil)
if err != nil {