MSP-623 refactoring initialize migration

This commit is contained in:
DeniskaRediska committed 2022-06-06 15:44:28 +03:00
1 parent 7b5ca9d12c
commit a7feb4e234
2 files changed
+120 -132

No files matched your search

+120 -70
View File
@@ -1,17 +1,17 @@
package migrations package migrations
import ( import (
"context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"github.com/integration-system/isp-lib/v2/database" "github.com/pkg/errors"
"github.com/integration-system/isp-lib/v2/structure"
"github.com/pressly/goose" "github.com/pressly/goose"
"isp-system-service/controller"
"isp-system-service/domain" "isp-system-service/domain"
"isp-system-service/entity"
) )
const ( const (
@@ -25,105 +25,155 @@ const (
initFile = "init.json" initFile = "init.json"
) )
var DatabaseConfig structure.DBConfiguration
func init() { func init() {
goose.AddMigration(Up, Down) goose.AddMigration(Initialize.Up, Initialize.Down)
} }
func Up(tx *sql.Tx) error { type IRedis interface {
path := filepath.Join(database.ResolveMigrationsDirectory(), initFile) SetApplicationToken(ctx context.Context, req entity.RedisSetToken) error
}
var Initialize = &initializeMigration{}
type initializeMigration struct {
redis IRedis
migrationDir string
schema string
}
type identity struct {
domainId int
appId int
serviceId int
}
func (i *initializeMigration) SetParams(redis IRedis, migrationDir string, schema string) {
i.redis = redis
i.migrationDir = migrationDir
i.schema = schema
}
func (i *initializeMigration) Up(tx *sql.Tx) error {
path := filepath.Join(i.migrationDir, initFile)
bytes, err := ioutil.ReadFile(path) bytes, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return errors.WithMessage(err, "read file")
} }
var list []domain.DomainWithServices list := make([]domain.DomainWithService, 0)
if err := json.Unmarshal(bytes, &list); err != nil { err = json.Unmarshal(bytes, &list)
return err if err != nil {
return errors.WithMessage(err, "json unmarshal")
} }
schema := DatabaseConfig.Schema lastDomainId := 0
lastDomainId := int64(0) lastServiceId := 0
lastServiceId := int64(0) lastAppId := 0
lastAppId := int64(0)
for _, domainInfo := range list { for _, domainInfo := range list {
domainId := int64(0) last, err := i.insert(tx, domainInfo)
if domainId, err = insertNode(tx, insertDomain, true, schema, domainInfo.Id, domainInfo.Name, domainInfo.Description, 1); err != nil { if err != nil {
return err return errors.WithMessage(err, "")
} }
if domainId > lastDomainId { if last.domainId > lastDomainId {
lastDomainId = domainId lastDomainId = last.domainId
} }
if last.serviceId > lastServiceId {
for _, service := range domainInfo.Services { lastServiceId = last.serviceId
serviceId := int64(0) }
if serviceId, err = insertNode(tx, insertService, true, schema, service.Id, service.Name, service.Description, domainId); err != nil { if last.appId > lastAppId {
return err lastAppId = last.appId
}
if serviceId > lastServiceId {
lastServiceId = serviceId
}
for _, app := range service.Apps {
appId := int64(0)
if appId, err = insertNode(tx, insertApp, true, schema, app.Id, app.Name, app.Description, serviceId, app.Type); err != nil {
return err
}
if appId > lastAppId {
lastAppId = appId
}
for _, t := range app.Tokens {
if _, err := insertNode(tx, insertToken, false, schema, t.Token, appId, t.ExpireTime); err != nil {
return err
} else {
idMap := map[string]interface{}{
controller.SystemIdentityFieldInDb: 1,
controller.DomainIdentityFieldInDb: domainId,
controller.ServiceIdentityFieldInDb: serviceId,
controller.ApplicationIdentityFieldInDb: appId,
}
if err := controller.Token.SetIdentityMapForTokenV2(t.Token, t.ExpireTime, idMap); err != nil {
return err
}
}
}
}
} }
} }
if err := setSeq(tx, schema, "domain", lastDomainId); err != nil { err = i.setSeq(tx, "domain", lastDomainId)
return err if err != nil {
} else if err := setSeq(tx, schema, "service", lastServiceId); err != nil { return errors.WithMessage(err, "set domain")
return err }
} else if err := setSeq(tx, schema, "application", lastAppId); err != nil {
return err err = i.setSeq(tx, "service", lastServiceId)
if err != nil {
return errors.WithMessage(err, "set service")
}
err = i.setSeq(tx, "application", lastAppId)
if err != nil {
return errors.WithMessage(err, "set application")
} }
return nil return nil
} }
func Down(tx *sql.Tx) error { func (i *initializeMigration) Down(tx *sql.Tx) error {
return nil return nil
} }
func insertNode(tx *sql.Tx, query string, scan bool, args ...interface{}) (int64, error) { func (i *initializeMigration) insert(tx *sql.Tx, domainInfo domain.DomainWithService) (*identity, error) {
var id int64 domainId, err := i.insertNode(tx, insertDomain, true, i.schema, domainInfo.Id, domainInfo.Name, domainInfo.Description, 1)
if err != nil {
return nil, errors.WithMessage(err, "insert domain")
}
serviceId := 0
appId := 0
for _, service := range domainInfo.Services {
serviceId, err = i.insertNode(tx, insertService, true, i.schema, service.Id, service.Name, service.Description, domainId)
if err != nil {
return nil, errors.WithMessage(err, "insert service")
}
for _, app := range service.Apps {
appId, err = i.insertNode(tx, insertApp, true, i.schema, app.Id, app.Name, app.Description, serviceId, app.Type)
if err != nil {
return nil, errors.WithMessage(err, "insert application")
}
for _, t := range app.Tokens {
_, err = i.insertNode(tx, insertToken, false, i.schema, t.Token, appId, t.ExpireTime)
if err != nil {
return nil, errors.WithMessage(err, "insert token")
}
err = i.redis.SetApplicationToken(context.Background(), entity.RedisSetToken{
Token: t.Token,
ExpireTime: t.ExpireTime,
DomainIdentity: domainId,
ServiceIdentity: serviceId,
ApplicationIdentity: appId,
})
if err != nil {
return nil, errors.WithMessage(err, "redis set token")
}
}
}
}
return &identity{
domainId: domainId,
appId: appId,
serviceId: serviceId,
}, nil
}
func (i *initializeMigration) insertNode(tx *sql.Tx, query string, scan bool, args ...interface{}) (int, error) {
var id int
query = fmt.Sprintf(query, args...) query = fmt.Sprintf(query, args...)
row := tx.QueryRow(query) row := tx.QueryRow(query)
if scan { if scan {
if err := row.Scan(&id); err != nil { err := row.Scan(&id)
return 0, err if err != nil {
return 0, errors.WithMessage(err, "row scan")
} }
} }
return id, nil return id, nil
} }
func setSeq(tx *sql.Tx, schema, table string, value int64) error { func (i *initializeMigration) setSeq(tx *sql.Tx, table string, value int) error {
q := fmt.Sprintf(setSeqQuery, schema, table, value) q := fmt.Sprintf(setSeqQuery, i.schema, table, value)
_, err := tx.Exec(q) _, err := tx.Exec(q)
if err != nil {
return errors.WithMessage(err, "exec db")
}
return err return nil
} }
-62
View File
@@ -24,67 +24,5 @@
] ]
} }
] ]
},
{
"id": 2,
"name": "citymodule",
"description": "",
"services": [
{
"id": 2,
"name": "citymodule_service",
"description": "",
"apps": [
{
"id": 2,
"name": "cityservice",
"description": "",
"type": "SYSTEM",
"tokens": [
{
"token": "{{ isp_service_cityservice_token }}",
"expireTime": -1
}
]
},
{
"id": 3,
"name": "citymodule",
"description": "",
"type": "MOBILE",
"tokens": [
{
"token": "{{ isp_service_citymodule_token }}",
"expireTime": -1
}
]
},
{
"id": 4,
"name": "cityservice_operator_ui",
"description": "",
"type": "SYSTEM",
"tokens": [
{
"token": "{{ isp_service_cityservice_operator_ui_token }}",
"expireTime": -1
}
]
},
{
"id": 5,
"name": "cityservice_web_portal",
"description": "",
"type": "SYSTEM",
"tokens": [
{
"token": "{{ isp_service_cityservice_web_portal_token }}",
"expireTime": -1
}
]
}
]
}
]
} }
] ]