Commit 695fb1e0 authored by Steven's avatar Steven

chore: update migration history store

parent 21ad6cc8
...@@ -2,21 +2,11 @@ package mysql ...@@ -2,21 +2,11 @@ package mysql
import ( import (
"context" "context"
)
type MigrationHistory struct {
Version string
CreatedTs int64
}
type MigrationHistoryUpsert struct { "github.com/usememos/memos/store"
Version string )
}
type MigrationHistoryFind struct {
}
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" query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` ORDER BY `created_ts` DESC"
rows, err := d.db.QueryContext(ctx, query) rows, err := d.db.QueryContext(ctx, query)
if err != nil { if err != nil {
...@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi ...@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
} }
defer rows.Close() defer rows.Close()
list := make([]*MigrationHistory, 0) list := make([]*store.MigrationHistory, 0)
for rows.Next() { for rows.Next() {
var migrationHistory MigrationHistory var migrationHistory store.MigrationHistory
if err := rows.Scan( if err := rows.Scan(
&migrationHistory.Version, &migrationHistory.Version,
&migrationHistory.CreatedTs, &migrationHistory.CreatedTs,
...@@ -44,14 +34,14 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi ...@@ -44,14 +34,14 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
return list, nil 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` = ?" stmt := "INSERT INTO `migration_history` (`version`) VALUES (?) ON DUPLICATE KEY UPDATE `version` = ?"
_, err := d.db.ExecContext(ctx, stmt, upsert.Version, upsert.Version) _, err := d.db.ExecContext(ctx, stmt, upsert.Version, upsert.Version)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var migrationHistory MigrationHistory var migrationHistory store.MigrationHistory
stmt = "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` WHERE `version` = ?" stmt = "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` WHERE `version` = ?"
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan( if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
&migrationHistory.Version, &migrationHistory.Version,
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/usememos/memos/server/version" "github.com/usememos/memos/server/version"
"github.com/usememos/memos/store"
) )
const ( const (
...@@ -76,7 +77,7 @@ func (d *DB) nonProdMigrate(ctx context.Context) error { ...@@ -76,7 +77,7 @@ func (d *DB) nonProdMigrate(ctx context.Context) error {
func (d *DB) prodMigrate(ctx context.Context) error { func (d *DB) prodMigrate(ctx context.Context) error {
currentVersion := version.GetCurrentVersion(d.profile.Mode) 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 there is no migration history, we should apply the latest schema.
if err != nil || len(migrationHistoryList) == 0 { if err != nil || len(migrationHistoryList) == 0 {
buf, err := migrationFS.ReadFile("migration/prod/" + latestSchemaFileName) buf, err := migrationFS.ReadFile("migration/prod/" + latestSchemaFileName)
...@@ -88,7 +89,7 @@ func (d *DB) prodMigrate(ctx context.Context) error { ...@@ -88,7 +89,7 @@ func (d *DB) prodMigrate(ctx context.Context) error {
if _, err := d.db.ExecContext(ctx, stmt); err != nil { if _, err := d.db.ExecContext(ctx, stmt); err != nil {
return errors.Errorf("failed to exec SQL %s: %s", stmt, err) 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, Version: currentVersion,
}); err != nil { }); err != nil {
return errors.Wrap(err, "failed to upsert migration history") return errors.Wrap(err, "failed to upsert migration history")
...@@ -145,7 +146,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str ...@@ -145,7 +146,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
// Upsert the newest version to migration_history. // Upsert the newest version to migration_history.
version := minorVersion + ".0" 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) return errors.Wrapf(err, "failed to upsert migration history with version: %s", version)
} }
......
...@@ -2,21 +2,11 @@ package sqlite ...@@ -2,21 +2,11 @@ package sqlite
import ( import (
"context" "context"
)
type MigrationHistory struct {
Version string
CreatedTs int64
}
type MigrationHistoryUpsert struct { "github.com/usememos/memos/store"
Version string )
}
type MigrationHistoryFind struct {
}
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" query := "SELECT `version`, `created_ts` FROM `migration_history` ORDER BY `created_ts` DESC"
rows, err := d.db.QueryContext(ctx, query) rows, err := d.db.QueryContext(ctx, query)
if err != nil { if err != nil {
...@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi ...@@ -24,9 +14,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
} }
defer rows.Close() defer rows.Close()
list := make([]*MigrationHistory, 0) list := make([]*store.MigrationHistory, 0)
for rows.Next() { for rows.Next() {
var migrationHistory MigrationHistory var migrationHistory store.MigrationHistory
if err := rows.Scan( if err := rows.Scan(
&migrationHistory.Version, &migrationHistory.Version,
&migrationHistory.CreatedTs, &migrationHistory.CreatedTs,
...@@ -44,7 +34,7 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi ...@@ -44,7 +34,7 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFi
return list, nil 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 := ` stmt := `
INSERT INTO migration_history ( INSERT INTO migration_history (
version version
...@@ -55,7 +45,7 @@ func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistor ...@@ -55,7 +45,7 @@ func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistor
version=EXCLUDED.version version=EXCLUDED.version
RETURNING version, created_ts RETURNING version, created_ts
` `
var migrationHistory MigrationHistory var migrationHistory store.MigrationHistory
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan( if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
&migrationHistory.Version, &migrationHistory.Version,
&migrationHistory.CreatedTs, &migrationHistory.CreatedTs,
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/usememos/memos/server/version" "github.com/usememos/memos/server/version"
"github.com/usememos/memos/store"
) )
//go:embed migration //go:embed migration
...@@ -33,7 +34,7 @@ func (d *DB) Migrate(ctx context.Context) error { ...@@ -33,7 +34,7 @@ func (d *DB) Migrate(ctx context.Context) error {
return errors.Wrap(err, "failed to apply latest schema") return errors.Wrap(err, "failed to apply latest schema")
} }
// Upsert the newest version to migration_history. // Upsert the newest version to migration_history.
if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ if _, err := d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: currentVersion, Version: currentVersion,
}); err != nil { }); err != nil {
return errors.Wrap(err, "failed to upsert migration history") return errors.Wrap(err, "failed to upsert migration history")
...@@ -43,7 +44,7 @@ func (d *DB) Migrate(ctx context.Context) error { ...@@ -43,7 +44,7 @@ func (d *DB) Migrate(ctx context.Context) error {
} }
} else { } else {
// If db file exists, we should check if we need to migrate the database. // 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 { if err != nil {
return errors.Wrap(err, "failed to find migration history") return errors.Wrap(err, "failed to find migration history")
} }
...@@ -53,7 +54,7 @@ func (d *DB) Migrate(ctx context.Context) error { ...@@ -53,7 +54,7 @@ func (d *DB) Migrate(ctx context.Context) error {
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil { if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
return errors.Wrapf(err, "failed to apply version %s migration", minorVersion) return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
} }
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ _, err := d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: currentVersion, Version: currentVersion,
}) })
if err != nil { if err != nil {
...@@ -162,7 +163,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str ...@@ -162,7 +163,7 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
// Upsert the newest version to migration_history. // Upsert the newest version to migration_history.
version := minorVersion + ".0" version := minorVersion + ".0"
if _, err = d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ if _, err = d.UpsertMigrationHistory(ctx, &store.UpsertMigrationHistory{
Version: version, Version: version,
}); err != nil { }); err != nil {
return errors.Wrapf(err, "failed to upsert migration history with version: %s", version) return errors.Wrapf(err, "failed to upsert migration history with version: %s", version)
......
...@@ -20,6 +20,10 @@ type Driver interface { ...@@ -20,6 +20,10 @@ type Driver interface {
// current file is driver // current file is driver
GetCurrentDBSize(ctx context.Context) (int64, error) 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. // Activity model related methods.
CreateActivity(ctx context.Context, create *Activity) (*Activity, error) CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
ListActivities(ctx context.Context, find *FindActivity) ([]*Activity, error) ListActivities(ctx context.Context, find *FindActivity) ([]*Activity, error)
......
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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment