Skip to content

Commit

Permalink
Fix waiting for table creation (#4638)
Browse files Browse the repository at this point in the history
* fix wait table exist

* fix wait table exist
  • Loading branch information
bxy4543 authored Mar 28, 2024
1 parent 9ee82d4 commit af438cf
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 128 deletions.
2 changes: 1 addition & 1 deletion controllers/job/init/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vet: ## Run go vet against code.
CONTROLLER_PKG=github.com/labring/sealos/controllers/pkg
CONTROLLER_LICENSE=github.com/labring/sealos/controllers/license/internal/controller
build: fmt vet ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -o bin/preset-${GOARCH} cmd/preset/main.go && chmod +x bin/preset-${GOARCH} && cp bin/preset-${GOARCH} bin/manager
CGO_ENABLED=0 GOOS=linux go build $(shell [ -n "${CRYPTOKEY}" ] && echo "-ldflags '-X github.com/labring/sealos/controllers/pkg/crypto.encryptionKey=${CRYPTOKEY} -X github.com/labring/sealos/controllers/pkg/database.cryptoKey=${CRYPTOKEY}'") -o bin/preset-${GOARCH} cmd/preset/main.go && chmod +x bin/preset-${GOARCH} && cp bin/preset-${GOARCH} bin/manager


.PHONY: run
Expand Down
26 changes: 23 additions & 3 deletions controllers/job/init/internal/util/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ func PresetAdminUser() error {
if err != nil {
return fmt.Errorf("failed to parse region %s uid: %v", os.Getenv(cockroach.EnvLocalRegion), err)
}
err = retry.Retry(10, 5*time.Second, func() error {
if !v2Account.DB.Migrator().HasTable(types.User{}) {
return fmt.Errorf("user table is null, please check")
err = retry.Retry(10, 3*time.Second, func() error {
tableTypes := []interface{}{
types.User{},
types.Region{},
types.RegionUserCr{},
types.Workspace{},
types.UserWorkspace{},
}
for _, tableType := range tableTypes {
if err := checkTableExists(v2Account, tableType); err != nil {
fmt.Println(err)
return err
}
}
return nil
})
Expand Down Expand Up @@ -121,5 +131,15 @@ func PresetAdminUser() error {
}); err != nil {
return fmt.Errorf("failed to create user: %v", err)
}
if err = v2Account.InitTables(); err != nil {
return fmt.Errorf("failed to init tables: %v", err)
}
return nil
}

func checkTableExists(m *cockroach.Cockroach, tableType interface{}) error {
if !m.DB.Migrator().HasTable(tableType) && !m.Localdb.Migrator().HasTable(tableType) {
return fmt.Errorf("%T table is null, please check", tableType)
}
return nil
}
Loading

0 comments on commit af438cf

Please sign in to comment.