fix: improve startup times by initializing projection tables during setup (#4642)

* fix: improve startup times by initializing projections table during setup

* add missing file
This commit is contained in:
Livio Spring
2022-11-04 10:21:58 +01:00
committed by GitHub
parent e15e733cc3
commit c791f6de58
12 changed files with 285 additions and 66 deletions

View File

@@ -3,7 +3,7 @@ package build
import "time"
var (
version = ""
version = time.Now().Format(time.RFC3339)
commit = ""
date = ""
dateTime time.Time

View File

@@ -15,6 +15,7 @@ import (
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/id"
"github.com/zitadel/zitadel/internal/query/projection"
)
type Config struct {
@@ -28,6 +29,7 @@ type Config struct {
EncryptionKeys *encryptionKeyConfig
DefaultInstance command.InstanceSetup
Machine *id.Config
Projections projection.Config
}
func MustNewConfig(v *viper.Viper) *Config {

31
cmd/setup/projections.go Normal file
View File

@@ -0,0 +1,31 @@
package setup
import (
"context"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/query/projection"
)
type projectionTables struct {
es *eventstore.Eventstore
currentVersion string
Version string `json:"version"`
}
func (mig *projectionTables) SetLastExecution(lastRun map[string]interface{}) {
mig.currentVersion, _ = lastRun["version"].(string)
}
func (mig *projectionTables) Check() bool {
return mig.currentVersion != mig.Version
}
func (mig *projectionTables) Execute(ctx context.Context) error {
return projection.Init(ctx)
}
func (mig *projectionTables) String() string {
return "projection_tables"
}

View File

@@ -8,11 +8,13 @@ import (
"github.com/spf13/viper"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/cmd/build"
"github.com/zitadel/zitadel/cmd/key"
"github.com/zitadel/zitadel/cmd/tls"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/migration"
"github.com/zitadel/zitadel/internal/query/projection"
)
var (
@@ -54,6 +56,7 @@ func Flags(cmd *cobra.Command) {
}
func Setup(config *Config, steps *Steps, masterKey string) {
ctx := context.Background()
logging.Info("setup started")
dbClient, err := database.Connect(config.Database, false)
@@ -80,6 +83,9 @@ func Setup(config *Config, steps *Steps, masterKey string) {
steps.s4EventstoreIndexes = &EventstoreIndexes{dbClient: dbClient, dbType: config.Database.Type()}
err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil)
logging.OnError(err).Fatal("unable to start projections")
repeatableSteps := []migration.RepeatableMigration{
&externalConfigChange{
es: eventstoreClient,
@@ -87,9 +93,12 @@ func Setup(config *Config, steps *Steps, masterKey string) {
ExternalPort: config.ExternalPort,
ExternalSecure: config.ExternalSecure,
},
&projectionTables{
es: eventstoreClient,
Version: build.Version(),
},
}
ctx := context.Background()
err = migration.Migrate(ctx, eventstoreClient, steps.s1ProjectionTable)
logging.OnError(err).Fatal("unable to migrate step 1")
err = migration.Migrate(ctx, eventstoreClient, steps.s2AssetsTable)