// ReSharper disable once UnusedMember.Global // ReSharper disable once MemberCanBePrivate.Global public async Task CreateTenantAsync(IOspSession systemSession, string databaseName, string tenantId) { ArgumentValidation.Validate(nameof(systemSession), systemSession); ArgumentValidation.ValidateString(nameof(databaseName), databaseName); ArgumentValidation.ValidateString(nameof(tenantId), tenantId); var normalizedDatabaseName = databaseName.ToLower(); var normalizedTenantId = databaseName.MakeKey(); if (await IsTenantExistingAsync(systemSession, normalizedTenantId)) { throw new TenantException($"Tenant '{normalizedTenantId}' already exists."); } if (await IsDatabaseAlreadyExistingAsync(normalizedDatabaseName)) { throw new DatabaseException($"Database '{normalizedDatabaseName}' already exists."); } await _repositoryClient.CreateRepositoryAsync(normalizedDatabaseName); await _repositoryClient.CreateUser(systemSession, _systemConfiguration.AuthenticationDatabaseName, normalizedDatabaseName, string.Format(_systemConfiguration.DatabaseUser, normalizedDatabaseName), _systemConfiguration.DatabaseUserPassword); var repository = _repositoryClient.GetRepository(normalizedDatabaseName); await repository.CreateCollectionIfNotExistsAsync <CkAttribute>(); await repository.CreateCollectionIfNotExistsAsync <CkEntity>(); await repository.CreateCollectionIfNotExistsAsync <CkEntityAssociation>(); await repository.CreateCollectionIfNotExistsAsync <CkEntityInheritance>(); await repository.CreateCollectionIfNotExistsAsync <RtAssociation>(); var ospTenant = new SystemEntities.OspTenant { TenantId = normalizedTenantId, DatabaseName = normalizedDatabaseName }; await _tenantCollection.InsertAsync(systemSession, ospTenant); await RestoreTenantSystemCkModelAsync(systemSession, ospTenant); }