mirror of https://github.com/Squidex/squidex.git
Browse Source
Agent-Logs-Url: https://github.com/Squidex/squidex/sessions/7f1356a2-bd23-4452-a915-c46854bc604d Co-authored-by: SebastianStehle <1236435+SebastianStehle@users.noreply.github.com>pull/1314/head
committed by
GitHub
8 changed files with 207 additions and 2 deletions
@ -0,0 +1,80 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Text.RegularExpressions; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
namespace Squidex.Providers; |
|||
|
|||
internal static partial class JsonFunctionMigration |
|||
{ |
|||
public static void Create(MigrationBuilder migrationBuilder, Type anchorType, string resourceName, bool splitStatements) |
|||
{ |
|||
var sqlText = ReadSql(anchorType, resourceName); |
|||
|
|||
if (splitStatements) |
|||
{ |
|||
foreach (var statement in SplitStatements(sqlText)) |
|||
{ |
|||
if (statement.StartsWith("CREATE", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
migrationBuilder.Sql(statement); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
migrationBuilder.Sql(sqlText); |
|||
} |
|||
} |
|||
|
|||
public static void Drop(MigrationBuilder migrationBuilder, Type anchorType, string resourceName, bool splitStatements) |
|||
{ |
|||
var sqlText = ReadSql(anchorType, resourceName); |
|||
|
|||
if (splitStatements) |
|||
{ |
|||
foreach (var statement in SplitStatements(sqlText).Reverse()) |
|||
{ |
|||
if (statement.StartsWith("DROP", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
migrationBuilder.Sql(statement); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
foreach (var functionName in ParseFunctions(sqlText).Reverse()) |
|||
{ |
|||
migrationBuilder.Sql($"DROP FUNCTION IF EXISTS {functionName} CASCADE;"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static string ReadSql(Type anchorType, string resourceName) |
|||
{ |
|||
using var sqlStream = anchorType.Assembly.GetManifestResourceStream(resourceName) ?? |
|||
throw new InvalidOperationException($"Cannot find embedded resource '{resourceName}'."); |
|||
|
|||
using var reader = new StreamReader(sqlStream); |
|||
|
|||
return reader.ReadToEnd(); |
|||
} |
|||
|
|||
private static string[] SplitStatements(string sqlText) |
|||
{ |
|||
return sqlText.Split(";;", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
|||
} |
|||
|
|||
private static IEnumerable<string> ParseFunctions(string sqlText) |
|||
{ |
|||
return FunctionRegex().Matches(sqlText).Select(x => x.Groups[1].Value); |
|||
} |
|||
|
|||
[GeneratedRegex(@"CREATE\s+OR\s+REPLACE\s+FUNCTION\s+([a-zA-Z0-9_]+)\s*\(", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)]
|
|||
private static partial Regex FunctionRegex(); |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace Squidex.Providers.MySql.App.Migrations |
|||
{ |
|||
[DbContext(typeof(MySqlAppDbContext))] |
|||
[Migration("20260512143000_AddJsonFunctions")] |
|||
internal sealed class AddJsonFunctions : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Create(migrationBuilder, typeof(MySqlDialect), "Squidex.Providers.MySql.json_function.sql", splitStatements: true); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Drop(migrationBuilder, typeof(MySqlDialect), "Squidex.Providers.MySql.json_function.sql", splitStatements: true); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace Squidex.Providers.Postgres.App.Migrations |
|||
{ |
|||
[DbContext(typeof(PostgresAppDbContext))] |
|||
[Migration("20260512143008_AddJsonFunctions")] |
|||
internal sealed class AddJsonFunctions : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Create(migrationBuilder, typeof(PostgresDialect), "Squidex.Providers.Postgres.json_function.sql", splitStatements: false); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Drop(migrationBuilder, typeof(PostgresDialect), "Squidex.Providers.Postgres.json_function.sql", splitStatements: false); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace Squidex.Providers.SqlServer.App.Migrations |
|||
{ |
|||
[DbContext(typeof(SqlServerAppDbContext))] |
|||
[Migration("20260512143016_AddJsonFunctions")] |
|||
internal sealed class AddJsonFunctions : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Create(migrationBuilder, typeof(SqlServerDialect), "Squidex.Providers.SqlServer.json_function.sql", splitStatements: true); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
JsonFunctionMigration.Drop(migrationBuilder, typeof(SqlServerDialect), "Squidex.Providers.SqlServer.json_function.sql", splitStatements: true); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue