Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: update migration history store

Steven695fb1e0ca63
diff --git a/store/db/mysql/migration_history.go b/store/db/mysql/migration_history.go
index 4dc79c132..0f7bd1a77 100644
--- a/store/db/mysql/migration_history.go
+++ b/store/db/mysql/migration_history.go
@@ -2,21 +2,11 @@ package mysql
import (
"context"
-)
-
-type MigrationHistory struct {
- Version string
- CreatedTs int64
-}
-type MigrationHistoryUpsert struct {
- Version string
-}
-
-type MigrationHistoryFind struct {
-}
+ "github.com/usememos/memos/store"
+)
-func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*MigrationHistory, error) {
+func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` ORDER BY `created_ts` DESC"
rows, err := d.db.QueryContext(ctx, query)
if err != nil {
@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
}
defer rows.Close()
- list := make([]*MigrationHistory, 0)
+ list := make([]*store.MigrationHistory, 0)
for rows.Next() {
- var migrationHistory MigrationHistory
+ var migrationHistory store.MigrationHistory
if err := rows.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
@@ -44,14 +34,14 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
return list, nil
}
-func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
+func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.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
+ var migrationHistory store.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,
diff --git a/store/db/mysql/migrator.go b/store/db/mysql/migrator.go
index 6b5c049b6..94bd540c5 100644
--- a/store/db/mysql/migrator.go
+++ b/store/db/mysql/migrator.go
@@ -12,6 +12,7 @@ import (
"github.com/pkg/errors"
"github.com/usememos/memos/server/version"
+ "github.com/usememos/memos/store"
)
const (
@@ -76,7 +77,7 @@ func (d *DB) nonProdMigrate(ctx context.Context) error {
func (d *DB) prodMigrate(ctx context.Context) error {
currentVersion := version.GetCurrentVersion(d.profile.Mode)
- migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
+ migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &store.FindMigrationHistory{})
// If there is no migration history, we should apply the latest schema.
if err != nil || len(migrationHistoryList) == 0 {
buf, err := migrationFS.ReadFile("migration/prod/" + latestSchemaFileName)
@@ -88,7 +89,7 @@ func (d *DB) prodMigrate(ctx context.Context) error {
if _, err := d.db.ExecContext(ctx, stmt); err != nil {
return errors.Errorf("failed to exec SQL %s: %s", stmt, err)
}
- if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
+ if _, err := d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: currentVersion,
}); err != nil {
return errors.Wrap(err, "failed to upsert migration history")
@@ -145,7 +146,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
// Upsert the newest version to migration_history.
version := minorVersion + ".0"
- if _, err = d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{Version: version}); err != nil {
+ if _, err = d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{Version: version}); err != nil {
return errors.Wrapf(err, "failed to upsert migration history with version: %s", version)
}
diff --git a/store/db/sqlite/migration_history.go b/store/db/sqlite/migration_history.go
index 922500d70..b7ec01b24 100644
--- a/store/db/sqlite/migration_history.go
+++ b/store/db/sqlite/migration_history.go
@@ -2,21 +2,11 @@ package sqlite
import (
"context"
-)
-
-type MigrationHistory struct {
- Version string
- CreatedTs int64
-}
-type MigrationHistoryUpsert struct {
- Version string
-}
-
-type MigrationHistoryFind struct {
-}
+ "github.com/usememos/memos/store"
+)
-func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*MigrationHistory, error) {
+func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
query := "SELECT `version`, `created_ts` FROM `migration_history` ORDER BY `created_ts` DESC"
rows, err := d.db.QueryContext(ctx, query)
if err != nil {
@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
}
defer rows.Close()
- list := make([]*MigrationHistory, 0)
+ list := make([]*store.MigrationHistory, 0)
for rows.Next() {
- var migrationHistory MigrationHistory
+ var migrationHistory store.MigrationHistory
if err := rows.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
@@ -44,7 +34,7 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
return list, nil
}
-func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
+func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
stmt := `
INSERT INTO migration_history (
version
@@ -55,7 +45,7 @@ func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistor
version=EXCLUDED.version
RETURNING version, created_ts
`
- var migrationHistory MigrationHistory
+ var migrationHistory store.MigrationHistory
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
diff --git a/store/db/sqlite/migrator.go b/store/db/sqlite/migrator.go
index 7aeb4aaac..7d14e851c 100644
--- a/store/db/sqlite/migrator.go
+++ b/store/db/sqlite/migrator.go
@@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/usememos/memos/server/version"
+ "github.com/usememos/memos/store"
)
//go:embed migration
@@ -33,7 +34,7 @@ func (d *DB) Migrate(ctx context.Context) error {
return errors.Wrap(err, "failed to apply latest schema")
}
// Upsert the newest version to migration_history.
- if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
+ if _, err := d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: currentVersion,
}); err != nil {
return errors.Wrap(err, "failed to upsert migration history")
@@ -43,7 +44,7 @@ func (d *DB) Migrate(ctx context.Context) error {
}
} else {
// If db file exists, we should check if we need to migrate the database.
- migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
+ migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &store.FindMigrationHistory{})
if err != nil {
return errors.Wrap(err, "failed to find migration history")
}
@@ -53,7 +54,7 @@ func (d *DB) Migrate(ctx context.Context) error {
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
}
- _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
+ _, err := d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: currentVersion,
})
if err != nil {
@@ -162,7 +163,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
// Upsert the newest version to migration_history.
version := minorVersion + ".0"
- if _, err = d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
+ if _, err = d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: version,
}); err != nil {
return errors.Wrapf(err, "failed to upsert migration history with version: %s", version)
diff --git a/store/driver.go b/store/driver.go
index f434fecb7..6c090dbac 100644
--- a/store/driver.go
+++ b/store/driver.go
@@ -20,6 +20,10 @@ type Driver interface {
// current file is driver
GetCurrentDBSize(ctx context.Context) (int64, error)
+ // MigrationHistory model related methods.
+ FindMigrationHistoryList(ctx context.Context, find *FindMigrationHistory) ([]*MigrationHistory, error)
+ UpsertMigrationHistory(ctx context.Context, upsert *UpsertMigrationHistory) (*MigrationHistory, error)
+
// Activity model related methods.
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
ListActivities(ctx context.Context, find *FindActivity) ([]*Activity, error)
diff --git a/store/migration_history.go b/store/migration_history.go
new file mode 100644
index 000000000..c1fb897f5
--- /dev/null
+++ b/store/migration_history.go
@@ -0,0 +1,25 @@
+package store
+
+import (
+ "context"
+)
+
+type MigrationHistory struct {
+ Version string
+ CreatedTs int64
+}
+
+type UpsertMigrationHistory struct {
+ Version string
+}
+
+type FindMigrationHistory struct {
+}
+
+func (s *Store) FindMigrationHistoryList(ctx context.Context, find *FindMigrationHistory) ([]*MigrationHistory, error) {
+ return s.driver.FindMigrationHistoryList(ctx, find)
+}
+
+func (s *Store) UpsertMigrationHistory(ctx context.Context, upsert *UpsertMigrationHistory) (*MigrationHistory, error) {
+ return s.driver.UpsertMigrationHistory(ctx, upsert)
+}