mirror of
https://github.com/usememos/memos.git
synced 2024-09-11 03:55:30 +03:00
chore: update migration history store
This commit is contained in:
parent
21ad6cc871
commit
695fb1e0ca
@ -2,21 +2,11 @@ package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
Version string
|
||||
CreatedTs int64
|
||||
}
|
||||
|
||||
type MigrationHistoryUpsert struct {
|
||||
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"
|
||||
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,
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -2,21 +2,11 @@ package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
Version string
|
||||
CreatedTs int64
|
||||
}
|
||||
|
||||
type MigrationHistoryUpsert struct {
|
||||
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"
|
||||
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,
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
25
store/migration_history.go
Normal file
25
store/migration_history.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user