From 0321a8f37af4378f1e0711eb7a2fedab2f7e45aa Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Mon, 2 Oct 2023 07:46:06 -0700 Subject: [PATCH] dump schemas. Signed-off-by: Jeff Ortel --- migration/migrate.go | 46 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/migration/migrate.go b/migration/migrate.go index dc2f53125..341048d56 100644 --- a/migration/migrate.go +++ b/migration/migrate.go @@ -6,7 +6,12 @@ import ( liberr "github.com/jortel/go-utils/error" "github.com/konveyor/tackle2-hub/database" "github.com/konveyor/tackle2-hub/model" + "github.com/konveyor/tackle2-hub/nas" + "gopkg.in/yaml.v2" "gorm.io/gorm" + "os" + "path" + "strconv" ) // @@ -80,7 +85,11 @@ func Migrate(migrations []Migration) (err error) { err = liberr.Wrap(err, "version", ver) return } - + err = writeSchema(db, ver) + if err != nil { + err = liberr.Wrap(err, "version", ver) + return + } err = database.Close(db) if err != nil { err = liberr.Wrap(err, "version", ver) @@ -134,3 +143,38 @@ func autoMigrate(db *gorm.DB, models []interface{}) (err error) { } return } + +// +// writeSchema - writes the migrated schema to a file. +func writeSchema(db *gorm.DB, version int) (err error) { + var list []struct { + Type string `gorm:"column:type"` + Name string `gorm:"column:name"` + Table string `gorm:"column:tbl_name"` + RootPage int `gorm:"column:rootpage"` + SQL string `gorm:"column:sql"` + } + db = db.Table("sqlite_schema") + db = db.Order("1, 2") + err = db.Find(&list).Error + if err != nil { + return + } + dir := path.Join( + path.Dir(Settings.Hub.DB.Path), + "migration") + err = nas.MkDir(dir, 0755) + f, err := os.Create(path.Join(dir, strconv.Itoa(version))) + if err != nil { + return + } + defer func() { + _ = f.Close() + }() + b, err := yaml.Marshal(&list) + if err != nil { + return + } + _, err = f.Write(b) + return +}