Skip to content

Commit

Permalink
feat(confix): allow customization of migration plan (#21202)
Browse files Browse the repository at this point in the history
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
(cherry picked from commit 1d7f891)
  • Loading branch information
GAtom22 authored and mergify[bot] committed Aug 13, 2024
1 parent 7373998 commit ffe37f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions tools/confix/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features

* (confix) [#21202](https://github.com/cosmos/cosmos-sdk/pull/21202) Allow customization of migration `PlanBuilder`.

## [v0.1.1](https://github.com/cosmos/cosmos-sdk/releases/tag/tools/confix/v0.1.1) - 2023-12-11

* [#18496](https://github.com/cosmos/cosmos-sdk/pull/18496) Remove invalid non SDK config from app.toml migration templates.
Expand Down
22 changes: 15 additions & 7 deletions tools/confix/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ const (
// MigrationMap defines a mapping from a version to a transformation plan.
type MigrationMap map[string]func(from *tomledit.Document, to, planType string) (transform.Plan, *tomledit.Document)

// loadDestConfigFile is the function signature to load the destination version
// configuration toml file.
type loadDestConfigFile func(to, planType string) (*tomledit.Document, error)

var Migrations = MigrationMap{
"v0.45": NoPlan, // Confix supports only the current supported SDK version. So we do not support v0.44 -> v0.45.
"v0.46": PlanBuilder,
"v0.47": PlanBuilder,
"v0.50": PlanBuilder,
"v0.52": PlanBuilder,
"v0.46": defaultPlanBuilder,
"v0.47": defaultPlanBuilder,
"v0.50": defaultPlanBuilder,
"v0.52": defaultPlanBuilder,
"v2": V2PlanBuilder,
// "v0.xx.x": PlanBuilder, // add specific migration in case of configuration changes in minor versions
// "v0.xx.x": defaultPlanBuilder, // add specific migration in case of configuration changes in minor versions
}

type v2KeyChangesMap map[string][]string
Expand All @@ -53,12 +57,16 @@ var v2KeyChanges = v2KeyChangesMap{
// Add other key mappings as needed
}

func defaultPlanBuilder(from *tomledit.Document, to, planType string) (transform.Plan, *tomledit.Document) {
return PlanBuilder(from, to, planType, LoadLocalConfig)
}

// PlanBuilder is a function that returns a transformation plan for a given diff between two files.
func PlanBuilder(from *tomledit.Document, to, planType string) (transform.Plan, *tomledit.Document) {
func PlanBuilder(from *tomledit.Document, to, planType string, loadFn loadDestConfigFile) (transform.Plan, *tomledit.Document) {
plan := transform.Plan{}
deletedSections := map[string]bool{}

target, err := LoadLocalConfig(to, planType)
target, err := loadFn(to, planType)
if err != nil {
panic(fmt.Errorf("failed to parse file: %w. This file should have been valid", err))
}
Expand Down

0 comments on commit ffe37f8

Please sign in to comment.