fix: use shared workbench database for tenants

This commit is contained in:
richjohnrambo
2026-09-23 11:21:05 +08:00
parent 0a3a406089
commit c8413f82d9
+6 -41
View File
@@ -114,26 +114,18 @@ type tenantDBConfig struct {
State int `db:"state"`
}
// ForTenant returns a Store connected to the tenant database configured by manager/hotgo.
// ForTenant returns a tenant-scoped Store over the configured shared database.
func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string) (*Store, error) {
if s == nil {
return nil, errors.New("store is nil")
}
if tenantID <= 0 && strings.TrimSpace(tenantCode) == "" {
return s, nil
}
cfg, err := s.tenantDBConfig(ctx, tenantID, tenantCode)
if err != nil {
return nil, err
}
if cfg.State != 1 {
return nil, fmt.Errorf("tenant disabled: id=%d code=%s", cfg.ID, cfg.Code)
}
cacheID := cfg.ID
_ = ctx
_ = tenantCode
cacheID := tenantID
if cacheID <= 0 {
cacheID = tenantID
cacheID = defaultTenantID
}
if s.tenantID == cacheID {
if cacheID == s.tenant() {
return s, nil
}
s.tenantStoreMu.Lock()
@@ -141,34 +133,7 @@ func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string
s.tenantStoreMu.Unlock()
return st, nil
}
s.tenantStoreMu.Unlock()
dsn, err := tenantDSN(s.dsn, cfg.DBName, cfg.DBDSN)
if err != nil {
return nil, err
}
if dsn == "" || dsn == s.dsn {
st := &Store{db: s.db, dsn: s.dsn, tenantID: cacheID}
s.tenantStoreMu.Lock()
if existing := s.tenantStores[cacheID]; existing != nil {
s.tenantStoreMu.Unlock()
return existing, nil
}
s.tenantStores[cacheID] = st
s.tenantStoreMu.Unlock()
return st, nil
}
st, err := openDSN(dsn)
if err != nil {
return nil, err
}
st.tenantID = cacheID
s.tenantStoreMu.Lock()
if existing := s.tenantStores[cacheID]; existing != nil {
s.tenantStoreMu.Unlock()
_ = st.Close()
return existing, nil
}
s.tenantStores[cacheID] = st
s.tenantStoreMu.Unlock()
return st, nil