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

Code before:
			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)
	}
	stmt := string(buf)
	if err := db.execute(ctx, stmt); err != nil {
		return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err)
	}

Review comment (suggestion):
```suggestion
			// In demo mode, we should seed the database.
```

Code after:
			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)
	}
	stmt := string(buf)
	if err := db.execute(ctx, stmt); err != nil {
		return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err)
	}