Skip to content

Commit

Permalink
dump schemas.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Oct 2, 2023
1 parent 6ed74da commit 0321a8f
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

//
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 0321a8f

Please sign in to comment.