From 3d389df858532e5de47dd4aaf5f86b3c93422eff Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 14 Jul 2023 19:53:45 +0200 Subject: [PATCH] Fixing IndexExists for tenants with a table prefix --- src/Orchard.Web/Core/Common/Migrations.cs | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Orchard.Web/Core/Common/Migrations.cs b/src/Orchard.Web/Core/Common/Migrations.cs index 7cad00828..b2a776636 100644 --- a/src/Orchard.Web/Core/Common/Migrations.cs +++ b/src/Orchard.Web/Core/Common/Migrations.cs @@ -1,21 +1,31 @@ using System; +using System.Collections.Generic; using System.Data; +using System.Data.Common; using System.Linq; using Orchard.ContentManagement.MetaData; using Orchard.Core.Common.Models; using Orchard.Core.Contents.Extensions; using Orchard.Data; using Orchard.Data.Migration; +using Orchard.Environment.Configuration; namespace Orchard.Core.Common { public class Migrations : DataMigrationImpl { private readonly IRepository _identityPartRepository; private readonly ISessionFactoryHolder _sessionFactoryHolder; + private readonly ShellSettings _shellSettings; + + private IList existingIndexNames; - public Migrations(IRepository identityPartRepository, ISessionFactoryHolder sessionFactoryHolder) { + public Migrations( + IRepository identityPartRepository, + ISessionFactoryHolder sessionFactoryHolder, + ShellSettings shellSettings) { _identityPartRepository = identityPartRepository; _sessionFactoryHolder = sessionFactoryHolder; + _shellSettings = shellSettings; } @@ -255,18 +265,23 @@ namespace Orchard.Core.Common { } private bool IndexExists(string tableName, string indexName) { - // Database-agnostic way of checking the existence of an index. - using (var session = _sessionFactoryHolder.GetSessionFactory().OpenSession()) { - var connection = session.Connection; + if (existingIndexNames == null) { + // Database-agnostic way of checking the existence of an index. + using (var session = _sessionFactoryHolder.GetSessionFactory().OpenSession()) { + var connection = session.Connection; - if (connection == null) { - throw new InvalidOperationException("The database connection object should derive from DbConnection to check if a table exists."); + if (connection == null) { + throw new InvalidOperationException("The database connection object should derive from DbConnection to check if a table exists."); + } + + existingIndexNames = connection.GetSchema("Indexes").Rows.Cast().Select(row => + $"{row["TABLE_NAME"]}.{row["INDEX_NAME"]}").ToList(); } - - return connection.GetSchema("Indexes").Rows.Cast().Any(row => - row["TABLE_NAME"].ToString() == SchemaBuilder.TableDbName(tableName) - && row["INDEX_NAME"].ToString() == indexName); } + + var indexNamePrefix = string.IsNullOrEmpty(_shellSettings.DataTablePrefix) + ? string.Empty : $"{_shellSettings.DataTablePrefix}_"; + return existingIndexNames.Contains($"{SchemaBuilder.TableDbName(tableName)}.{indexNamePrefix}{indexName}"); } } } \ No newline at end of file