mirror of
https://github.com/Luzifer/go-latestver.git
synced 2024-11-08 23:20:03 +00:00
Add migration tooling
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
994d2d2aba
commit
869eb4c3c5
3 changed files with 69 additions and 0 deletions
|
@ -24,6 +24,10 @@ type (
|
|||
logwrap struct {
|
||||
l *io.PipeWriter
|
||||
}
|
||||
|
||||
migrator interface {
|
||||
Migrate(dest *Client) error
|
||||
}
|
||||
)
|
||||
|
||||
func NewClient(dbtype, dsn string) (*Client, error) {
|
||||
|
@ -71,6 +75,19 @@ func NewClient(dbtype, dsn string) (*Client, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
func (c Client) Migrate(dest *Client) error {
|
||||
for _, m := range []migrator{
|
||||
c.Catalog,
|
||||
c.Logs,
|
||||
} {
|
||||
if err := m.Migrate(dest); err != nil {
|
||||
return errors.Wrap(err, "executing migrating")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Client) initDB() error {
|
||||
for name, fn := range map[string]func() error{
|
||||
"catalogMeta": c.Catalog.ensureTable,
|
||||
|
|
|
@ -77,6 +77,21 @@ func (c CatalogMetaStore) GetMeta(ce *CatalogEntry) (*CatalogMeta, error) {
|
|||
return out, errors.Wrap(err, "querying metadata")
|
||||
}
|
||||
|
||||
func (c CatalogMetaStore) Migrate(dest *Client) error {
|
||||
var metas []*CatalogMeta
|
||||
if err := c.c.db.Find(&metas).Error; err != nil {
|
||||
return errors.Wrap(err, "listing meta entries")
|
||||
}
|
||||
|
||||
for _, m := range metas {
|
||||
if err := dest.Catalog.PutMeta(m); err != nil {
|
||||
return errors.Wrap(err, "storing meta to dest database")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CatalogMetaStore) PutMeta(cm *CatalogMeta) error {
|
||||
return errors.Wrap(
|
||||
c.c.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(cm).Error,
|
||||
|
@ -103,6 +118,21 @@ func (l LogStore) ListForCatalogEntry(ce *CatalogEntry, num, page int) ([]LogEnt
|
|||
return l.listWithFilter(l.c.db.Where(&LogEntry{CatalogName: ce.Name, CatalogTag: ce.Tag}), num, page)
|
||||
}
|
||||
|
||||
func (l LogStore) Migrate(dest *Client) error {
|
||||
var logs []*LogEntry
|
||||
if err := l.c.db.Find(&logs).Error; err != nil {
|
||||
return errors.Wrap(err, "listing log entries")
|
||||
}
|
||||
|
||||
for _, l := range logs {
|
||||
if err := dest.Logs.Add(l); err != nil {
|
||||
return errors.Wrap(err, "storing log to dest database")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l LogStore) listWithFilter(filter *gorm.DB, num, page int) ([]LogEntry, error) {
|
||||
var out []LogEntry
|
||||
return out, errors.Wrap(
|
||||
|
|
22
internal/migrate/migrate.go
Normal file
22
internal/migrate/migrate.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/Luzifer/go-latestver/internal/database"
|
||||
)
|
||||
|
||||
func main() {
|
||||
src, err := database.NewClient("sqlite", "")
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "opening src database"))
|
||||
}
|
||||
dest, err := database.NewClient("mysql", "")
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "opening dest database"))
|
||||
}
|
||||
|
||||
if err := src.Migrate(dest); err != nil {
|
||||
panic(errors.Wrap(err, "migrating to dest database"))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue