MSP-623 refactoring initialize migration
This commit is contained in:
1 parent
7b5ca9d12c
commit
a7feb4e234
2 files changed
+120
-132
No files matched your search
@@ -1,17 +1,17 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/database"
|
||||
"github.com/integration-system/isp-lib/v2/structure"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pressly/goose"
|
||||
"isp-system-service/controller"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,105 +25,155 @@ const (
|
||||
initFile = "init.json"
|
||||
)
|
||||
|
||||
var DatabaseConfig structure.DBConfiguration
|
||||
|
||||
func init() {
|
||||
goose.AddMigration(Up, Down)
|
||||
goose.AddMigration(Initialize.Up, Initialize.Down)
|
||||
}
|
||||
|
||||
func Up(tx *sql.Tx) error {
|
||||
path := filepath.Join(database.ResolveMigrationsDirectory(), initFile)
|
||||
type IRedis interface {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.WithMessage(err, "read file")
|
||||
}
|
||||
|
||||
var list []domain.DomainWithServices
|
||||
if err := json.Unmarshal(bytes, &list); err != nil {
|
||||
return err
|
||||
list := make([]domain.DomainWithService, 0)
|
||||
err = json.Unmarshal(bytes, &list)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "json unmarshal")
|
||||
}
|
||||
|
||||
schema := DatabaseConfig.Schema
|
||||
lastDomainId := int64(0)
|
||||
lastServiceId := int64(0)
|
||||
lastAppId := int64(0)
|
||||
lastDomainId := 0
|
||||
lastServiceId := 0
|
||||
lastAppId := 0
|
||||
for _, domainInfo := range list {
|
||||
domainId := int64(0)
|
||||
if domainId, err = insertNode(tx, insertDomain, true, schema, domainInfo.Id, domainInfo.Name, domainInfo.Description, 1); err != nil {
|
||||
return err
|
||||
last, err := i.insert(tx, domainInfo)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "")
|
||||
}
|
||||
if domainId > lastDomainId {
|
||||
lastDomainId = domainId
|
||||
if last.domainId > lastDomainId {
|
||||
lastDomainId = last.domainId
|
||||
}
|
||||
|
||||
for _, service := range domainInfo.Services {
|
||||
serviceId := int64(0)
|
||||
if serviceId, err = insertNode(tx, insertService, true, schema, service.Id, service.Name, service.Description, domainId); err != nil {
|
||||
return err
|
||||
}
|
||||
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 last.serviceId > lastServiceId {
|
||||
lastServiceId = last.serviceId
|
||||
}
|
||||
if last.appId > lastAppId {
|
||||
lastAppId = last.appId
|
||||
}
|
||||
}
|
||||
|
||||
if err := setSeq(tx, schema, "domain", lastDomainId); err != nil {
|
||||
return err
|
||||
} else if err := setSeq(tx, schema, "service", lastServiceId); err != nil {
|
||||
return err
|
||||
} else if err := setSeq(tx, schema, "application", lastAppId); err != nil {
|
||||
return err
|
||||
err = i.setSeq(tx, "domain", lastDomainId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set domain")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func Down(tx *sql.Tx) error {
|
||||
func (i *initializeMigration) Down(tx *sql.Tx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertNode(tx *sql.Tx, query string, scan bool, args ...interface{}) (int64, error) {
|
||||
var id int64
|
||||
func (i *initializeMigration) insert(tx *sql.Tx, domainInfo domain.DomainWithService) (*identity, error) {
|
||||
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...)
|
||||
row := tx.QueryRow(query)
|
||||
if scan {
|
||||
if err := row.Scan(&id); err != nil {
|
||||
return 0, err
|
||||
err := row.Scan(&id)
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "row scan")
|
||||
}
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func setSeq(tx *sql.Tx, schema, table string, value int64) error {
|
||||
q := fmt.Sprintf(setSeqQuery, schema, table, value)
|
||||
func (i *initializeMigration) setSeq(tx *sql.Tx, table string, value int) error {
|
||||
q := fmt.Sprintf(setSeqQuery, i.schema, table, value)
|
||||
_, err := tx.Exec(q)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "exec db")
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user