mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update package name
This commit is contained in:
21
cmd/memos.go
21
cmd/memos.go
@ -16,8 +16,7 @@ import (
|
||||
"github.com/usememos/memos/server"
|
||||
_profile "github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/mysql"
|
||||
"github.com/usememos/memos/store/sqlite"
|
||||
"github.com/usememos/memos/store/db"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -45,31 +44,19 @@ var (
|
||||
Short: `An open-source, self-hosted memo hub with knowledge management and social networking.`,
|
||||
Run: func(_cmd *cobra.Command, _args []string) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
var err error
|
||||
var driver store.Driver
|
||||
switch profile.Driver {
|
||||
case "sqlite":
|
||||
driver, err = sqlite.NewDriver(profile)
|
||||
case "mysql":
|
||||
driver, err = mysql.NewDriver(profile)
|
||||
default:
|
||||
cancel()
|
||||
log.Error("unknown db driver", zap.String("driver", profile.Driver))
|
||||
return
|
||||
}
|
||||
dbDriver, err := db.NewDBDriver(profile)
|
||||
if err != nil {
|
||||
cancel()
|
||||
log.Error("failed to create db driver", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if err := driver.Migrate(ctx); err != nil {
|
||||
if err := dbDriver.Migrate(ctx); err != nil {
|
||||
cancel()
|
||||
log.Error("failed to migrate db", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
store := store.New(driver, profile)
|
||||
store := store.New(dbDriver, profile)
|
||||
s, err := server.NewServer(ctx, profile, store)
|
||||
if err != nil {
|
||||
cancel()
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/sqlite"
|
||||
"github.com/usememos/memos/store/db/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -39,7 +39,7 @@ var (
|
||||
return
|
||||
}
|
||||
|
||||
driver, err := sqlite.NewDriver(profile)
|
||||
driver, err := sqlite.NewDB(profile)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
||||
return
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
|
||||
"github.com/usememos/memos/common/util"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/sqlite"
|
||||
"github.com/usememos/memos/store/db/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -36,7 +36,7 @@ var (
|
||||
return
|
||||
}
|
||||
|
||||
driver, err := sqlite.NewDriver(profile)
|
||||
driver, err := sqlite.NewDB(profile)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
||||
return
|
||||
|
28
store/db/db.go
Normal file
28
store/db/db.go
Normal file
@ -0,0 +1,28 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/db/mysql"
|
||||
"github.com/usememos/memos/store/db/sqlite"
|
||||
)
|
||||
|
||||
// NewDBDriver creates new db driver based on profile.
|
||||
func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
|
||||
var driver store.Driver
|
||||
var err error
|
||||
switch profile.Driver {
|
||||
case "sqlite":
|
||||
driver, err = sqlite.NewDB(profile)
|
||||
case "mysql":
|
||||
driver, err = mysql.NewDB(profile)
|
||||
default:
|
||||
return nil, errors.New("unknown db driver")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create db driver")
|
||||
}
|
||||
return driver, nil
|
||||
}
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||
stmt := `
|
||||
INSERT INTO activity (
|
||||
creator_id,
|
||||
@ -36,7 +36,7 @@ func (d *Driver) CreateActivity(ctx context.Context, create *store.Activity) (*s
|
||||
return d.FindActivity(ctx, id)
|
||||
}
|
||||
|
||||
func (d *Driver) FindActivity(ctx context.Context, id int64) (*store.Activity, error) {
|
||||
func (d *DB) FindActivity(ctx context.Context, id int64) (*store.Activity, error) {
|
||||
var activity store.Activity
|
||||
stmt := `
|
||||
SELECT
|
@ -10,7 +10,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
|
||||
var configBytes []byte
|
||||
if create.Type == store.IdentityProviderOAuth2Type {
|
||||
bytes, err := json.Marshal(create.Config.OAuth2Config)
|
||||
@ -52,7 +52,7 @@ func (d *Driver) CreateIdentityProvider(ctx context.Context, create *store.Ident
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
|
||||
func (d *DB) 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, "id = ?"), append(args, *v)
|
||||
@ -109,7 +109,7 @@ func (d *Driver) ListIdentityProviders(ctx context.Context, find *store.FindIden
|
||||
return identityProviders, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetIdentityProvider(ctx context.Context, find *store.FindIdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) GetIdentityProvider(ctx context.Context, find *store.FindIdentityProvider) (*store.IdentityProvider, error) {
|
||||
list, err := d.ListIdentityProviders(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -122,7 +122,7 @@ func (d *Driver) GetIdentityProvider(ctx context.Context, find *store.FindIdenti
|
||||
return identityProvider, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.Name; v != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *v)
|
||||
@ -167,7 +167,7 @@ func (d *Driver) UpdateIdentityProvider(ctx context.Context, update *store.Updat
|
||||
return identityProvider, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
|
||||
func (d *DB) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
|
||||
where, args := []string{"id = ?"}, []any{delete.ID}
|
||||
stmt := `DELETE FROM idp WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
|
||||
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo (
|
||||
creator_id,
|
||||
@ -47,7 +47,7 @@ func (d *Driver) CreateMemo(ctx context.Context, create *store.Memo) (*store.Mem
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
|
||||
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -200,7 +200,7 @@ 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) {
|
||||
func (d *DB) GetMemo(ctx context.Context, find *store.FindMemo) (*store.Memo, error) {
|
||||
list, err := d.ListMemos(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -213,7 +213,7 @@ func (d *Driver) GetMemo(ctx context.Context, find *store.FindMemo) (*store.Memo
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
||||
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.CreatedTs; v != nil {
|
||||
set, args = append(set, "created_ts = FROM_UNIXTIME(?)"), append(args, *v)
|
||||
@ -243,7 +243,7 @@ func (d *Driver) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
where, args := []string{"id = ?"}, []any{delete.ID}
|
||||
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
@ -261,7 +261,7 @@ func (d *Driver) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]store.Visibility, error) {
|
||||
func (d *DB) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]store.Visibility, error) {
|
||||
args := make([]any, 0, len(memoIDs))
|
||||
list := make([]string, 0, len(memoIDs))
|
||||
for _, memoID := range memoIDs {
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo_organizer (
|
||||
memo_id,
|
||||
@ -26,7 +26,7 @@ func (d *Driver) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrga
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
func (d *DB) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
where, args := []string{}, []any{}
|
||||
if find.MemoID != 0 {
|
||||
where = append(where, "memo_id = ?")
|
||||
@ -65,7 +65,7 @@ func (d *Driver) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrgan
|
||||
return memoOrganizer, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
||||
func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
||||
where, args := []string{}, []any{}
|
||||
if v := delete.MemoID; v != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, *v)
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
||||
func (d *DB) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo_relation (
|
||||
memo_id,
|
||||
@ -39,7 +39,7 @@ func (d *Driver) UpsertMemoRelation(ctx context.Context, create *store.MemoRelat
|
||||
return &memoRelation, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
||||
func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
||||
where, args := []string{"TRUE"}, []any{}
|
||||
if find.MemoID != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, find.MemoID)
|
||||
@ -83,7 +83,7 @@ func (d *Driver) ListMemoRelations(ctx context.Context, find *store.FindMemoRela
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
||||
func (d *DB) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
||||
where, args := []string{"TRUE"}, []any{}
|
||||
if delete.MemoID != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, delete.MemoID)
|
@ -85,7 +85,7 @@ CREATE TABLE `resource` (
|
||||
`external_link` TEXT NOT NULL,
|
||||
`type` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`size` INT NOT NULL DEFAULT '0',
|
||||
`INTernal_path` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`internal_path` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`memo_id` INT DEFAULT NULL
|
||||
);
|
||||
|
@ -85,7 +85,7 @@ CREATE TABLE `resource` (
|
||||
`external_link` TEXT NOT NULL,
|
||||
`type` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`size` INT NOT NULL DEFAULT '0',
|
||||
`INTernal_path` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`internal_path` VARCHAR(255) NOT NULL DEFAULT '',
|
||||
`memo_id` INT DEFAULT NULL
|
||||
);
|
||||
|
@ -18,7 +18,7 @@ type MigrationHistoryFind struct {
|
||||
Version *string
|
||||
}
|
||||
|
||||
func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||
func (d *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Version; v != nil {
|
||||
@ -57,7 +57,7 @@ func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *MigrationHi
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
||||
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
||||
stmt := `
|
||||
INSERT INTO migration_history (version) VALUES (?)
|
||||
ON DUPLICATE KEY UPDATE version = ?
|
@ -21,7 +21,7 @@ const (
|
||||
//go:embed migration
|
||||
var migrationFS embed.FS
|
||||
|
||||
func (d *Driver) Migrate(ctx context.Context) error {
|
||||
func (d *DB) Migrate(ctx context.Context) error {
|
||||
if d.profile.IsDev() {
|
||||
return d.nonProdMigrate(ctx)
|
||||
}
|
||||
@ -29,7 +29,7 @@ func (d *Driver) Migrate(ctx context.Context) error {
|
||||
return d.prodMigrate(ctx)
|
||||
}
|
||||
|
||||
func (d *Driver) nonProdMigrate(ctx context.Context) error {
|
||||
func (d *DB) nonProdMigrate(ctx context.Context) error {
|
||||
buf, err := migrationFS.ReadFile("migration/dev/" + latestSchemaFileName)
|
||||
if err != nil {
|
||||
return errors.Errorf("failed to read latest schema file: %s", err)
|
||||
@ -49,7 +49,7 @@ func (d *Driver) nonProdMigrate(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) prodMigrate(ctx context.Context) error {
|
||||
func (d *DB) prodMigrate(ctx context.Context) error {
|
||||
currentVersion := version.GetCurrentVersion(d.profile.Mode)
|
||||
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
|
||||
// If there is no migration history, we should apply the latest schema.
|
||||
@ -95,7 +95,7 @@ func (d *Driver) prodMigrate(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error {
|
||||
func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error {
|
||||
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("migration/prod/%s/*.sql", minorVersion))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read ddl files")
|
||||
@ -130,7 +130,7 @@ func (d *Driver) applyMigrationForMinorVersion(ctx context.Context, minorVersion
|
||||
//go:embed seed
|
||||
var seedFS embed.FS
|
||||
|
||||
func (d *Driver) seed(ctx context.Context) error {
|
||||
func (d *DB) seed(ctx context.Context) error {
|
||||
filenames, err := fs.Glob(seedFS, "seed/*.sql")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read seed files")
|
@ -11,12 +11,12 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
type DB struct {
|
||||
db *sql.DB
|
||||
profile *profile.Profile
|
||||
}
|
||||
|
||||
func NewDriver(profile *profile.Profile) (store.Driver, error) {
|
||||
func NewDB(profile *profile.Profile) (store.Driver, error) {
|
||||
// Open MySQL connection with parameter.
|
||||
// multiStatements=true is required for migration.
|
||||
// See more in: https://github.com/go-sql-driver/mysql#multistatements
|
||||
@ -25,15 +25,15 @@ func NewDriver(profile *profile.Profile) (store.Driver, error) {
|
||||
return nil, errors.Wrapf(err, "failed to open db: %s", profile.DSN)
|
||||
}
|
||||
|
||||
driver := Driver{db: db, profile: profile}
|
||||
driver := DB{db: db, profile: profile}
|
||||
return &driver, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetDB() *sql.DB {
|
||||
func (d *DB) GetDB() *sql.DB {
|
||||
return d.db
|
||||
}
|
||||
|
||||
func (d *Driver) Vacuum(ctx context.Context) error {
|
||||
func (d *DB) Vacuum(ctx context.Context) error {
|
||||
tx, err := d.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -63,10 +63,10 @@ func (d *Driver) Vacuum(ctx context.Context) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (*Driver) BackupTo(context.Context, string) error {
|
||||
func (*DB) BackupTo(context.Context, string) error {
|
||||
return errors.New("Please use mysqldump to backup")
|
||||
}
|
||||
|
||||
func (d *Driver) Close() error {
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
@ -11,19 +11,8 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
|
||||
stmt := `
|
||||
INSERT INTO resource (
|
||||
filename,
|
||||
resource.blob,
|
||||
external_link,
|
||||
type,
|
||||
size,
|
||||
creator_id,
|
||||
internal_path
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
|
||||
stmt := "INSERT INTO resource (`filename`, `blob`, `external_link`, `type`, `size`, `creator_id`, `internal_path`) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
result, err := d.db.ExecContext(
|
||||
ctx,
|
||||
stmt,
|
||||
@ -56,7 +45,7 @@ func (d *Driver) CreateResource(ctx context.Context, create *store.Resource) (*s
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
|
||||
func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -77,7 +66,7 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
|
||||
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "UNIX_TIMESTAMP(created_ts)", "UNIX_TIMESTAMP(updated_ts)", "internal_path", "memo_id"}
|
||||
if find.GetBlob {
|
||||
fields = append(fields, "resource.blob")
|
||||
fields = append(fields, "blob")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
@ -136,7 +125,7 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
|
||||
func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
|
||||
set, args := []string{}, []any{}
|
||||
|
||||
if v := update.UpdatedTs; v != nil {
|
||||
@ -151,11 +140,8 @@ func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResourc
|
||||
if v := update.MemoID; v != nil {
|
||||
set, args = append(set, "memo_id = ?"), append(args, *v)
|
||||
}
|
||||
if update.UnbindMemo {
|
||||
set = append(set, "memo_id = NULL")
|
||||
}
|
||||
if v := update.Blob; v != nil {
|
||||
set, args = append(set, "resource.blob = ?"), append(args, v)
|
||||
set, args = append(set, "blob = ?"), append(args, v)
|
||||
}
|
||||
|
||||
args = append(args, update.ID)
|
||||
@ -179,7 +165,7 @@ func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResourc
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
|
||||
func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
|
||||
stmt := `DELETE FROM resource WHERE id = ?`
|
||||
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
||||
if err != nil {
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) 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,
|
||||
@ -30,7 +30,7 @@ func (d *Driver) CreateStorage(ctx context.Context, create *store.Storage) (*sto
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
||||
func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "id = ?"), append(args, *find.ID)
|
||||
@ -73,7 +73,7 @@ func (d *Driver) ListStorages(ctx context.Context, find *store.FindStorage) ([]*
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
|
||||
func (d *DB) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
|
||||
list, err := d.ListStorages(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -85,7 +85,7 @@ func (d *Driver) GetStorage(ctx context.Context, find *store.FindStorage) (*stor
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
||||
func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.Name != nil {
|
||||
set = append(set, "name = ?")
|
||||
@ -121,7 +121,7 @@ func (d *Driver) UpdateStorage(ctx context.Context, update *store.UpdateStorage)
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
||||
func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
||||
stmt := `
|
||||
DELETE FROM storage
|
||||
WHERE id = ?
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSetting) (*store.SystemSetting, error) {
|
||||
func (d *DB) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSetting) (*store.SystemSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO system_setting (
|
||||
name, value, description
|
||||
@ -31,7 +31,7 @@ func (d *Driver) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSe
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListSystemSettings(ctx context.Context, find *store.FindSystemSetting) ([]*store.SystemSetting, error) {
|
||||
func (d *DB) ListSystemSettings(ctx context.Context, find *store.FindSystemSetting) ([]*store.SystemSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.Name != "" {
|
||||
where, args = append(where, "name = ?"), append(args, find.Name)
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag, error) {
|
||||
func (d *DB) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag, error) {
|
||||
stmt := `
|
||||
INSERT INTO tag (name, creator_id)
|
||||
VALUES (?, ?)
|
||||
@ -21,7 +21,7 @@ func (d *Driver) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag,
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Tag, error) {
|
||||
func (d *DB) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Tag, error) {
|
||||
where, args := []string{"creator_id = ?"}, []any{find.CreatorID}
|
||||
query := `
|
||||
SELECT
|
||||
@ -57,7 +57,7 @@ func (d *Driver) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Ta
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteTag(ctx context.Context, delete *store.DeleteTag) error {
|
||||
func (d *DB) DeleteTag(ctx context.Context, delete *store.DeleteTag) error {
|
||||
where, args := []string{"name = ?", "creator_id = ?"}, []any{delete.Name, delete.CreatorID}
|
||||
stmt := `DELETE FROM tag WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||
stmt := `
|
||||
INSERT INTO user (
|
||||
username,
|
||||
@ -50,7 +50,7 @@ func (d *Driver) CreateUser(ctx context.Context, create *store.User) (*store.Use
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
|
||||
func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.UpdatedTs; v != nil {
|
||||
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
||||
@ -117,7 +117,7 @@ func (d *Driver) UpdateUser(ctx context.Context, update *store.UpdateUser) (*sto
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
|
||||
func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -185,7 +185,7 @@ func (d *Driver) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
|
||||
func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
|
||||
result, err := d.db.ExecContext(ctx, `
|
||||
DELETE FROM user WHERE id = ?
|
||||
`, delete.ID)
|
@ -12,12 +12,8 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertUserSetting(ctx context.Context, upsert *store.UserSetting) (*store.UserSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO user_setting (user_id,user_setting.key,value)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE value = ?
|
||||
`
|
||||
func (d *DB) UpsertUserSetting(ctx context.Context, upsert *store.UserSetting) (*store.UserSetting, error) {
|
||||
stmt := "INSERT INTO user_setting (`user_id`, `key`, `value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = ?"
|
||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserID, upsert.Key, upsert.Value, upsert.Value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -25,7 +21,7 @@ func (d *Driver) UpsertUserSetting(ctx context.Context, upsert *store.UserSettin
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*store.UserSetting, error) {
|
||||
func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*store.UserSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Key; v != "" {
|
||||
@ -68,7 +64,7 @@ func (d *Driver) ListUserSettings(ctx context.Context, find *store.FindUserSetti
|
||||
return userSettingList, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
||||
func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO user_setting (user_id, user_setting.key, value)
|
||||
VALUES (?, ?, ?)
|
||||
@ -92,7 +88,7 @@ func (d *Driver) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSe
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUserSettingsV1(ctx context.Context, find *store.FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
||||
func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
|
@ -6,7 +6,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||
stmt := `
|
||||
INSERT INTO activity (
|
||||
creator_id,
|
@ -11,7 +11,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) CreateIdentityProvider(ctx context.Context, create *store.IdentityProvider) (*store.IdentityProvider, error) {
|
||||
var configBytes []byte
|
||||
if create.Type == store.IdentityProviderOAuth2Type {
|
||||
bytes, err := json.Marshal(create.Config.OAuth2Config)
|
||||
@ -50,7 +50,7 @@ func (d *Driver) CreateIdentityProvider(ctx context.Context, create *store.Ident
|
||||
return identityProvider, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListIdentityProviders(ctx context.Context, find *store.FindIdentityProvider) ([]*store.IdentityProvider, error) {
|
||||
func (d *DB) 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)
|
||||
@ -107,7 +107,7 @@ func (d *Driver) ListIdentityProviders(ctx context.Context, find *store.FindIden
|
||||
return identityProviders, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetIdentityProvider(ctx context.Context, find *store.FindIdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) GetIdentityProvider(ctx context.Context, find *store.FindIdentityProvider) (*store.IdentityProvider, error) {
|
||||
list, err := d.ListIdentityProviders(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -120,7 +120,7 @@ func (d *Driver) GetIdentityProvider(ctx context.Context, find *store.FindIdenti
|
||||
return identityProvider, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
|
||||
func (d *DB) UpdateIdentityProvider(ctx context.Context, update *store.UpdateIdentityProvider) (*store.IdentityProvider, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.Name; v != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *v)
|
||||
@ -176,7 +176,7 @@ func (d *Driver) UpdateIdentityProvider(ctx context.Context, update *store.Updat
|
||||
return &identityProvider, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
|
||||
func (d *DB) DeleteIdentityProvider(ctx context.Context, delete *store.DeleteIdentityProvider) error {
|
||||
where, args := []string{"id = ?"}, []any{delete.ID}
|
||||
stmt := `DELETE FROM idp WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
@ -13,7 +13,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
|
||||
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
|
||||
if create.CreatedTs == 0 {
|
||||
create.CreatedTs = time.Now().Unix()
|
||||
}
|
||||
@ -47,7 +47,7 @@ func (d *Driver) CreateMemo(ctx context.Context, create *store.Memo) (*store.Mem
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
|
||||
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -200,7 +200,7 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
||||
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.CreatedTs; v != nil {
|
||||
set, args = append(set, "created_ts = ?"), append(args, *v)
|
||||
@ -230,7 +230,7 @@ func (d *Driver) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
||||
where, args := []string{"id = ?"}, []any{delete.ID}
|
||||
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
@ -248,7 +248,7 @@ func (d *Driver) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]store.Visibility, error) {
|
||||
func (d *DB) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]store.Visibility, error) {
|
||||
args := make([]any, 0, len(memoIDs))
|
||||
list := make([]string, 0, len(memoIDs))
|
||||
for _, memoID := range memoIDs {
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo_organizer (
|
||||
memo_id,
|
||||
@ -28,7 +28,7 @@ func (d *Driver) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrga
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
func (d *DB) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) (*store.MemoOrganizer, error) {
|
||||
where, args := []string{}, []any{}
|
||||
if find.MemoID != 0 {
|
||||
where = append(where, "memo_id = ?")
|
||||
@ -67,7 +67,7 @@ func (d *Driver) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrgan
|
||||
return memoOrganizer, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
||||
func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
||||
where, args := []string{}, []any{}
|
||||
if v := delete.MemoID; v != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, *v)
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
||||
func (d *DB) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo_relation (
|
||||
memo_id,
|
||||
@ -38,7 +38,7 @@ func (d *Driver) UpsertMemoRelation(ctx context.Context, create *store.MemoRelat
|
||||
return memoRelation, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
||||
func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
||||
where, args := []string{"TRUE"}, []any{}
|
||||
if find.MemoID != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, find.MemoID)
|
||||
@ -82,7 +82,7 @@ func (d *Driver) ListMemoRelations(ctx context.Context, find *store.FindMemoRela
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
||||
func (d *DB) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
||||
where, args := []string{"TRUE"}, []any{}
|
||||
if delete.MemoID != nil {
|
||||
where, args = append(where, "memo_id = ?"), append(args, delete.MemoID)
|
@ -18,7 +18,7 @@ type MigrationHistoryFind struct {
|
||||
Version *string
|
||||
}
|
||||
|
||||
func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||
func (d *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Version; v != nil {
|
||||
@ -60,7 +60,7 @@ func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *MigrationHi
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
||||
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
||||
stmt := `
|
||||
INSERT INTO migration_history (
|
||||
version
|
@ -22,7 +22,7 @@ var migrationFS embed.FS
|
||||
var seedFS embed.FS
|
||||
|
||||
// Migrate applies the latest schema to the database.
|
||||
func (d *Driver) Migrate(ctx context.Context) error {
|
||||
func (d *DB) Migrate(ctx context.Context) error {
|
||||
if d.profile.Mode == "prod" {
|
||||
_, err := os.Stat(d.profile.DSN)
|
||||
if err != nil {
|
||||
@ -112,7 +112,7 @@ const (
|
||||
latestSchemaFileName = "LATEST__SCHEMA.sql"
|
||||
)
|
||||
|
||||
func (d *Driver) applyLatestSchema(ctx context.Context) error {
|
||||
func (d *DB) applyLatestSchema(ctx context.Context) error {
|
||||
schemaMode := "dev"
|
||||
if d.profile.Mode == "prod" {
|
||||
schemaMode = "prod"
|
||||
@ -129,7 +129,7 @@ func (d *Driver) applyLatestSchema(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error {
|
||||
func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error {
|
||||
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/%s/*.sql", "migration/prod", minorVersion))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read ddl files")
|
||||
@ -162,7 +162,7 @@ func (d *Driver) applyMigrationForMinorVersion(ctx context.Context, minorVersion
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) seed(ctx context.Context) error {
|
||||
func (d *DB) seed(ctx context.Context) error {
|
||||
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read seed files")
|
||||
@ -185,7 +185,7 @@ func (d *Driver) seed(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// execute runs a single SQL statement within a transaction.
|
||||
func (d *Driver) execute(ctx context.Context, stmt string) error {
|
||||
func (d *DB) execute(ctx context.Context, stmt string) error {
|
||||
tx, err := d.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
|
||||
func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
|
||||
stmt := `
|
||||
INSERT INTO resource (
|
||||
filename,
|
||||
@ -40,7 +40,7 @@ func (d *Driver) CreateResource(ctx context.Context, create *store.Resource) (*s
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
|
||||
func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -120,7 +120,7 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
|
||||
func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
|
||||
set, args := []string{}, []any{}
|
||||
|
||||
if v := update.UpdatedTs; v != nil {
|
||||
@ -135,9 +135,6 @@ func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResourc
|
||||
if v := update.MemoID; v != nil {
|
||||
set, args = append(set, "memo_id = ?"), append(args, *v)
|
||||
}
|
||||
if update.UnbindMemo {
|
||||
set = append(set, "memo_id = NULL")
|
||||
}
|
||||
if v := update.Blob; v != nil {
|
||||
set, args = append(set, "blob = ?"), append(args, v)
|
||||
}
|
||||
@ -168,7 +165,7 @@ func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResourc
|
||||
return &resource, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
|
||||
func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
|
||||
stmt := `
|
||||
DELETE FROM resource
|
||||
WHERE id = ?
|
@ -11,15 +11,15 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
type DB struct {
|
||||
db *sql.DB
|
||||
profile *profile.Profile
|
||||
}
|
||||
|
||||
// NewDriver opens a database specified by its database driver name and a
|
||||
// NewDB opens a database specified by its database driver name and a
|
||||
// driver-specific data source name, usually consisting of at least a
|
||||
// database name and connection information.
|
||||
func NewDriver(profile *profile.Profile) (store.Driver, error) {
|
||||
func NewDB(profile *profile.Profile) (store.Driver, error) {
|
||||
// Ensure a DSN is set before attempting to open the database.
|
||||
if profile.DSN == "" {
|
||||
return nil, errors.New("dsn required")
|
||||
@ -44,16 +44,16 @@ func NewDriver(profile *profile.Profile) (store.Driver, error) {
|
||||
return nil, errors.Wrapf(err, "failed to open db with dsn: %s", profile.DSN)
|
||||
}
|
||||
|
||||
driver := Driver{db: sqliteDB, profile: profile}
|
||||
driver := DB{db: sqliteDB, profile: profile}
|
||||
|
||||
return &driver, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetDB() *sql.DB {
|
||||
func (d *DB) GetDB() *sql.DB {
|
||||
return d.db
|
||||
}
|
||||
|
||||
func (d *Driver) Vacuum(ctx context.Context) error {
|
||||
func (d *DB) Vacuum(ctx context.Context) error {
|
||||
tx, err := d.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -100,7 +100,7 @@ func vacuumImpl(ctx context.Context, tx *sql.Tx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) BackupTo(ctx context.Context, filename string) error {
|
||||
func (d *DB) BackupTo(ctx context.Context, filename string) error {
|
||||
conn, err := d.db.Conn(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fail to open new connection")
|
||||
@ -137,6 +137,6 @@ func (d *Driver) BackupTo(ctx context.Context, filename string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) Close() error {
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) 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,
|
||||
@ -27,7 +27,7 @@ func (d *Driver) CreateStorage(ctx context.Context, create *store.Storage) (*sto
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
||||
func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "id = ?"), append(args, *find.ID)
|
||||
@ -70,7 +70,7 @@ func (d *Driver) ListStorages(ctx context.Context, find *store.FindStorage) ([]*
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
|
||||
func (d *DB) GetStorage(ctx context.Context, find *store.FindStorage) (*store.Storage, error) {
|
||||
list, err := d.ListStorages(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -82,7 +82,7 @@ func (d *Driver) GetStorage(ctx context.Context, find *store.FindStorage) (*stor
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
||||
func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.Name != nil {
|
||||
set = append(set, "name = ?")
|
||||
@ -117,7 +117,7 @@ func (d *Driver) UpdateStorage(ctx context.Context, update *store.UpdateStorage)
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
||||
func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
|
||||
stmt := `
|
||||
DELETE FROM storage
|
||||
WHERE id = ?
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSetting) (*store.SystemSetting, error) {
|
||||
func (d *DB) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSetting) (*store.SystemSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO system_setting (
|
||||
name, value, description
|
||||
@ -25,7 +25,7 @@ func (d *Driver) UpsertSystemSetting(ctx context.Context, upsert *store.SystemSe
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListSystemSettings(ctx context.Context, find *store.FindSystemSetting) ([]*store.SystemSetting, error) {
|
||||
func (d *DB) ListSystemSettings(ctx context.Context, find *store.FindSystemSetting) ([]*store.SystemSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.Name != "" {
|
||||
where, args = append(where, "name = ?"), append(args, find.Name)
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag, error) {
|
||||
func (d *DB) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag, error) {
|
||||
stmt := `
|
||||
INSERT INTO tag (
|
||||
name, creator_id
|
||||
@ -26,7 +26,7 @@ func (d *Driver) UpsertTag(ctx context.Context, upsert *store.Tag) (*store.Tag,
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Tag, error) {
|
||||
func (d *DB) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Tag, error) {
|
||||
where, args := []string{"creator_id = ?"}, []any{find.CreatorID}
|
||||
query := `
|
||||
SELECT
|
||||
@ -62,7 +62,7 @@ func (d *Driver) ListTags(ctx context.Context, find *store.FindTag) ([]*store.Ta
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteTag(ctx context.Context, delete *store.DeleteTag) error {
|
||||
func (d *DB) DeleteTag(ctx context.Context, delete *store.DeleteTag) error {
|
||||
where, args := []string{"name = ?", "creator_id = ?"}, []any{delete.Name, delete.CreatorID}
|
||||
stmt := `DELETE FROM tag WHERE ` + strings.Join(where, " AND ")
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||
stmt := `
|
||||
INSERT INTO user (
|
||||
username,
|
||||
@ -40,7 +40,7 @@ func (d *Driver) CreateUser(ctx context.Context, create *store.User) (*store.Use
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
|
||||
func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.UpdatedTs; v != nil {
|
||||
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
||||
@ -90,7 +90,7 @@ func (d *Driver) UpdateUser(ctx context.Context, update *store.UpdateUser) (*sto
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
|
||||
func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
@ -158,7 +158,7 @@ func (d *Driver) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
|
||||
func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
|
||||
result, err := d.db.ExecContext(ctx, `
|
||||
DELETE FROM user WHERE id = ?
|
||||
`, delete.ID)
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *Driver) UpsertUserSetting(ctx context.Context, upsert *store.UserSetting) (*store.UserSetting, error) {
|
||||
func (d *DB) UpsertUserSetting(ctx context.Context, upsert *store.UserSetting) (*store.UserSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO user_setting (
|
||||
user_id, key, value
|
||||
@ -28,7 +28,7 @@ func (d *Driver) UpsertUserSetting(ctx context.Context, upsert *store.UserSettin
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*store.UserSetting, error) {
|
||||
func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*store.UserSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Key; v != "" {
|
||||
@ -71,7 +71,7 @@ func (d *Driver) ListUserSettings(ctx context.Context, find *store.FindUserSetti
|
||||
return userSettingList, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
||||
func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO user_setting (
|
||||
user_id, key, value
|
||||
@ -98,7 +98,7 @@ func (d *Driver) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSe
|
||||
return upsert, nil
|
||||
}
|
||||
|
||||
func (d *Driver) ListUserSettingsV1(ctx context.Context, find *store.FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
||||
func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
|
@ -7,6 +7,8 @@ import (
|
||||
storepb "github.com/usememos/memos/proto/gen/store"
|
||||
)
|
||||
|
||||
// Driver is an interface for store driver.
|
||||
// It contains all methods that store database driver should implement.
|
||||
type Driver interface {
|
||||
GetDB() *sql.DB
|
||||
Close() error
|
||||
@ -15,50 +17,61 @@ type Driver interface {
|
||||
Vacuum(ctx context.Context) error
|
||||
BackupTo(ctx context.Context, filename string) error
|
||||
|
||||
// Activity model related methods.
|
||||
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
|
||||
|
||||
// Resource model related methods.
|
||||
CreateResource(ctx context.Context, create *Resource) (*Resource, error)
|
||||
ListResources(ctx context.Context, find *FindResource) ([]*Resource, error)
|
||||
UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error)
|
||||
DeleteResource(ctx context.Context, delete *DeleteResource) error
|
||||
|
||||
// Memo model related methods.
|
||||
CreateMemo(ctx context.Context, create *Memo) (*Memo, error)
|
||||
ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error)
|
||||
UpdateMemo(ctx context.Context, update *UpdateMemo) error
|
||||
DeleteMemo(ctx context.Context, delete *DeleteMemo) error
|
||||
FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]Visibility, error)
|
||||
|
||||
// MemoRelation model related methods.
|
||||
UpsertMemoRelation(ctx context.Context, create *MemoRelation) (*MemoRelation, error)
|
||||
ListMemoRelations(ctx context.Context, find *FindMemoRelation) ([]*MemoRelation, error)
|
||||
DeleteMemoRelation(ctx context.Context, delete *DeleteMemoRelation) error
|
||||
|
||||
// MemoOrganizer model related methods.
|
||||
UpsertMemoOrganizer(ctx context.Context, upsert *MemoOrganizer) (*MemoOrganizer, error)
|
||||
GetMemoOrganizer(ctx context.Context, find *FindMemoOrganizer) (*MemoOrganizer, error)
|
||||
DeleteMemoOrganizer(ctx context.Context, delete *DeleteMemoOrganizer) error
|
||||
|
||||
// SystemSetting model related methods.
|
||||
UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error)
|
||||
ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error)
|
||||
|
||||
// User model related methods.
|
||||
CreateUser(ctx context.Context, create *User) (*User, error)
|
||||
UpdateUser(ctx context.Context, update *UpdateUser) (*User, error)
|
||||
ListUsers(ctx context.Context, find *FindUser) ([]*User, error)
|
||||
DeleteUser(ctx context.Context, delete *DeleteUser) error
|
||||
|
||||
// UserSetting model related methods.
|
||||
UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error)
|
||||
ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error)
|
||||
UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error)
|
||||
ListUserSettingsV1(ctx context.Context, find *FindUserSettingV1) ([]*storepb.UserSetting, error)
|
||||
|
||||
// IdentityProvider model related methods.
|
||||
CreateIdentityProvider(ctx context.Context, create *IdentityProvider) (*IdentityProvider, error)
|
||||
ListIdentityProviders(ctx context.Context, find *FindIdentityProvider) ([]*IdentityProvider, error)
|
||||
GetIdentityProvider(ctx context.Context, find *FindIdentityProvider) (*IdentityProvider, error)
|
||||
UpdateIdentityProvider(ctx context.Context, update *UpdateIdentityProvider) (*IdentityProvider, error)
|
||||
DeleteIdentityProvider(ctx context.Context, delete *DeleteIdentityProvider) error
|
||||
|
||||
// Tag model related methods.
|
||||
UpsertTag(ctx context.Context, upsert *Tag) (*Tag, error)
|
||||
ListTags(ctx context.Context, find *FindTag) ([]*Tag, error)
|
||||
DeleteTag(ctx context.Context, delete *DeleteTag) error
|
||||
|
||||
// Storage model related methods.
|
||||
CreateStorage(ctx context.Context, create *Storage) (*Storage, error)
|
||||
ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error)
|
||||
GetStorage(ctx context.Context, find *FindStorage) (*Storage, error)
|
||||
|
@ -39,7 +39,6 @@ type UpdateResource struct {
|
||||
Filename *string
|
||||
InternalPath *string
|
||||
MemoID *int32
|
||||
UnbindMemo bool
|
||||
Blob []byte
|
||||
}
|
||||
|
||||
|
@ -1,138 +0,0 @@
|
||||
-- migration_history
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
|
||||
);
|
||||
|
||||
-- system_setting
|
||||
CREATE TABLE system_setting (
|
||||
name TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
UNIQUE(name)
|
||||
);
|
||||
|
||||
-- user
|
||||
CREATE TABLE user (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
role TEXT NOT NULL CHECK (role IN ('HOST', 'ADMIN', 'USER')) DEFAULT 'USER',
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
nickname TEXT NOT NULL DEFAULT '',
|
||||
password_hash TEXT NOT NULL,
|
||||
avatar_url TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE INDEX idx_user_username ON user (username);
|
||||
|
||||
-- user_setting
|
||||
CREATE TABLE user_setting (
|
||||
user_id INTEGER NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE(user_id, key)
|
||||
);
|
||||
|
||||
-- memo
|
||||
CREATE TABLE memo (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
|
||||
content TEXT NOT NULL DEFAULT '',
|
||||
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED', 'PRIVATE')) DEFAULT 'PRIVATE'
|
||||
);
|
||||
|
||||
CREATE INDEX idx_memo_creator_id ON memo (creator_id);
|
||||
CREATE INDEX idx_memo_content ON memo (content);
|
||||
CREATE INDEX idx_memo_visibility ON memo (visibility);
|
||||
|
||||
-- memo_organizer
|
||||
CREATE TABLE memo_organizer (
|
||||
memo_id INTEGER NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
pinned INTEGER NOT NULL CHECK (pinned IN (0, 1)) DEFAULT 0,
|
||||
UNIQUE(memo_id, user_id)
|
||||
);
|
||||
|
||||
-- shortcut
|
||||
CREATE TABLE shortcut (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
payload TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
-- resource
|
||||
CREATE TABLE resource (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
filename TEXT NOT NULL DEFAULT '',
|
||||
blob BLOB DEFAULT NULL,
|
||||
external_link TEXT NOT NULL DEFAULT '',
|
||||
type TEXT NOT NULL DEFAULT '',
|
||||
size INTEGER NOT NULL DEFAULT 0,
|
||||
internal_path TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE INDEX idx_resource_creator_id ON resource (creator_id);
|
||||
|
||||
-- memo_resource
|
||||
CREATE TABLE memo_resource (
|
||||
memo_id INTEGER NOT NULL,
|
||||
resource_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
UNIQUE(memo_id, resource_id)
|
||||
);
|
||||
|
||||
-- tag
|
||||
CREATE TABLE tag (
|
||||
name TEXT NOT NULL,
|
||||
creator_id INTEGER NOT NULL,
|
||||
UNIQUE(name, creator_id)
|
||||
);
|
||||
|
||||
-- activity
|
||||
CREATE TABLE activity (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
creator_id INTEGER NOT NULL,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
type TEXT NOT NULL DEFAULT '',
|
||||
level TEXT NOT NULL CHECK (level IN ('INFO', 'WARN', 'ERROR')) DEFAULT 'INFO',
|
||||
payload TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
-- storage
|
||||
CREATE TABLE storage (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
config TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
-- idp
|
||||
CREATE TABLE idp (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
identifier_filter TEXT NOT NULL DEFAULT '',
|
||||
config TEXT NOT NULL DEFAULT '{}'
|
||||
);
|
||||
|
||||
-- memo_relation
|
||||
CREATE TABLE memo_relation (
|
||||
memo_id INTEGER NOT NULL,
|
||||
related_memo_id INTEGER NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
UNIQUE(memo_id, related_memo_id, type)
|
||||
);
|
@ -18,7 +18,7 @@ import (
|
||||
"github.com/usememos/memos/server"
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/sqlite"
|
||||
"github.com/usememos/memos/store/db"
|
||||
"github.com/usememos/memos/test"
|
||||
)
|
||||
|
||||
@ -31,15 +31,15 @@ type TestingServer struct {
|
||||
|
||||
func NewTestingServer(ctx context.Context, t *testing.T) (*TestingServer, error) {
|
||||
profile := test.GetTestingProfile(t)
|
||||
driver, err := sqlite.NewDriver(profile)
|
||||
dbDriver, err := db.NewDBDriver(profile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create db driver")
|
||||
}
|
||||
if err := driver.Migrate(ctx); err != nil {
|
||||
if err := dbDriver.Migrate(ctx); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to migrate db")
|
||||
}
|
||||
|
||||
store := store.New(driver, profile)
|
||||
store := store.New(dbDriver, profile)
|
||||
server, err := server.NewServer(ctx, profile, store)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create server")
|
||||
|
@ -11,30 +11,20 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/mysql"
|
||||
"github.com/usememos/memos/store/sqlite"
|
||||
"github.com/usememos/memos/store/db"
|
||||
"github.com/usememos/memos/test"
|
||||
)
|
||||
|
||||
func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
|
||||
profile := test.GetTestingProfile(t)
|
||||
var driver store.Driver
|
||||
var err error
|
||||
switch profile.Driver {
|
||||
case "sqlite":
|
||||
driver, err = sqlite.NewDriver(profile)
|
||||
case "mysql":
|
||||
driver, err = mysql.NewDriver(profile)
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown db driver: %s", profile.Driver))
|
||||
}
|
||||
dbDriver, err := db.NewDBDriver(profile)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
||||
}
|
||||
if err := driver.Migrate(ctx); err != nil {
|
||||
if err := dbDriver.Migrate(ctx); err != nil {
|
||||
fmt.Printf("failed to migrate db, error: %+v\n", err)
|
||||
}
|
||||
|
||||
store := store.New(driver, profile)
|
||||
store := store.New(dbDriver, profile)
|
||||
return store
|
||||
}
|
||||
|
Reference in New Issue
Block a user