Skip to content

Commit

Permalink
feat(migration): OneToTwo basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed May 27, 2020
1 parent 321e0d3 commit 4159a25
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
88 changes: 83 additions & 5 deletions config/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
package migrate

import (
"fmt"
"os"
"path/filepath"

"github.com/qri-io/ioes"
"github.com/qri-io/qri/config"
qerr "github.com/qri-io/qri/errors"
)

// RunMigrations checks to see if any migrations runs them
func RunMigrations(streams ioes.IOStreams, cfg *config.Config) (migrated bool, err error) {
func RunMigrations(streams ioes.IOStreams, cfg *config.Config) (err error) {
if cfg.Revision != config.CurrentConfigRevision {
streams.PrintErr("migrating configuration...")
if err := ZeroToOne(cfg); err != nil {
return false, err
if cfg.Revision == 0 {
if err := ZeroToOne(cfg); err != nil {
return err
}
}
if cfg.Revision == 1 {
if err := OneToTwo(cfg); err != nil {
return err
}
}
streams.PrintErr("done!\n")
return true, nil
}
return false, nil
return nil
}

// ZeroToOne migrates a configuration from Revision Zero (no revision number) to Revision 1
Expand Down Expand Up @@ -49,9 +60,76 @@ func ZeroToOne(cfg *config.Config) error {
}

cfg.Revision = 1

if err := safeWriteConfig(cfg); err != nil {
rollbackConfigWrite(cfg)
return err
}

return nil
}

// OneToTwo migrates a configuration from Revision 1 to Revision 2
func OneToTwo(cfg *config.Config) error {

// TODO(ramfox): qfs migration

// TODO(arqu): config migration
// if err := oneToTwoConfig(cfg); err != nil {
// return err
// }
// cfg.Revision = 2
// if err := cfg.Validate(); err != nil {
// return qerr.New(err, "config is invalid")
// }
// TODO(arqu): config write/rollback
// if err := safeWriteConfig(cfg); err != nil {
// rollbackConfigWrite(cfg)
// return err
// }

// TODO(ramfox): remove original ipfs repo after all migrations were successful

return nil
}

func oneToTwoConfig(cfg *config.Config) error {
return nil
}

func rollbackConfigWrite(cfg *config.Config) {
cfgPath := cfg.Path()
if len(cfgPath) == 0 {
return
}
tmpCfgPath := getTmpConfigFilepath(cfgPath)
if _, err := os.Stat(tmpCfgPath); !os.IsNotExist(err) {
os.Remove(tmpCfgPath)
}
}

func safeWriteConfig(cfg *config.Config) error {
cfgPath := cfg.Path()
if len(cfgPath) == 0 {
return qerr.New(fmt.Errorf("invalid path"), "could not determine config path")
}
tmpCfgPath := getTmpConfigFilepath(cfgPath)
if err := cfg.WriteToFile(tmpCfgPath); err != nil {
return qerr.New(err, fmt.Sprintf("could not write config to path %s", tmpCfgPath))
}
if err := os.Rename(tmpCfgPath, cfgPath); err != nil {
return qerr.New(err, fmt.Sprintf("could not write config to path %s", cfgPath))
}

return nil
}

func getTmpConfigFilepath(cfgPath string) string {
cfgDir := filepath.Dir(cfgPath)
tmpCfgPath := filepath.Join(cfgDir, "config_updated.yaml")
return tmpCfgPath
}

func delIdx(i int, sl []string) []string {
if i < len(sl)-1 {
return append(sl[:i], sl[i+1:]...)
Expand Down
2 changes: 1 addition & 1 deletion dsref/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import (

func TestParallelResolver(t *testing.T) {
t.Skip("TODO(b5)")
}
}
5 changes: 1 addition & 4 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,11 @@ func OptCheckConfigMigrations(cfgPath string) Option {
return fmt.Errorf("no config file to check for migrations")
}

migrated, err := migrate.RunMigrations(o.Streams, o.Cfg)
err := migrate.RunMigrations(o.Streams, o.Cfg)
if err != nil {
return err
}

if migrated {
return o.Cfg.WriteToFile(cfgPath)
}
return nil
}
}
Expand Down

0 comments on commit 4159a25

Please sign in to comment.