Repository: usememos/memos
File: store/db/db.go (Go)

Code before:
			}
			backupDBFilePath := fmt.Sprintf("%s/memos_%s_%d_backup.db", db.profile.Data, db.profile.Version, time.Now().Unix())
			if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil {
				return fmt.Errorf("failed to write raw database file, err: %w", err)
			}
			println("succeed to copy a backup database file")

			println("start migrate")
			for _, minorVersion := range minorVersionList {
				normalizedVersion := minorVersion + ".0"
				if version.IsVersionGreaterThan(normalizedVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {
					println("applying migration for", normalizedVersion)
					if err := db.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
						return fmt.Errorf("failed to apply minor version migration: %w", err)
					}
				}
			}
			println("end migrate")

			// remove the created backup db file after migrate succeed
			if err := os.Remove(backupDBFilePath); err != nil {
				println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
			}
		}
	} else {
		// In dev mode, we should migrate the database.
		if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
			if err := db.applyLatestSchema(ctx); err != nil {
				return fmt.Errorf("failed to apply latest schema: %w", err)
			}
			// In demo mode, we should migrate and seed the database.
			if db.profile.Mode == "demo" {
				if err := db.seed(ctx); err != nil {
					return fmt.Errorf("failed to seed: %w", err)
				}
			}
		}
	}

	return nil
}

const (
	latestSchemaFileName = "LATEST__SCHEMA.sql"
)

func (db *DB) applyLatestSchema(ctx context.Context) error {
	latestSchemaPath := fmt.Sprintf("%s/%s/%s", "migration", db.profile.Mode, latestSchemaFileName)
	buf, err := migrationFS.ReadFile(latestSchemaPath)
	if err != nil {
		return fmt.Errorf("failed to read latest schema %q, error %w", latestSchemaPath, err)

Review comment (suggestion):
```suggestion
		// In non-prod mode, we should migrate the database.
```

Code after:
			}
			backupDBFilePath := fmt.Sprintf("%s/memos_%s_%d_backup.db", db.profile.Data, db.profile.Version, time.Now().Unix())
			if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil {
				return fmt.Errorf("failed to write raw database file, err: %w", err)
			}
			println("succeed to copy a backup database file")

			println("start migrate")
			for _, minorVersion := range minorVersionList {
				normalizedVersion := minorVersion + ".0"
				if version.IsVersionGreaterThan(normalizedVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {
					println("applying migration for", normalizedVersion)
					if err := db.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
						return fmt.Errorf("failed to apply minor version migration: %w", err)
					}
				}
			}
			println("end migrate")

			// remove the created backup db file after migrate succeed
			if err := os.Remove(backupDBFilePath); err != nil {
				println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
			}
		}
	} else {
		// In non-prod mode, we should migrate the database.
		if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
			if err := db.applyLatestSchema(ctx); err != nil {
				return fmt.Errorf("failed to apply latest schema: %w", err)
			}
			// In demo mode, we should seed the database.
			if db.profile.Mode == "demo" {
				if err := db.seed(ctx); err != nil {
					return fmt.Errorf("failed to seed: %w", err)
				}
			}
		}
	}

	return nil
}

const (
	latestSchemaFileName = "LATEST__SCHEMA.sql"
)

func (db *DB) applyLatestSchema(ctx context.Context) error {
	latestSchemaPath := fmt.Sprintf("%s/%s/%s", "migration", db.profile.Mode, latestSchemaFileName)
	buf, err := migrationFS.ReadFile(latestSchemaPath)
	if err != nil {
		return fmt.Errorf("failed to read latest schema %q, error %w", latestSchemaPath, err)