-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move model update to a db migration.
- Loading branch information
Showing
4 changed files
with
72 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mig004 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/aserto-dev/azm/model" | ||
v3 "github.com/aserto-dev/azm/v3" | ||
dsm3 "github.com/aserto-dev/go-directory/aserto/directory/model/v3" | ||
"github.com/aserto-dev/go-edge-ds/pkg/bdb" | ||
"github.com/rs/zerolog" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
// mig004 | ||
// | ||
// load model from manifest and write it back to the db. | ||
const ( | ||
Version string = "0.0.4" | ||
) | ||
|
||
func Migrate(log *zerolog.Logger, roDB, rwDB *bolt.DB) error { | ||
logger := log.With().Str("version", Version).Logger() | ||
logger.Info().Msg("StartMigration") | ||
|
||
// skip when roDB is nil. | ||
if roDB == nil { | ||
logger.Debug().Msg("SKIP") | ||
return nil | ||
} | ||
|
||
ctx := context.Background() | ||
m, err := loadModel(ctx, roDB) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := rwDB.Update(func(tx *bolt.Tx) error { | ||
_, err := bdb.SetAny[model.Model](ctx, tx, bdb.ManifestPath, bdb.ModelKey, m) | ||
return err | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
logger.Info().Msg("FinishedMigration") | ||
return nil | ||
} | ||
|
||
func loadModel(ctx context.Context, roDB *bolt.DB) (*model.Model, error) { | ||
var m *model.Model | ||
if err := roDB.View(func(rtx *bolt.Tx) error { | ||
manifestBody, err := bdb.Get[dsm3.Body](ctx, rtx, bdb.ManifestPath, bdb.BodyKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m, err = v3.Load(bytes.NewReader(manifestBody.Data)) | ||
return err | ||
}); err != nil { | ||
return m, err | ||
} | ||
|
||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters