chore: update migration history store
This commit is contained in:
parent
21ad6cc871
commit
695fb1e0ca
6 changed files with 52 additions and 41 deletions
|
|
@ -2,21 +2,11 @@ package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MigrationHistory struct {
|
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
|
||||||
Version string
|
|
||||||
CreatedTs int64
|
|
||||||
}
|
|
||||||
|
|
||||||
type MigrationHistoryUpsert struct {
|
|
||||||
Version string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MigrationHistoryFind struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*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
|
||||||
}
|
}
|
||||||
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
|
||||||
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 (
|
||||||
"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 {
|
||||||
|
|
||||||
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 {
|
||||||
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
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MigrationHistory struct {
|
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
|
||||||
Version string
|
|
||||||
CreatedTs int64
|
|
||||||
}
|
|
||||||
|
|
||||||
type MigrationHistoryUpsert struct {
|
|
||||||
Version string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MigrationHistoryFind struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*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
|
||||||
}
|
}
|
||||||
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
|
||||||
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
|
||||||
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 (
|
||||||
"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 {
|
||||||
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 {
|
||||||
}
|
}
|
||||||
} 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 {
|
||||||
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
|
||||||
|
|
||||||
// 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 {
|
||||||
// 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)
|
||||||
|
|
|
||||||
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 a new issue