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: fix sqlite migrator

Stevenc0422dea5b8c
diff --git a/store/db/db.go b/store/db/db.go
index 4367ccf6d..4ba27c4c0 100644
--- a/store/db/db.go
+++ b/store/db/db.go
@@ -15,17 +15,17 @@ func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
var err error
// As mysql driver is not fully implemented, we use sqlite for now in prod mode.
- if profile.Mode == "prod" {
+ if profile.Mode == "prod" && profile.Driver != "sqlite" {
+ return nil, errors.New("Only SQLite is supported in prod mode")
+ }
+
+ switch profile.Driver {
+ case "sqlite":
driver, err = sqlite.NewDB(profile)
- } else {
- switch profile.Driver {
- case "sqlite":
- driver, err = sqlite.NewDB(profile)
- case "mysql":
- driver, err = mysql.NewDB(profile)
- default:
- return nil, errors.New("unknown db driver")
- }
+ case "mysql":
+ driver, err = mysql.NewDB(profile)
+ default:
+ return nil, errors.New("unknown db driver")
}
if err != nil {
return nil, errors.Wrap(err, "failed to create db driver")
diff --git a/store/db/sqlite/migrator.go b/store/db/sqlite/migrator.go
index c6b95f884..7aeb4aaac 100644
--- a/store/db/sqlite/migrator.go
+++ b/store/db/sqlite/migrator.go
@@ -23,6 +23,7 @@ var seedFS embed.FS
// Migrate applies the latest schema to the database.
func (d *DB) Migrate(ctx context.Context) error {
+ currentVersion := version.GetCurrentVersion(d.profile.Mode)
if d.profile.Mode == "prod" {
_, err := os.Stat(d.profile.DSN)
if err != nil {
@@ -31,17 +32,27 @@ func (d *DB) Migrate(ctx context.Context) error {
if err := d.applyLatestSchema(ctx); err != nil {
return errors.Wrap(err, "failed to apply latest schema")
}
+ // Upsert the newest version to migration_history.
+ if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
+ Version: currentVersion,
+ }); err != nil {
+ return errors.Wrap(err, "failed to upsert migration history")
+ }
} else {
return errors.Wrap(err, "failed to get db file stat")
}
} else {
// If db file exists, we should check if we need to migrate the database.
- currentVersion := version.GetCurrentVersion(d.profile.Mode)
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
if err != nil {
return errors.Wrap(err, "failed to find migration history")
}
+ // If no migration history, we should apply the latest version migration and upsert the migration history.
if len(migrationHistoryList) == 0 {
+ minorVersion := version.GetMinorVersion(currentVersion)
+ if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
+ return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
+ }
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
Version: currentVersion,
})
@@ -60,7 +71,6 @@ func (d *DB) Migrate(ctx context.Context) error {
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
minorVersionList := getMinorVersionList()
-
// backup the raw database file before migration
rawBytes, err := os.ReadFile(d.profile.DSN)
if err != nil {
@@ -71,7 +81,6 @@ func (d *DB) Migrate(ctx context.Context) error {
return errors.Wrap(err, "failed to write raw database file")
}
println("succeed to copy a backup database file")
-
println("start migrate")
for _, minorVersion := range minorVersionList {
normalizedVersion := minorVersion + ".0"