mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: support mysql as backend storage driver (#2300)
* Rename checkDSN to checkDataDir * Add option to set DSN and db driver * Add mysql driver skeleton * Add mysql container in compose for debug * Add basic function for mysql driver * Cleanup go mod with tidy * Cleanup go.sum with tidy * Add DeleteUser support for mysql driver * Fix UpdateUser of mysql driver * Add DeleteTag support for mysql driver * Add DeleteResource support for mysql driver * Add UpdateMemo and DeleteMemo support for mysql driver * Add MemoRelation support for mysql driver * Add MemoOrganizer support for mysql driver * Add Idp support for mysql driver * Add Storage support for mysql driver * Add FindMemosVisibilityList support for mysql driver * Add Vacuum support for mysql driver * Add Migration support for mysql driver * Add Migration support for mysql driver * Fix ListMemo failed with referece * Change Activity.CreateTs type in MySQL * Change User.CreateTs type in MySQL * Fix by golangci-lint * Change Resource.CreateTs type in MySQL * Change MigrationHistory.CreateTs type in MySQL * Change Memo.CreateTs type in MySQL
This commit is contained in:
84
store/mysql/migration_history.go
Normal file
84
store/mysql/migration_history.go
Normal file
@ -0,0 +1,84 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
Version string
|
||||
CreatedTs int64
|
||||
}
|
||||
|
||||
type MigrationHistoryUpsert struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
type MigrationHistoryFind struct {
|
||||
Version *string
|
||||
}
|
||||
|
||||
func (d *Driver) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.Version; v != nil {
|
||||
where, args = append(where, "version = ?"), append(args, *v)
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT version, UNIX_TIMESTAMP(created_ts)
|
||||
FROM migration_history
|
||||
WHERE ` + strings.Join(where, " AND ") + `
|
||||
ORDER BY created_ts DESC
|
||||
`
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := make([]*MigrationHistory, 0)
|
||||
for rows.Next() {
|
||||
var migrationHistory MigrationHistory
|
||||
if err := rows.Scan(
|
||||
&migrationHistory.Version,
|
||||
&migrationHistory.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list = append(list, &migrationHistory)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *Driver) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
||||
stmt := `
|
||||
INSERT INTO migration_history (version) VALUES (?)
|
||||
ON DUPLICATE KEY UPDATE version = ?
|
||||
`
|
||||
_, err := d.db.ExecContext(ctx, stmt, upsert.Version, upsert.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var migrationHistory MigrationHistory
|
||||
stmt = `
|
||||
SELECT version, UNIX_TIMESTAMP(created_ts)
|
||||
FROM migration_history
|
||||
WHERE version = ?
|
||||
`
|
||||
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
|
||||
&migrationHistory.Version,
|
||||
&migrationHistory.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &migrationHistory, nil
|
||||
}
|
Reference in New Issue
Block a user