fix: use shared workbench database for tenants
This commit is contained in:
+7
-42
@@ -114,26 +114,18 @@ type tenantDBConfig struct {
|
|||||||
State int `db:"state"`
|
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) {
|
func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string) (*Store, error) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil, errors.New("store is nil")
|
return nil, errors.New("store is nil")
|
||||||
}
|
}
|
||||||
if tenantID <= 0 && strings.TrimSpace(tenantCode) == "" {
|
_ = ctx
|
||||||
return s, nil
|
_ = tenantCode
|
||||||
}
|
cacheID := tenantID
|
||||||
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
|
|
||||||
if cacheID <= 0 {
|
if cacheID <= 0 {
|
||||||
cacheID = tenantID
|
cacheID = defaultTenantID
|
||||||
}
|
}
|
||||||
if s.tenantID == cacheID {
|
if cacheID == s.tenant() {
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
s.tenantStoreMu.Lock()
|
s.tenantStoreMu.Lock()
|
||||||
@@ -141,34 +133,7 @@ func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string
|
|||||||
s.tenantStoreMu.Unlock()
|
s.tenantStoreMu.Unlock()
|
||||||
return st, nil
|
return st, nil
|
||||||
}
|
}
|
||||||
s.tenantStoreMu.Unlock()
|
st := &Store{db: s.db, dsn: s.dsn, tenantID: cacheID}
|
||||||
|
|
||||||
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.tenantStores[cacheID] = st
|
||||||
s.tenantStoreMu.Unlock()
|
s.tenantStoreMu.Unlock()
|
||||||
return st, nil
|
return st, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user