Fixing MySQL setup error in DistributedLockSchemaBuilder

This commit is contained in:
Benedek Farkas
2025-10-29 22:22:46 +01:00
parent 10522e47a0
commit 6a4988ce04
2 changed files with 16 additions and 3 deletions

View File

@@ -89,7 +89,7 @@ namespace Orchard.Data.Migration {
private void EnsureDistributedLockSchemaExists() {
// Ensure the distributed lock record schema exists.
var schemaBuilder = new SchemaBuilder(_dataMigrationInterpreter);
var distributedLockSchemaBuilder = new DistributedLockSchemaBuilder(schemaBuilder);
var distributedLockSchemaBuilder = new DistributedLockSchemaBuilder(schemaBuilder, _shellSettings);
if (!distributedLockSchemaBuilder.SchemaExists()) {
// Workaround to avoid some Transaction issue for PostgreSQL.

View File

@@ -1,24 +1,37 @@
using System;
using Orchard.Data.Migration.Schema;
using Orchard.Environment.Configuration;
namespace Orchard.Tasks.Locking.Services {
public class DistributedLockSchemaBuilder {
private readonly SchemaBuilder _schemaBuilder;
private readonly ShellSettings _shellSettings;
private const string TableName = "Orchard_Framework_DistributedLockRecord";
public DistributedLockSchemaBuilder(SchemaBuilder schemaBuilder) {
public DistributedLockSchemaBuilder(SchemaBuilder schemaBuilder, ShellSettings shellSettings) {
_schemaBuilder = schemaBuilder;
_shellSettings = shellSettings;
}
public void CreateSchema() {
_schemaBuilder.CreateTable(TableName, table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Name", column => column.NotNull().WithLength(512).Unique())
// Keep 512 chars but avoid inline .Unique() to prevent MySQL TEXT/BLOB key-length errors.
.Column<string>("Name", column => column.NotNull().WithLength(512))
.Column<string>("MachineName", column => column.WithLength(256))
.Column<DateTime>("CreatedUtc")
.Column<DateTime>("ValidUntilUtc", column => column.Nullable()));
// MySQL-specific adjustment to avoid error when adding the unique constraint.
if (string.Equals(_shellSettings.DataProvider, "MySql", StringComparison.OrdinalIgnoreCase)) {
// Force VARCHAR(512) on MySQL (some interpreters map >255 to TEXT).
_schemaBuilder.ExecuteSql(
$"ALTER TABLE {_schemaBuilder.TableDbName(TableName)} MODIFY COLUMN Name VARCHAR(512) NOT NULL;",
cmd => cmd.ForProvider("MySql"));
}
_schemaBuilder.AlterTable(TableName, table => {
table.AddUniqueConstraint("UQ_DistributedLockRecord_Name", "Name");
table.CreateIndex("IDX_DistributedLockRecord_Name", "Name");
});
}